1

Assuming that I set an environment variable in my Dockerfile like this:

FROM ubuntu:latest
ENV MY_NAME=YOYO

When running docker run -it my_image bash I am able to access MY_NAME like this:

root@dec16fb8dab1:/home $ echo $MY_NAME
YOYO

However, when I try docker run my_image echo $MY_NAME, not surprisingly I get nothing (since there is no MY_NAME variable on the host). How can I use docker run with a command to use the image's environement variables instead of the host's?

In a more general case, I need to see the output of docker run my_image cmd --flag $CONTAINER_ENV_VARIABLE.

Farzad Vertigo
  • 2,458
  • 1
  • 29
  • 32
  • 1
    Ask you shell to not expand `$MY_NAME` to its value, but pass it as literal argument to the command. How to do that is depend on your shell, but not docker. In `bash` single quoting should do that. – user4003407 Dec 23 '18 at 09:03
  • also, you can use: `docker run --env-file .env my_image` which spread environment variables and override its own – amir Dec 23 '18 at 10:22
  • Thanks to PetSerAl's comment. `docker run my_image /bin/bash -c 'echo $MY_NAME'` solved the issue since the base image was derived from ubuntu and bash was available. – Farzad Vertigo Dec 23 '18 at 11:27

0 Answers0