If you remember from my previous post, this is my current setup.
Ghost references the PostgreSQL server using the ip address of the Docker container running PostgreSQL. The problem is that each time I restart the container it gets a new ipaddress and I have to change the ipaddress in the config.js of Ghost. Luckily there is a better way to do this by linking containers.
When starting a container it is possible to give Docker instructions to links this container with another container using the –link command. The arguments to the link parameter are the name or id of the container you want to link to and an alias.
On the basis of this argument Docker moves information of the container you link into the container that you start. Docker creates environment variables and also changes the hosts file in which it includes the name of the linked container and the ipaddress of that container. Lucky for me, this is exactly what I needed to being able to remove the hard coded ipaddress in the config.js file.
The Ghost container can be started with the following command including the linking
If we then look into the hosts file it actually has a new entry postgres that points to the ipaddress of the postgres container.
The config.js file of Ghost can now point to the hostname of the postgres container instead of the ipaddress.
One gotcha I found out that linking of the containers is static. If you restart the Postgres container and the ip address changes, the ip address in the hosts file of the container that links to the PostgreSQL container does not automatically gets updated. You have to also restart and relink the Ghost container.