Can you tell me how to pass the CONTAINER env path file during the docker run?
More likely
docker run -it -d --env-file=<path of env file in container> NOT THE HOST ENV PATH
Thanks in advance.
Can you tell me how to pass the CONTAINER env path file during the docker run?
More likely
docker run -it -d --env-file=<path of env file in container> NOT THE HOST ENV PATH
Thanks in advance.
docker run -it -d --env-file=./somefile.txt
will read from the file and make vars inside that file available as ENV
in the container.
If you want to replace a file you put into the container at creation, you could use a mount binding.
docker run -v /some/path/on/the/host:/path/in/the/container
Docker on its own doesn't know how to do this, but it's easy to write an entrypoint wrapper script that does:
#!/bin/sh
# If the script was started with an `ENV_FILE` environment variable,
# read that file in.
if [ -n "$ENV_FILE" ]; then
. "$ENV_FILE"
fi
# Run the main container CMD.
exec "$@"
In your Dockerfile, COPY
this script in, and set the image's ENTRYPOINT
to run it. (If you already have only an ENTRYPOINT
, change it to CMD
; if you already have a wrapper script like this, add this fragment there.)
COPY entrypoint.sh .
ENTRYPOINT ["./entrypoint.sh"] # must be JSON-array form
CMD the same main container command as before
The particular setup I've shown here requires the file to be a shell script fragment (it would need to export VAR=value
for variables to be usable); other recipes like Set environment variables from file of key/value pairs can support a flat VAR=value
syntax like what docker run --env-file
would support.
Since this setup always runs the ENTRYPOINT
and it's straightforward to override the CMD
at the docker run
command, you can pretty easily check that this is working by running tools like env(1) to dump out the environment. This would happen as the exec "$@"
step at the end of the file, so after your first-time setup has happened.
docker run --rm -e ENV_FILE=env.sh my-image:latest /usr/bin/env