I am learning how to pass environment variables to Docker containers. While the following works fine,
Dockerfile
FROM ubuntu ENV USERNAME='david' CMD echo "username = $USERNAME"
Build & run commands
docker build . -t enviro docker run -d enviro
docker ps -a gives
2a3a69aa7868 enviro "/bin/sh -c 'echo "u…"
docker logs 2a3a69aa7868 gives
username = david
The following doesn’t work
Dockerfile
FROM ubuntu CMD echo "username = $USERNAME"
Build & run commands
docker build . -t enviro docker run -d enviro -e USERNAME='david'
Here the run command gives this,
docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: "-e": executable file not found in $PATH": unknown.
While docker ps -a gives
249cb045c26a enviro "-e USERNAME=david"
docker logs 249cb045c26a gives nothing
Any idea, what is going on here? Why is the environment variable not being passed?
Advertisement
Answer
OK, I got it. Instead of the following,
docker run -d enviro -e USERNAME='david'
it must be like this
docker run -d -e USERNAME='david' enviro
No idea, why docker requires the environment variable before the image’s name though.