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
we can also you host machine environment variable using -e flag and $ :
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)
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')