1

I have an existing docker container and I would like to set an environmental variable in that container. Can this be done in Docker? If so, how?

1
  • So you want to add the environment variable to a container you have already run docker create or docker run for? Commented Oct 6, 2016 at 2:37

1 Answer 1

1

Environment variables get set on a process when it starts, and are typically inherited by child processes. In Docker, you can set the environment variable on an image with an ENV command in the Dockerfile. And you can configure the default environment used to start the container when you call docker run -e ... or define the environment inside your docker-compose.yml. So there are quite a few ways to do it before your container is started.

However, once started, there's no way to change it on a running process, and Docker doesn't have a command to alter the default environment of a container that could only effect exec'd processes or a restart of the container if they did add the feature. Therefore, the best option is to stop and remove the existing container, then recreate it with the correct environment.

Sign up to request clarification or add additional context in comments.

1 Comment

Environment can also be set up in an entrypoint or start script, if one exists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.