[英]How to pass host IP as environment variable when running a docker container on OS X
I have a docker image and when I run it I need to pass the host machine IP address as an environment variable. 我有一个docker镜像,当我运行它时,我需要将主机IP地址作为环境变量传递。 So I need something like this: 所以我需要这样的东西:
docker run --rm -it -e HOST_IP=<?????> -p 8000:8000 image
I am using Docker on OS X. Basically this image is running a service that I want to connect to my local PostgreSQL server. 我在OS X上使用Docker。基本上这个映像运行的服务我想连接到我的本地PostgreSQL服务器。 The service reads the server host IP from an environment variable. 该服务从环境变量中读取服务器主机IP。
How do I get the IP of the host machine for docker to use? 如何获取主机的IP以供docker使用?
If I use local machine address 192.168.99.1 (from ifconfig
), psycopg2 complains: 如果我使用本地机器地址192.168.99.1(来自ifconfig
),psycopg2抱怨:
psycopg2.OperationalError: FATAL: no pg_hba.conf entry for host "192.168.99.100", user "postgres", database "database", SSL off
The address 192.168.99.100 mentioned in the error is the IP of my docker-machine. 错误中提到的地址192.168.99.100是我的docker-machine的IP。
How can I get the correct IP? 我怎样才能获得正确的IP?
I'm not sure you could visit the host IP inside the docker container. 我不确定你是否可以访问docker容器中的主机IP。 Because I believe they are inside different network. 因为我相信他们在不同的网络里面。
Instead of visit pgSQL on host, you should run pgSQL in another docker container, then use docker-compose.yml to connect the two docker containers, so they could link to each other, and visit each other by IP. 您应该在另一个docker容器中运行pgSQL,然后使用docker-compose.yml连接两个docker容器,以便它们可以相互链接,并通过IP访问彼此,而不是在主机上访问pgSQL。
More details on: https://docs.docker.com/compose/compose-file/ 更多细节: https : //docs.docker.com/compose/compose-file/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.