Publish the compose application including environment variables
In this step, we will learn how to include environment variables in our Docker Compose application and how this affects publishing. Environment variables are a common way to configure applications, and Docker Compose provides several ways to manage them.
First, let's stop the running Nginx container from the previous step. Navigate to the ~/project
directory if you are not already there.
cd ~/project
Stop the running services using the docker-compose down
command.
docker-compose down
Now, let's modify our docker-compose.yaml
file to use an environment variable. We will add a simple environment variable to the web
service. Open the docker-compose.yaml
file in the ~/project
directory using nano
.
nano ~/project/docker-compose.yaml
Add an environment
section to the web
service definition. We'll add a variable named MY_VARIABLE
with a value.
version: "3.8"
services:
web:
image: nginx@sha256:your_image_digest_here ## Replace with your actual digest
ports:
- "80:80"
environment:
- MY_VARIABLE=HelloFromCompose
Remember to replace your_image_digest_here
with the actual image digest you obtained in the previous step.
Save the file by pressing Ctrl + S
and exit the editor by pressing Ctrl + X
.
Now, when you start the application with docker-compose up
, the MY_VARIABLE
environment variable will be set inside the Nginx container.
docker-compose up -d
To verify that the environment variable is set inside the container, we can execute a command inside the running container using docker exec
. First, find the container ID using docker ps
.
docker ps
Note the Container ID of the running Nginx container. Then, use docker exec
to run the printenv
command inside the container and filter the output for MY_VARIABLE
.
docker exec < container_id > printenv | grep MY_VARIABLE
Replace <container_id>
with the actual ID of your running Nginx container. You should see output similar to MY_VARIABLE=HelloFromCompose
.
When publishing a Compose application that uses environment variables, you need to consider how these variables will be provided in the target environment.
One common approach is to use a .env
file. Docker Compose automatically looks for a file named .env
in the directory where the docker-compose.yaml
file is located. You can define environment variables in this file, and Compose will load them.
Let's create a .env
file in the ~/project
directory.
nano ~/project/.env
Add the following content to the .env
file:
ANOTHER_VARIABLE=ThisIsFromDotEnv
Save the file and exit nano
.
Now, let's modify the docker-compose.yaml
file to use this new environment variable. Open the docker-compose.yaml
file again.
nano ~/project/docker-compose.yaml
Add ANOTHER_VARIABLE
to the environment
section.
version: "3.8"
services:
web:
image: nginx@sha256:your_image_digest_here ## Replace with your actual digest
ports:
- "80:80"
environment:
- MY_VARIABLE=HelloFromCompose
- ANOTHER_VARIABLE
Notice that for ANOTHER_VARIABLE
, we just listed the variable name. Compose will look for this variable in the environment where docker-compose up
is run, including variables loaded from the .env
file.
Save the file and exit nano
.
Now, stop and restart the Compose application to pick up the changes.
docker-compose down
docker-compose up -d
Get the new container ID using docker ps
.
docker ps
Now, use docker exec
to check for both environment variables inside the container.
docker exec < container_id > printenv | grep MY_VARIABLE
docker exec < container_id > printenv | grep ANOTHER_VARIABLE
Replace <container_id>
with the new Container ID. You should see both MY_VARIABLE=HelloFromCompose
and ANOTHER_VARIABLE=ThisIsFromDotEnv
in the output.
When publishing, you would typically share the docker-compose.yaml
file and provide instructions on how to set the necessary environment variables, either through a .env
file or by setting them directly in the environment where Compose is run. Sensitive information should not be hardcoded in the docker-compose.yaml
or .env
files and should be managed using secrets management solutions.