Docker Compose

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.

Prerequisites

Method 1: Docker Compose Environment File

You can also use CloudTruth Parameters with Docker Compose by using env_file and creating an Environment file with CloudTruth CLI cloudtruth parameter export docker.

Inside your Docker folder create an environment file with the CloudTruth CLI.

cloudtruth parameter export docker > dotenv

Create a Dockerfile with the following contents.

FROM alpine
CMD ["printenv"]

Create a docker-compose.yml and copy the following snippet. The env_file specified will import all environment variables to use with Docker compose.

services:
app:
build: .
image: cloudtruth
container_name: ct-test
env_file:
- dotenv

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.

docker-compose up
Creating ct-test ... done
Attaching to ct-test
ct-test | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ct-test | HOSTNAME=16569452923f
ct-test | FLASK_APP=flaskdemo.py
ct-test | FLASK_DEBUG=True
ct-test | FLASK_ENV=development
ct-test | FLASK_MESSAGE=Configuration Management for Default
ct-test | FLASK_RUN_HOST=0.0.0.0
ct-test | FLASK_RUN_PORT=3000
ct-test | HOME=/root
ct-test exited with code 0

Passing variables using the --env-flag file

Alternatively the created dotenv file can be passed to Docker-Compose with the --env-file flag. This allows you to set specific variables for your app in docker-compose.yml.

services:
app:
build: .
image: cloudtruth
container_name: ct-test
environment:
- FLASK_ENV

Now you pass the --env-file where only the specified variables are displayed.

docker-compose --env-file dotenv up
Starting ct-test ... done
Attaching to ct-test
ct-test | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ct-test | HOSTNAME=ac37b482be46
ct-test | FLASK_ENV=development
ct-test | HOME=/root
ct-test exited with code 0

Method 2: Docker Compose with CloudTruth Run

Create a Dockerfile with the following contents.

FROM alpine
CMD ["printenv"]

The docker-compose.yml allows you to set specific variables for your app. The FLASK variables below are created in CloudTruth.

services:
app:
build: .
image: cloudtruth
container_name: ct-test
environment:
- FLASK_ENV
- FLASK_APP
- FLASK_RUN_PORT

Now execute docker-compose up with the cloudtruth run command which will pass your CloudTruth parameters to Docker compose and display the variables selected.

cloudtruth run -- docker-compose up
Starting ct-test ... done
Attaching to ct-test
ct-test | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ct-test | HOSTNAME=4ac024aae550
ct-test | FLASK_ENV=development
ct-test | FLASK_APP=flaskdemo.py
ct-test | FLASK_RUN_PORT=3000
ct-test | HOME=/root
ct-test exited with code 0