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.

# Install the CloudTruth CLI
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

This is an example CMD that will print the environment variables from the container environment.

CMD ["cloudtruth", "--project", "MyFirstProject", "run", "--", "printenv"]

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.

FROM alpine

# Install the CloudTruth CLI
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

CMD ["cloudtruth", "--project", "MyFirstProject", "run", "--", "printenv"]

Now you can build the image from your directory.

docker build -t tutorial .

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.

docker run -it --rm -e CLOUDTRUTH_API_KEY="YOUR_API_KEY" tutorial

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.

export CLOUDTRUTH_API_KEY="YOUR_API_KEY"
docker run -it --rm -e CLOUDTRUTH_API_KEY tutorial

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.

 cloudtruth --project MyFirstProject parameter export docker > dotenv

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.

 docker run -it --rm --env-file dotenv alpine printenv

This can also be accomplished in a single line using Bash substitution and embedding the CloudTruth CLI command directly in Docker run.

docker run -it --rm --env-file <(cloudtruth --project MyFirstProject parameter export docker) alpine printenv

Last updated

Copyright© 2023 CloudTruth