We have a Docker Compose stack with Jenkins, SonarQube, and PostgresSQL, check the SonarQube: running tests from Jenkins Pipeline in Docker post.
The problem is that they are described in the same Docker Compose file and are restarted by the same systemd file (check the Linux: systemd сервис для Docker Compose post, Rus)
Thus, if need to restart a SonarQube – will have to restart all of them including Jenkins, where are jobs running.
So the task will be to split those three services into the two Compose files, but leave the communication ability without changing URLs to connect between containers.
Will use the external
feature here.
Compose version 3.5.
Contents
Jenkins network
In a Jenkins’ Compose file – add a name
to create a custom network called jenkins:
version: '3.5' networks: jenkins: name: jenkins services: jenkins: user: root image: jenkins/jenkins:2.176.2 networks: - jenkins ports: - '8080:8080' - '50000:50000' environment: - JENKINS_HOME=/var/lib/jenkins - JAVA_OPTS=-Duser.timezone=Europe/Kiev - JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Dhudson.model.DirectoryBrowserSupport.CSP=\"default-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src 'self' 'unsafe-inline' data:;\"" logging: driver: "journald"
Otherwise, Docker will create a network with a servceiname_networkname name, which doesn’t look too good.
SonarQube network
Then in the SonarQube’s Compose file describe an additional network called sonar – but as an external of the jenkins network:
version: '3.5' networks: sonar: external: name: jenkins services: sonarqube: user: 1004:1004 image: sonarqube:7.9.1-community ports: - "9000:9000" networks: - sonar environment: - sonar.jdbc.url=jdbc:postgresql://db:5432/sonar volumes: - /data/sonarqube/conf:/opt/sonarqube/conf - /data/sonarqube/logs:/opt/sonarqube/logs - /data/sonarqube/temp:/opt/sonarqube/temp - /data/sonarqube/data:/opt/sonarqube/data - /data/sonarqube/extensions:/opt/sonarqube/extensions - /data/sonarqube/bundled_plugins:/opt/sonarqube/lib/bundled-plugins logging: driver: "journald" db: image: postgres networks: - sonar environment: - POSTGRES_USER=sonar - POSTGRES_PASSWORD=sonar volumes: - /data/sonarqube/postgresql:/var/lib/postgresql - /data/sonarqube/postgresql_data:/var/lib/postgresql/data logging: driver: "journald"
Restart the service and list networks:
[simterm]
root@jenkins-production:/opt/jenkins# docker network ls NETWORK ID NAME DRIVER SCOPE ... fb22545d1fae jenkins bridge local ...
[/simterm]
Check containers using this network:
[simterm]
root@jenkins-production:/opt/jenkins# docker network inspect jenkins | jq '.[] | .Containers[].Name' "jenkins_db_1" "jenkins_sonarqube_1" "jenkins_jenkins_1"
[/simterm]
And check connections using services names, for example, SonarQube’s Compose – sonarqube:
[simterm]
root@jenkins-production:/opt/jenkins# docker exec -ti jenkins_jenkins_1 curl -I sonarqube:9000 HTTP/1.1 200
[/simterm]
Or db:
[simterm]
root@jenkins-production:/opt/jenkins# docker exec -ti jenkins_jenkins_1 telnet db 5432 Trying 172.20.0.3... Connected to db. Escape character is '^]'.
[/simterm]
Done.