Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override ENV variable in base docker image

I have a base docker image, call it docker-image with Dockerfile

FROM Ubuntu
ENV USER default
CMD ['start-application']

a customized docker image, based on docker-image

FROM docker-image
ENV USER username

I want to overwrite USER Environment Variable without changing the base-image, (before the application starts), is that possible?

like image 446
Hello lad Avatar asked Jun 17 '18 06:06

Hello lad


People also ask

How do I pass an environment variable in Dockerfile?

Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.

Does docker inherit environment variables?

You can pass the values of environment variables from the host to your containers without much effort. Simply don't specify a value in the command line, and make sure that the environment variable is named the same as the variable the containerized app expects: $ docker run -e var_name (...)

How do I override a docker image?

You could just enter via docker run -it --entrypoint=/bin/bash $IMAGE -i (you 'll launch a new container from the image and get a bash shell in interactive mode), then run the entrypoint command in that container. You can then inspect the running container in the state it should be running.

Does Docker compose override environment variables?

But docker-compose does not stop at the . env and the host's current environment variables. It's cool that you can simply override values of your . env file, but this flexibility is can also be the source of nasty bugs.

How do I set environment variables during a docker image build?

To set environment variables during your image build, you will need either ENV or ARG and ENV at the same time. Docker ENV and ARG are pretty similar, but not quite the same . One of the differences: ARG can be set during the image build with --build-arg , but there is no such flag for ENV.

What are ENV and Arg variables in Docker?

In docker, if we do not set an environment variable, it will not have any value and docker compose substitutes them with an empty string. When working in docker, sometimes we might need to pass environment information to the operating container. To achieve this, we can employ both ENV and ARG variables.

What is an ENV_file in Docker?

An env_file, is a convenient way to pass many environment variables to a single command in one batch. This should not be confused with a.env file. Setting ARG and ENV values leaves traces in the Docker image. Don’t use them for secrets which are not meant to stick around (well, you kinda can with multi-stage builds).

What is the command to override an existing Docker image?

Based on the selected configuration in runtime of the container, it should be etc-override-int or etc-override-ext . A sed command could do the job during the build time of the Docker image.


Video Answer


1 Answers

If you cannot build another image, as described in "Dockerfile Overriding ENV variable", you at least can modify it when starting the container with docker run -e

See "ENV (environment variables)"

the operator can set any environment variable in the container by using one or more -e flags, even overriding those mentioned above, or already defined by the developer with a Dockerfile ENV

$ docker run -e "deep=purple" -e today --rm alpine env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=d2219b854598
deep=purple   <=============
like image 78
VonC Avatar answered Oct 22 '22 09:10

VonC