Docker Compose
Last updated
Was this helpful?
Last updated
Was this helpful?
This walkthrough will show you two methods of using CloudTruth to pass environment variables to a Docker container with Docker Compose. The first method uses an environment file and the second method uses the CloudTruth run command.
Method 1:
Good for injecting environment variables into a container
Method 2:
Good for injecting environment variables into a container
Method 3: Docker Compose with init container
Good for rendering templates as files in a container
Working knowledge of Docker and Docker-Compose.
You have created one or more .
You have created a .
You can also use CloudTruth Parameters with Docker Compose by using and creating an Environment file with CloudTruth CLI cloudtruth parameter export docker
.
Inside your Docker folder create an environment file with the CloudTruth CLI.
Create a Dockerfile
with the following contents.
With the Dockerfile that you created above, create a new container image called cloudtruth
Create a docker-compose.yml
and copy the following snippet. The env_file
specified will import all environment variables to use with Docker compose.
Issuing the following command will build the Docker container and issue the printenv
command which will display all of the parameters passed in the dotenv file that are now sourced and available to use within your Docker environment.
You'll see an output to the terminal with your parameters, similar to the output below.
Now you pass the --env-file
where only the specified variables are displayed.
This method will use the cloudtruth CLI to add CloudTruth parameters to the environment docker-compose runs in, allowing you to inject the values into the services controlled by docker-compose.
Create a Dockerfile
with the following contents.
Now execute docker-compose up
with the cloudtruth run
command which will pass your CloudTruth parameters to Docker compose and display the variables selected.
This method will use the CloudTruth init (sidecar) container to render templates into a volume(s) shared with the target services in your docker-compose file.
This example injects a simple static HTML page into an nginx container. The static HTML page is stored as a template in CloudTruth.
The init container will scan the specified project and environment for all templates with a special comment on the first line that specifies the file to render the template to. It must be on the first line and it looks like this:
For this example, CloudTruth created a template named `DEMO_PAGE` in project "MyFirstProject", but feel free to change the project name based on your use case. The template references the automatic parameter "cloudtruth.environment", which will insert the name of the environment the template was pulled from.
You'll notice that the template has the special comment as the first line: `#~ target: ...`. The init container will find this template and render it to "/usr/share/nginx/html/index.html".
Here is the template:
This example uses the public nginx container image, but you can replace this with your own container image.
Create a docker-compose.yml file or use one you already have. There are three important pieces to note, that make this whole solution work:
Include the cloudtruth/templates
image as a service in the docker-compose file. You will see that listed as the web_init service in the example file below.
One or more named volumes shared between the target container (web) and the init container (web_init). In this case, we use the "static-content" named volume. Be sure to mount the named volumes to the same location in both the init container and the target container.
Add the `depends_on` stanza to the container you want to inject the template into. The target container (web) should depend on the CloudTruth init container (web_init)
The full docker-compose.yml file for this example:
You will need to set a few environment variables for this exercise:
`CLOUDTRUTH_API_KEY` = your APi-Key
`CLOUDTRUTH_PROJECT` = the project you want to render templates from
`CLOUDTRUTH_ENVIRONMENT` = the environment you want to use values from to render into the template
Now run docker-compose:
After a few seconds, the nginx container should be running with the web page hosted at http://localhost:8585
In this case, I ran with `CLOUDTRUTH_ENVIRONMENT=production` and can see that reflected in the web page:
Alternatively, the created dotenv
file can be passed to Docker-Compose with the --env-file
flag. This allows you to for your app in docker-compose.yml
.
The docker-compose.yml
allows you to for your app. For example, you can create variables/parameters for a Flask app, which are created in CloudTruth.
Source code for the CloudTruth init container: