简体   繁体   中英

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. 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. The service reads the server host IP from an environment variable.

How do I get the IP of the host machine for docker to use?

If I use local machine address 192.168.99.1 (from ifconfig ), psycopg2 complains:

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.

How can I get the correct IP?

I'm not sure you could visit the host IP inside the docker container. 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.

More details on: https://docs.docker.com/compose/compose-file/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM