Docker
This walkthrough will show you two methods of using CloudTruth to pass environment variables to a Docker container. The first method installs the CloudTruth CLI directly in your docker image, the second method uses a CloudTruth environment file with docker run.

Prerequisites

Method 1: Docker CloudTruth CLI

The CloudTruth CLI can be installed as part of your docker Linux container.
1
# Install the CloudTruth CLI
2
RUN (curl -sL https://github.com/cloudtruth/cloudtruth-cli/releases/latest/download/install.sh || wget -qO- https://github.com/cloudtruth/cloudtruth-cli/releases/latest/download/install.sh) | sh
Copied!
This is an example CMD that will print the environment variables from the container environment.
1
CMD ["cloudtruth", "--project", "MyFirstProject", "run", "--", "printenv"]
Copied!
To try this out, create a new directory called tutorial. Inside the directory make a new Dockerfile with the following contents. Depending on your container image you may need to install curl or wget.
1
FROM alpine
2
3
# Install the CloudTruth CLI
4
RUN (curl -sL https://github.com/cloudtruth/cloudtruth-cli/releases/latest/download/install.sh || wget -qO- https://github.com/cloudtruth/cloudtruth-cli/releases/latest/download/install.sh) | sh
5
6
CMD ["cloudtruth", "--project", "MyFirstProject", "run", "--", "printenv"]
Copied!
Now you can build the image from your directory.
1
docker build -t tutorial .
Copied!
Run your tutorial Docker image and pass in the CloudTruth API key to allow CloudTruth to authenticate in the container instance. This will print your CloudTruth Parameters and container environment variables.
1
docker run -it --rm -e CLOUDTRUTH_API_KEY="YOUR_API_KEY" tutorial
Copied!
You can also export the CloudTruth API key to your local environment and then pass the key into the container without specifying the key value.
1
export CLOUDTRUTH_API_KEY="YOUR_API_KEY"
2
docker run -it --rm -e CLOUDTRUTH_API_KEY tutorial
Copied!

Method 2: Docker Environment File

You can also use CloudTruth Parameters inside a Docker container by using Docker run --env-file and passing parameters with the cloudtruth parameter export docker CloudTruth CLI command from your local environment.
You can create an environment file with the CloudTruth CLI.
1
cloudtruth --project MyFirstProject parameter export docker > dotenv
Copied!
The dotenv file can now be passed to Docker with the --env-file flag. This will pull down an alpine container, issue the printenv command and display your CloudTruth parameters and container environment variables.
1
docker run -it --rm --env-file dotenv alpine printenv
Copied!
This can also be accomplished in a single line using Bash substitution and embedding the CloudTruth CLI command directly in Docker run.
1
docker run -it --rm --env-file <(cloudtruth --project MyFirstProject parameter export docker) alpine printenv
Copied!
Last modified 2mo ago