简体   繁体   English

在OS X上运行docker容器时如何将主机IP作为环境变量传递

[英]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.

相关问题 如何在mac OS中将主机IP地址传递给docker? - How to pass host ip address to docker in mac OS? 如何将主机 IP 地址传递给 docker 容器 - How do you pass in host IP address to docker container 使用 --network=host 在 MacOS 上运行 docker 容器时如何访问主机上的端口? - How to access a port on the host machine when running docker container on MacOS with --network=host? Mac OS X sudo docker 无法连接到 Docker 守护程序。 docker 守护进程是否在此主机上运行? - Mac OS X sudo docker Cannot connect to the Docker daemon. Is the docker daemon running on this host? 从boot2docker / osx中运行的docker容器中查找主机ip - Find host ip from a docker container running in boot2docker / osx 如何将文件从 docker 虚拟机复制到 OS X 上的主机? - How to copy file from docker virtual machine to the host on OS X? Docker容器 - 如何配置以便在vagrant中运行时获得可行的IP地址? - Docker container - how to configure so it gets a viable IP address when running in vagrant? 使用Docker在OS X上进行前端开发并从主机到容器通过ENV的最佳实践是什么 - What is the best practices of using Docker for front end development on OS X and passthrough ENV from host to container 无法在Mac OS X主机上的Windows 10 VirtualBox VM中运行基于docker的Windows容器 - Cannot run docker based Windows container in Windows 10 VirtualBox VM on Mac OS X host 如何从其他主机访问在MacOSX上运行的docker容器? - How to access a docker container running on MacOSX from another host?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM