Docker: Compose shared networks
Tags: docker, networking, virtualization
Published: 2019-08-20 13:13:22

---
title: Docker: Compose shared networks
published: true
tags: Docker,Networking,Virtualization
canonical_url: https://rtfm.co.ua/en/docker-compose-shared-networks/
---

![](https://rtfm.co.ua/wp-content/uploads/2015/09/docker_lxc.png)We have a Docker Compose stack with Jenkins, SonarQube, and PostgresSQL, check the [SonarQube: running tests from Jenkins Pipeline in Docker](https://rtfm.co.ua/en/sonarqube-running-tests-from-jenkins-pipeline-from-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](https://rtfm.co.ua/linux-systemd-servis-dlya-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`](https://docs.docker.com/compose/networking/#use-a-pre-existing-network) feature here.

Compose version 3.5.

### Jenkins network

In a Jenkins’ Compose file – add a `name` to create a [custom network](https://docs.docker.com/compose/compose-file/#name-1) 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](https://docs.docker.com/compose/networking/#use-a-pre-existing-network) 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:

```
root@jenkins-production:/opt/jenkins# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
...
fb22545d1fae        jenkins             bridge              local
...
```

Check containers using this network:

```
root@jenkins-production:/opt/jenkins# docker network inspect jenkins | jq '.[] | .Containers[].Name'
"jenkins_db_1"
"jenkins_sonarqube_1"
"jenkins_jenkins_1"
```

And check connections using services names, for example, SonarQube’s Compose – _sonarqube_:

```
root@jenkins-production:/opt/jenkins# docker exec -ti jenkins_jenkins_1 curl -I sonarqube:9000
HTTP/1.1 200
```

Or _db_:

```
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 '^]'.
```

Done.

### Similar posts

- <small>02/27/2017</small> [Docker: Compose down – network jmadmin_default has active endpoints](https://rtfm.co.ua/docker-compose-down-network-jmadmin_default-has-active-endpoints/)
- <small>06/18/2019</small> [SonarQube: running tests from Jenkins Pipeline in Docker](https://rtfm.co.ua/en/sonarqube-running-tests-from-jenkins-pipeline-from-docker/)
- <small>08/15/2019</small> [Kubernetes: part 3 – AWS EKS overview and manual EKS cluster set up](https://rtfm.co.ua/en/kubernetes-part-3-aws-eks-overview-and-manual-eks-cluster-set-up/)