0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • You can add Environment Variables using ENV command inside the Dockerfile. Or copy over a file into your image during build and set them as environment varibles using a script – Vijayaraghavan Sundararaman Jun 19 '21 at 13:54
  • 1
    main agenda . step 1. file (.env) will be copied to dockerFile 2.during container start i want specify the path of env (inside the container) ... – ravi rocky Jun 19 '21 at 13:58
  • If you copy the environment file into the container during build, it'll always be in the same spot. So passing the location at run-time seems odd. Why don't you set the location at build time using a ENV statement? – Hans Kilian Jun 19 '21 at 14:33

2 Answers2

0

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

ethergeist
  • 599
  • 4
  • 14
0

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
David Maze
  • 130,717
  • 29
  • 175
  • 215