How do I pass environment variables to Docker containers?

前端 未结 14 1407
独厮守ぢ
独厮守ぢ 2020-11-22 11:15

I\'m new to Docker, and it\'s unclear how to access an external database from a container. Is the best way to hard-code in the connection string?

# Dockerfil         


        
14条回答
  •  情深已故
    2020-11-22 11:23

    If you have the environment variables in an env.sh locally and want to set it up when the container starts, you could try

    COPY env.sh /env.sh
    COPY .jar /.jar
    ENTRYPOINT ["/bin/bash" , "-c", "source /env.sh && printenv && java -jar /.jar"]
    

    This command would start the container with a bash shell (I want a bash shell since source is a bash command), sources the env.sh file(which sets the environment variables) and executes the jar file.

    The env.sh looks like this,

    #!/bin/bash
    export FOO="BAR"
    export DB_NAME="DATABASE_NAME"
    

    I added the printenv command only to test that actual source command works. You should probably remove it when you confirm the source command works fine or the environment variables would appear in your docker logs.

提交回复
热议问题