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.
1
cloudtruth --project MyFirstProject parameter export docker > dotenv
Copied!
Create a Dockerfile with the following contents.
1
FROM alpine
2
CMD ["printenv"]
Copied!
Create a docker-compose.yml and copy the following snippet. The env_file specified will import all environment variables to use with Docker compose.
1
services:
2
app:
3
build: .
4
image: cloudtruth
5
container_name: ct-test
6
env_file:
7
- dotenv
Copied!
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.
1
docker-compose up
Copied!
1
Creating ct-test ... done
2
Attaching to ct-test
3
ct-test | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
4
ct-test | HOSTNAME=16569452923f
5
ct-test | FLASK_APP=flaskdemo.py
6
ct-test | FLASK_DEBUG=True
7
ct-test | FLASK_ENV=development
8
ct-test | FLASK_MESSAGE=Configuration Management for Default
9
ct-test | FLASK_RUN_HOST=0.0.0.0
10
ct-test | FLASK_RUN_PORT=3000
11
ct-test | HOME=/root
12
ct-test exited with code 0
Copied!

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.
1
services:
2
app:
3
build: .
4
image: cloudtruth
5
container_name: ct-test
6
environment:
7
- FLASK_ENV
Copied!
Now you pass the --env-file where only the specified variables are displayed.
1
docker-compose --env-file dotenv up
Copied!
1
Starting ct-test ... done
2
Attaching to ct-test
3
ct-test | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
4
ct-test | HOSTNAME=ac37b482be46
5
ct-test | FLASK_ENV=development
6
ct-test | HOME=/root
7
ct-test exited with code 0
Copied!

Method 2: Docker Compose with CloudTruth Run

Create a Dockerfile with the following contents.
1
FROM alpine
2
CMD ["printenv"]
Copied!
The docker-compose.yml allows you to set specific variables for your app. The FLASK variables below are created in CloudTruth.
1
services:
2
app:
3
build: .
4
image: cloudtruth
5
container_name: ct-test
6
environment:
7
- FLASK_ENV
8
- FLASK_APP
9
- FLASK_RUN_PORT
Copied!
Now execute docker-compose up with the cloudtruth run command which will pass your CloudTruth parameters to Docker compose and display the variables selected.
1
cloudtruth --project MyFirstProject run -- docker-compose up
2
Starting ct-test ... done
3
Attaching to ct-test
4
ct-test | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
5
ct-test | HOSTNAME=4ac024aae550
6
ct-test | FLASK_ENV=development
7
ct-test | FLASK_APP=flaskdemo.py
8
ct-test | FLASK_RUN_PORT=3000
9
ct-test | HOME=/root
10
ct-test exited with code 0
Copied!
Last modified 2mo ago