set environment variable in running docker contianer

前端 未结 2 718
遇见更好的自我
遇见更好的自我 2021-01-24 14:27

I need to set environment variable in a running docker container. I am already aware of the way of setting environment variable while creating a container. As far I found there

相关标签:
2条回答
  • 2021-01-24 15:26

    If you have a running process in the docker and you are attempting to change the environment variable in the docker so the running process will dynamically change - this will not work. The environment variables of a process are set when it starts. You can see here ways to overcome that, but I don't think that is the right way to go.

    I would instead, have a configuration file that the file reads (or listens to) periodically. And when you want to change the configuration change the file.

    If this isn't your scenario, please describe your scenario so we can better assist you.

    0 讨论(0)
  • 2021-01-24 15:29

    I find a way to provide environment variable to a running container. Fist upgrade your docker-engine. I am using V1.12.5.

    create a script with environment variables-

    #!/bin/bash
    
    echo "export VAR1=VAL1
    export VAR2=VAL2" >> /etc/bash.bashrc
    source /etc/bash.bashrc
    

    Now start a container. Here, 'test' is the container name:

    docker run -idt --name=test ubuntu
    

    Copy your script to container:

    docker cp script.sh test:/
    

    Run the script :

    docker exec -it test /bin/bash -c "/script.sh"
    

    Restart your container:

    docker restart test 
    

    Go to container shell

    docker exec -it test /bin/bash
    

    Check the variable

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