Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose, can't print environment variable in docker container

I want to print environment variable defined in docker-compose file.

Here is my docker-compose-1.yml:

version: '3.6'

services:
  busybox:
    image: busybox
    command: 'echo $DEBUG'
    environment:
      - DEBUG='123456'

And, try to print the DEBUG environment variable in docker container:

☁  environment-variables-in-compose [master] ⚡  docker-compose -f docker-compose-1.yml up
WARNING: The DEBUG variable is not set. Defaulting to a blank string.
Creating network "environmentvariablesincompose_default" with the default driver
Creating environmentvariablesincompose_busybox_1 ... done
Attaching to environmentvariablesincompose_busybox_1
busybox_1  |
environmentvariablesincompose_busybox_1 exited with code 0

As you can see, got WARNING: The DEBUG variable is not set. Defaulting to a blank string.

Am I wrong?

update 1:

docker-compose-1.yml:

version: '3.6'

services:
  busybox:
    image: busybox
    command: 'echo $$DEBUG'
    environment:
      DEBUG: 123456

I change command using double $ sign, but the result is still not what I want.

☁  environment-variables-in-compose [master] ⚡  docker-compose -f docker-compose-1.yml up
Creating network "environmentvariablesincompose_default" with the default driver
Creating environmentvariablesincompose_busybox_1 ... done
Attaching to environmentvariablesincompose_busybox_1
busybox_1  | $DEBUG
environmentvariablesincompose_busybox_1 exited with code 0

As you can see, the environment variable is printed as $DEBUG, not expected value 123456

like image 659
slideshowp2 Avatar asked Jun 25 '18 11:06

slideshowp2


People also ask

Can you use environment variables in Docker compose?

Configure Compose using environment variablesSeveral environment variables are available for you to configure the Docker Compose command-line behavior.

How do I pass environment variables to docker containers?

Using –env, -e When we launch our Docker container, we can pass environment variables as key-value pairs directly into the command line using the parameter –env (or its short form -e). As can be seen, the Docker container correctly interprets the variable VARIABLE1.

How do I set an environment variable on an existing container?

Use the -e , --env , and --env-file flags to set simple (non-array) environment variables in the container you're running, or overwrite variables that are defined in the Dockerfile of the image you're running.

Does docker use .env file?

Here's a list of easy takeaways: The . env file, is only used during a pre-processing step when working with docker-compose. yml files.


1 Answers

Compose will expand variables from the environment where you are running compose. To expand the variable inside your container, escape the $ using $$:

command: '/bin/sh -c "echo $$DEBUG"'

For more details, see: https://docs.docker.com/compose/compose-file/#variable-substitution

Edit: I've also added a shell inside the container to expand the variable.

like image 174
BMitch Avatar answered Oct 17 '22 15:10

BMitch