Challenge
Let’s assume we are going to run 3 containers:-
- Jenkins – http://server:8080/jenkins
- Nexus – http://server:8081/nexus
- Nginx – http://server
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:-
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:
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.
However, when running the containers, it halts with an error:-
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.
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.
This time, Jenkins is able to communicate successfully with Nexus throughhttp://server/nexus/some/pathand Nginx will handle the reverse proxy accordingly.
This solution was originally published by My Shitty Code, Embracing the Messiness in Search of Epic Solutions.
Written by Choon-Chern Lim