I have a few Docker containers running like:
- Nginx
- Web app 1
- Web app 2
- PostgreSQL
Since Nginx needs to connect to the web application servers inside web app 1 and 2, and the web apps need to talk to PostgreSQL, I have linkages like this:
- Nginx --- link ---> Web app 1
- Nginx --- link ---> Web app 2
- Web app 1 --- link ---> PostgreSQL
- Web app 2 --- link ---> PostgreSQL
This works pretty well at first. However, when I develop a new version of web app 1 and web app 2, I need to replace them. What I do is remove the web app containers, set up new containers and start them.
For the web app containers, their IP addresses at first would be something like:
- 172.17.0.2
- 172.17.0.3
And after I replace them, they will have new IP addresses:
- 172.17.0.5
- 172.17.0.6
Now, those exposed environment variables in the Nginx container are still pointing to the old IP addresses. Here comes the problem. How do I replace a container without breaking linkage between containers? The same issue will also happen to PostgreSQL. If I want to upgrade the PostgreSQL image version, I certainly need to remove it and run the new one, but then I need to rebuild the whole container graph, so this is not ideal for real-life server operation.