Skip to main content

Environment variables

Using environment variables

There are several ways to use environment variables (env vars) in your apps.

  • Hardcoded environment variables in your project compose file, using the env or args keywords.
    More on environment variables and build arguments.
  • Passing external environment variables to a compose file, using the ${MY_ENV_VAR} syntax in your docker-compose.yml file.
  • Doing this manually or with a custom script by adding optional .env and entrypoint files in the root of the project.
    See this example on how to pass environment variables from a .env file to a service in the compose file, which in turn injects them into the Docker container.

Environment variables in builds

You can create a .env and/or entrypoint file in the root of your project.
These files will be sourced by the Swarmlet deployer_agent service before building and deploying an app.

Print out all the current environment variables used by deployer_agent:

ENV=$(docker container exec $(docker ps --filter name=deployer_agent -q) /entrypoint printenv) && echo "${ENV%%BASH_FUNC*}"

Adding environment variables to your apps

Use Docker configs and secrets to store different types of environment variables in your swarm. See the Swarmlet Secrets and configs documentation section. Secrets and configs can be added to services by specifying them in - you guessed it - the project docker-compose.yml file.

# On a swarm manager node
echo $MY_CONF | docker config create my-config -
version: "3.7"

image: alpine
command: sh -c 'echo "Contents of /my-config:"; cat /my-config'
### Or:
# command: sh -c 'set -o allexport; source /my-config; printenv'
- my-config
mode: replicated
replicas: 1

external: true

Deploy the stack, either create a local project and push (as described here), or copy-paste the above snippet to the server and run:

docker stack deploy --compose-file docker-compose.yml env-vars
docker service logs env-vars_my-service -f