How do I pass environment variables to Docker containers?

前端 未结 14 1515
独厮守ぢ
独厮守ぢ 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:37

    Use -e or --env value to set environment variables (default []).

    An example from a startup script:

     docker run  -e myhost='localhost' -it busybox sh
    

    If you want to use multiple environments from the command line then before every environment variable use the -e flag.

    Example:

     sudo docker run -d -t -i -e NAMESPACE='staging' -e PASSWORD='foo' busybox sh
    

    Note: Make sure put the container name after the environment variable, not before that.

    If you need to set up many variables, use the --env-file flag

    For example,

     $ docker run --env-file ./my_env ubuntu bash
    

    For any other help, look into the Docker help:

     $ docker run --help
    

    Official documentation: https://docs.docker.com/compose/environment-variables/

提交回复
热议问题