Override ENV variable in base docker image - docker

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?

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 <=============

Related

Docker - List environment variables defined in dockerfile from within a container

Is there a way to list all the environment variables defined in dockerfile (or docker-compose.yml) from within a container but only the ones defined in dockerfile, without variables like PATH, PS1, TERM,... ??
My container is the php-apache-dev container from webdevops which is based on the ubuntu image.
You should not assign root privileged just for the sake of ENV, better to print all env and exclude the system environment that you are not intrested in.
The best option is to grep only that are defined in Dockerfile is to print all env that start from WEB_, as your mentioned Dockerfile ENV all start from WEB_*.
ENV WEB_DOCUMENT_ROOT=/app \
WEB_DOCUMENT_INDEX=index.php \
WEB_ALIAS_DOMAIN=*.vm \
WEB_PHP_TIMEOUT=600 \
WEB_PHP_SOCKET=""
ENV WEB_PHP_SOCKET=127.0.0.1:9000
ENV WEB_NO_CACHE_PATTERN="\.(css|js|gif|png|jpg|svg|json|xml)$"
So this will print Dockerfile ENV only.
docker run -it --rm webdevops/php-apache-dev printenv | grep -E "^WEB"
Or to exclude some variables you can try
docker run -it --rm webdevops/php-apache-dev printenv | grep -vE "^PATH|^HOME|^TERM"
this will not show your mentioned ENV that you are not intrested in.

Can Docker environment variables be used as an dynamic entrypoint runtime arg?

I'm trying to parameterize my Dockerfile running nodeJS so I can have my entrypoint command's args be customizable on docker run so I can maintain one container artifact that can be deployed repeatedly with variations to some runtime args.
I've tried a few different ways, the most basic being
ENV CONFIG_FILE=default.config.js
ENTRYPOINT node ... --config ${CONFIG_FILE}
What I'm finding is that whatever value is defaulted remains in my docker run command even if I'm using -e to pass in new values. Such as
docker run -e CONFIG_FILE=desired.config.js
Another Dockerfile form I've tried is this:
ENTRYPOINT node ... --config ${CONFIG_FILE:-default.config.js}
Not specifying the environment variable with an ENV directive, but using bash expansion to specify a default value if nonexistent or null is found. This gives me the same behavior though.
Lastly, the last thing I tried was to create a bash script file that contains the same entrypoint command, then ADD it to the docker context and invoke it in my ENTRYPOINT. And this also seems to give the same behavior.
Is what I'm attempting even possible?
EDIT:
Here is a minimal dockerfile that reproduces this behavior for me:
FROM alpine
ENV CONFIG "no"
ENTRYPOINT echo "CONFIG=${CONFIG}"
Here is the build command:
docker build -f test.Dockerfile -t test .
Here is the run command, which echoes no despite the -e arg:
docker run -t test -e CONFIG=yes
Some additional details,
I'm running OSX sierra with a Docker version of 18.09.2, build 6247962

Docker Entrypoint environment variables not printed

I'm new to Docker. All I want is to print an environment variable I pass to docker run via the -e flag. My Dockerfile looks like this:
FROM openjdk:8-jdk-alpine
ENTRYPOINT echo $TEST
I build my image with docker build -t test-docker . and execute it with docker run test-docker -e TEST=bar. It just prints an empty line and exits.
This happens because you run the image having parameters in wrong order, should be:
docker run --rm -e TEST=bar test-docker
Notice the env var is specified before the image name. Everything after your image name is considered as an argument of your container.
Use --rm always when playing to prevent garbage containers from piling up.

Dockerfile: Inherit environmental variables from shell

When building an image using a Dockerfile, in order to make some env vars available to the docker build context, one should explicitly declare associations of the form
ENV MYVAR=MYVALUE
AFAIK (correct me if I am misguided here), the environmental variables exported to the shell from which the docker build command is executed, are not passed to the Docker build context, i.e. if in my shell I have previously
export FOO=BAR
having the following declaration in my Dockerfile
ENV FOO=$FOO
and then echoing (still from within the Dockerfile) $FOO will print an empty string.
So if all of the above is correct, my question is if there is a way for the docker build context to inherit the environment of the shell being called from.
You could define default values with ARG:
ARG build_var=default_value
ENV ENV_VAR=$build_var
and then override at build time:
docker build --build-arg build_var=$HOST_VAR
You can get the value from your terminal and pass it like this...
$ export test="works"
$ docker run --name your_image_name -e TEST="${test}" -d -P your_image
$ docker exec -it your_image_name /bin/bash
$ env
...
TEST=works

How to set default docker environment variables

I'd like to set the env variable SERVICE_CHECK_TTL for all containers by default. Can I somehow use the docker deamon for that like this broken example of setting a default env variable for all containers:
ExecStart=/usr/bin/docker daemon --env SERVICE_CHECK_TTL=30s -H fd://
The failing example is part of the docker.service file. The env variable SERVICE_CHECK_TTL is used by the Registrator that registers containers in Consul.
EDIT:
I don't want to set this env variable in a Dockerfile or a docker-compose file if there is another way of setting env variables that are the same for all containers (default). The reason is that I'd like to avoid changing every single Dockerfiles and every single docker-compose file.
The ENV directive in a Dockerfile is designed for that, have a look at the docker docs, they are very good.
So let's suppose all your containers use debian Jessie, you could put in a Dockerfile FROM debian
ENV xxx yyy, then build your specific debian docker build -t mydebian . and then, when you build your containers, your Dockerfile always starts with FROM mydebian
You now have your specific ENV value for all your containers
Of course, you may replace debian by ubuntu, centos or any other
Use this command (remember to change the Docker Host) :
docker exec -i CONTAINER_ID /bin/bash -c "export DOCKER_HOST=tcp://localhost:port"
OR
echo 'export DOCKER_HOST=tcp://localhost:port' >> ~/.bashrc

Resources