How do I pass environment variables to Docker containers?

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

    we can also you host machine environment variable using -e flag and $ :

    Before running need to export(means set) local env variable and file or just before using

    docker run -it -e MG_HOST=$MG_HOST -e MG_USER=$MG_USER -e MG_PASS=$MG_PASS -e MG_AUTH=$MG_AUTH -e MG_DB=$MG_DB -t image_tag_name_and_version 
    

    By using this method set env variable automatically with your given name in my case(MG_HOST ,MG_USER)

    Additional:

    If you are using python you can access these envment variable inside docker by

    import os
    host,username,password,auth,database=os.environ.get('MG_HOST'),os.environ.get('MG_USER'),os.environ.get('MG_PASS'),os.environ.get('MG_AUTH'),os.environ.get('MG_DB')
    

提交回复
热议问题