Restarting a named container assigns different IP addresses

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?

+6
source share
3 answers

2nd UPDATE: you may have already found this, but as a workaround I plan to display the service for sharing the database with the host interface (ej: with -p 5432: 5432) and connect webapps to the host IP (docker0 interface IP: on my Ubuntu and CentOS IP - 172.17.42.1). If you restart the postgres container, the sender's IP address will change, but I will be available using 172.17.42.1:5432. The disadvantage is that you open this port to all containers and lose the fine-grained map that the link gives you.

--- OLD UPDATES:

CORRECTION: Docker will map "postgres" to the container IP address in the / etc / hosts files in the webapp container. So in the webapp container you can ping 'postgres' and it will be mapped to IP.

1st UPDATE: I saw that Docker generates and mounts /etc/hosts,/etc/resolv.conf etc. to always have the correct information, but this does not apply when the associated container restarts. So, I assumed (erroneously) that Docker will update host files.

- ORIGINAL (incorrect) answer:

Add --hostname = postgres-db (you can use anythin, I use something other than "postgres" to avoid confusion with the container name):

 $ docker run --name postgres --hostname postgres-db postgres/image 

Docker will map 'postgres-db' to the IP address of the container (check the contents of / etc / hosts in the webapp container).

This will allow you to run ping postgres-db from the webapp container. If the IP changes, Dockers will update / etc / hosts for you.

In a Django application, use "postgres-db" instead of IP (or something you use for the --hostname container with PostgreSql).

Bye! Horacio

0
source

According to https://docs.docker.com/engine/reference/commandline/run/ it should be possible to assign a static IP address for your container - during container creation - using the --ip option:

Example:

 docker run -itd --ip 172.30.100.104 --name postgres postgres/image 

.... where 172.30.100.104 is the free IP address in the bridge / overlay user network.

Then it should keep the same IP address even if the postgres container fails / reboots.

It looks like it was released in Docker Engine version 1.10 or higher, so if you have a lower version, you need to upgrade first.

0
source

Starting with Docker 1.0, they have implemented a stronger sense of bound containers. Now you can use the name of the container instance as if it were the host name.

Here is the link

I found a link that better describes your problem. And although this question has been answered, I am wondering if this ambassador template can solve this problem ... this suggests that the ambassador is more reliable than the services that refer.

-1
source

Source: https://habr.com/ru/post/972149/


All Articles