I am trying to deploy my application using Docker and have run into a problem where restarting named containers assigns a different IP address to the container. Perhaps an explanation of what I'm doing will better explain the problem:
- Postgres runs inside a separate container named
"postgres"
$ PG_ID=$(docker run --name postgres postgres/image) - My webapp container container for postgres container
$ APP_ID=$(docker run --link postgres:postgres webapp/image)
Linking the image of the postgres container to the containers of the webapp container in the webapp container contains a host file entry with the IP address of the postgres container. This allows me to point to postgres db inside my webapp using postgres:5432 (I use Django btw). All this works well, if only for some reason postgres crashes.
Before manually stopping the postgres process to simulate the failure of the postgres process, I check the IP address of the postgres container:
$ docker inspect --format "{{.NetworkSettings.IPAddress}}" $PG_ID 172.17.0.73
Now, to simulate a crash, I stop the postgres container:
$ docker stop $PG_ID
If now I restart postgres using
$ docker start $PG_ID
container ip changes:
$ docker inspect --format "{{.NetworkSettings.IPAddress}}" $PG_ID 172.17.0.74
Therefore, the IP that points to the postgres container in the webapp container is no longer valid. I, however, that, by naming a container docker, assigns a name to it with certain configurations so that you can reliably bind between containers (both the network and the volumes). If the IP changes seem to defeat the target.
If I need to restart my webapp process every time I restart postgres, this does not seem to be better than just using one container to start both processes. Then I can use a supervisor or something similar to make them both work and use localhost to communicate between processes.
I'm still new to Docker, am I doing something wrong or is this a docker bug?
source share