Search code examples
macospostgresqldockerboot2dockerdocker-machine

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?


Solution

  • 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/