set environment variable in running docker contianer

前端 未结 2 717
遇见更好的自我
遇见更好的自我 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: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
    

提交回复
热议问题