Docker: Handling Circular Dependency Between Containers

December 07, 2017 By Choon-Chern Lim

2 minute read time

Challenge

Let’s assume we are going to run 3 containers:-

Nginx is used to serve cleaner URLs through reverse proxies so that users will access http://server/jenkins and http://server/nexus instead of remembering specific ports.

So, the simplified docker-compose.yml looks like this:-

shittycode1

While http://server/jenkins and http://server/nexus work flawlessly, the Jenkins container is unable to communicate with Nexus through http://server/nexus/some/path, which is handled by Nginx.

Hence, when a Jenkins job tries to pull artifacts from Nexus, the following error is thrown:

shittycode2

SOLUTION: ATTEMPT #1

The first attempt is to set up a link between Jenkins and Nginx with the Nginx alias pointing to the hostname, which is server.

The goal is when Jenkins communicate with Nexus through http://server/nexus/some/path, Nginx will handle the reverse proxy accordingly.

shittycode3

However, when running the containers, it halts with an error:-

shittycode4

SOLUTION: ATTEMPT #2

In effort to prevent the circular dependency problem, we can set up a link between Jenkins and Nexus with the Nexus alias pointing to the hostname, which is server.

This way, Jenkins communicate directly with Nexus through http://server:8081/nexus/some/pathand Nginx will stay out of it.

shittycode5

This works without problem.

However, this configuration somewhat defeats the purpose of using Nginx because while the users may access Jenkins and Nexus without specifying custom ports, Jenkins has to communicate with Nexus using port 8081.

Furthermore, this Nexus port is fully exposed in the build logs in all Jenkins jobs.

 

SOLUTION: ATTEMPT #3

The last attempt is to configure Nginx with the hostname as a network alias.

shittycode6

This time, Jenkins is able to communicate successfully with Nexus throughhttp://server/nexus/some/pathand Nginx will handle the reverse proxy accordingly.

6835135[1].jpegThis solution was originally published by My Shitty Code, Embracing the Messiness in Search of Epic Solutions. 

 

Tags: Nexus 3, jenkins, nginx, technical

Written by Choon-Chern Lim