I have few dockers containers running like
- Nginx
- Web app 1
- Web app 2
- PostgreSQL
Since Nginx need to connects web application server inside web app 1 and 2, and web apps need to talk to postgresql, so 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 beginning, however, when I develop new version of web app 1 and web app 2, I need to replace them. What I do is to remove web app containers, setup new containers and start them.
For web app containers, their IP address at very first would be something like
- 172.17.0.2
- 172.17.0.3
And after I replace them, they have new IP addresses now
- 172.17.0.5
- 172.17.0.6
At this moment, those exposed environment variables in Nginx container are still pointed to old IP addresses. Here comes the problem, how to replace a container without breaking linkage between other 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, this is not a good idea for real life server operation.
You can bind the connection ports of your images to fixed ports on the host and configure the services to use them instead.
This has its drawbacks as well, but it might work in your case.
The effect of
--link
is static, so it will not work for your scenario (there is currently no re-linking, although you can remove links).We have been using two different approaches at dockerize.it to solve this, without links or ambassadors (although you could add ambassadors too).
1) Use dynamic DNS
The general idea is that you specify a single name for your database (or any other service) and update a short-lived DNS server with the actual IP as you start and stop containers.
We started with SkyDock. It works with two docker containers, the DNS server and a monitor that keeps it updated automatically. Later we moved to something more custom using Consul (also using a dockerized version: docker-consul).
An evolution of this (which we haven't tried) would be to setup etcd or similar and use its custom API to learn the IPs and ports. The software should support dynamic reconfiguration too.
2) Use the docker bridge ip
When exposing the container ports you can just bind them to the
docker0
bridge, which has (or can have) a well known address.When replacing a container with a new version, just make the new container publish the same port on the same IP.
This is simpler but also more limited. You might have port conflicts if you run similar software (for instance, two containers can not listen on the 3306 port on the
docker0
bridge), etcétera… so our current favorite is option 1.You can use an ambassador container. But do not link the ambassador container to your client, since this creates the same problem as above. Instead, use the exposed port of the ambassador container on the docker host (typically 172.17.42.1). Example:
postgres volume:
postgres-container:
ambassador-container for postgres:
Now you can start a postgresql client container without linking the ambassador container and access postgresql on the gateway host (typically 172.17.42.1):
Now you can restart the ambassador container whithout having to restart the client.
Another alternative is to use the
--net container:$CONTAINER_ID
option.Step 1: Create "network" containers
Step 2: Inject services into "network" containers
As long as you do not touch the "network" containers, the IP addresses of your links should not change.
You could also try the ambassador method of having an intermediary container just for keeping the link intact... (see https://docs.docker.com/articles/ambassador_pattern_linking/ ) for more info