Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an environment variable in a running docker container

Tags:

docker

If I have a docker container that I started a while back, what is the best way to set an environment variable in that running container? I set an environment variable initially when I ran the run command.

$ docker run --name my-wordpress -e VIRTUAL_HOST=domain.example --link my-mysql:mysql -d spencercooley/wordpress

but now that it has been running for a while I want to add another VIRTUAL_HOST to the environment variable. I do not want to delete the container and then just re-run it with the environment variable that I want because then I would have to migrate the old volumes to the new container, it has theme files and uploads in it that I don't want to lose.

I would just like to change the value of VIRTUAL_HOST environment variable.

like image 719
Spencer Cooley Avatar asked Jan 07 '15 04:01

Spencer Cooley


People also ask

How do I set an environment variable in docker run?

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.

How do I see environment variables in running container?

Fetch Using docker exec Command Here, we are executing the /usr/bin/env utility inside the Docker container. Using this utility, you can view all the environment variables set inside Docker containers. Notice that our my_env_var is also present in the output.

Can I use ENV variable in Dockerfile?

Dockerfile provides a dedicated variable type ENV to create an environment variable. We can access ENV values during the build, as well as once the container runs. Let's see how we can use it to pass value to our greetings script. There are two different ways to do it.


Video Answer


4 Answers

There are generaly two options, because docker doesn't support this feature now:

  1. Create your own script, which will act like runner for your command. For example:

    #!/bin/bash
    export VAR1=VAL1
    export VAR2=VAL2
    your_cmd
    
  2. Run your command following way:

    docker exec -i CONTAINER_ID /bin/bash -c "export VAR1=VAL1 && export VAR2=VAL2 && your_cmd"
    
like image 111
mirek Avatar answered Oct 19 '22 19:10

mirek


Docker doesn't offer this feature.

There is an issue: "How to set an enviroment variable on an existing container? #8838"

Also from "Allow docker start to take environment variables #7561":

Right now Docker can't change the configuration of the container once it's created, and generally this is OK because it's trivial to create a new container.

like image 27
Bryan Avatar answered Oct 19 '22 19:10

Bryan


For a somewhat narrow use case, docker issue 8838 mentions this sort-of-hack:

You just stop docker daemon and change container config in /var/lib/docker/containers/[container-id]/config.json (sic)

This solution updates the environment variables without the need to delete and re-run the container, having to migrate volumes and remembering parameters to run.

However, this requires a restart of the docker daemon. And, until issue issue 2658 is addressed, this includes a restart of all containers.

like image 17
mknecht Avatar answered Oct 19 '22 19:10

mknecht


To:

  1. set up many env. vars in one step,
  2. prevent exposing them in 'sh' history, like with '-e' option (passing credentials/api tokens!),

you can use

--env-file key_value_file.txt

option:

docker run --env-file key_value_file.txt $INSTANCE_ID
like image 9
Mariusz Kedzierawski Avatar answered Oct 19 '22 18:10

Mariusz Kedzierawski