How to set an environment variable in a running docker container

前端 未结 11 1138
难免孤独
难免孤独 2020-11-29 23:28

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 initia

相关标签:
11条回答
  • 2020-11-30 00:13

    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"
      
    0 讨论(0)
  • 2020-11-30 00:13

    Use export VAR=Value

    Then type printenv in terminal to validate it is set correctly.

    0 讨论(0)
  • 2020-11-30 00:24

    If you are running the container as a service using docker swarm, you can do:

    docker service update --env-add <you environment variable> <service_name>

    Also remove using --env-rm

    To make sure it's addedd as you wanted, just run: docker exec -it <container id> env

    0 讨论(0)
  • 2020-11-30 00:24

    here is how to update a docker container config permanently

    1. stop container: docker stop <container name>
    2. edit container config: docker run -it -v /var/lib/docker:/var/lib/docker alpine vi $(docker inspect --format='/var/lib/docker/containers/{{.Id}}/config.v2.json' <container name>)
    3. restart docker
    0 讨论(0)
  • 2020-11-30 00:25

    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.

    0 讨论(0)
提交回复
热议问题