1

My goal was, that Jenkins container will "talk" with Ansible container in order to create Jenkins file for Jenkins pipeline.

I was expected that those two container will "join" to bridge network and get 2 IP addresses of the same network id, but instead additional two networks were created, and each container got IP of different Network id.

Also, expected that those two container will have access to the internet.

So, In my Windows 10 I'm running Docker Desktop, I have 2 docker-compose.yml files,
One, for Jenkins container:

version: '3.7' services: jenkins: image: jenkins/jenkins:lts privileged: true user: root ports: - 8080:8080 - 50000:50000 container_name: jenkins 

Another, for Ansible container:

version: '2' services: ansible: container_name: ansible hostname: ansible image: ansible volumes: - ~/product/ansible:/ansible working_dir: /ansible build: context: . dockerfile: Dockerfile dns: - 200.20.200.20 

I ran the following command for each docker-compose.yml file, so I had two separate machines:

docker-compose up --build 

I inspected my container's network details and found out that each container got a different Network ID, see below:

PS > docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ansible 172.18.0.2 PS > docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' jenkins 172.19.0.2 

check my docker networks section, revealed that 2 more networks were created, besides bridge, host and none:

PS > docker network ls NETWORK ID NAME DRIVER SCOPE 8cefaed24885 ansible_default bridge local 44bedcd1622d bridge bridge local 61e1c7f7051e host host local b5a7a7a424a4 jenkins_default bridge local 4e5d6c77cb5a none null local 

Of course, inspecting bridge network shows that container key is empty:

{ "Name": "bridge", "Id": "44bedcd1622d820ce4e29a5cd545f244ba2d303102f1956fe76069a63e7c220e", "Created": "2021-08-25T13:13:57.6577149Z", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": null, "Config": [ { "Subnet": "172.17.0.0/16", "Gateway": "172.17.0.1" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": {}, "Options": { "com.docker.network.bridge.default_bridge": "true", "com.docker.network.bridge.enable_icc": "true", "com.docker.network.bridge.enable_ip_masquerade": "true", "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0", "com.docker.network.bridge.name": "docker0", "com.docker.network.driver.mtu": "1500" }, "Labels": {}, "CreatedTime": 1629897237657 } 

My question are:
a) Why two more docker networks were created when I ran docker-compose up --build command?
b) How can I make those containers working with bridge network and get IPs of the same network id (the bridge network id 172.17.x.x) in order that they'll talk each other?

1 Answer 1

0

The solution for connecting multiple docker-compose instances is using external networks.

First create a new network with the command:

docker network create my-network 

Then add to both docker-compose.yml the following:

networks: my_network: external: true 

And in the network section of the services:

(...) networks: - my_network (...) 

The Jenkins docker-compose would become:

version: '3.7' networks: my_network: external: true services: jenkins: image: jenkins/jenkins:lts privileged: true user: root networks: - my_network ports: - 8080:8080 - 50000:50000 container_name: jenkins 

Follow the same idea for the other docker-compose.yml.

3
  • thanks. Please clarify: a) Is there a way to use the default bridge network? b) Is the syntax for docker-compose version: 3.7 equal to version 2.0? Commented Sep 3, 2021 at 10:45
  • How do I include static IP in you solution? Commented Sep 3, 2021 at 11:32
  • a) The default bridge network is only available to the services in one particular docker-compose.yml. If you put all services in that file they will all share the same network. b) Each version of docker-compose yaml has its specific syntax, normally complementary, but there are some exceptions. The documentation regard this topic is very extensive in docker site (docs.docker.com/compose/compose-file/compose-versioning). - It is not common to use a static IP in docker. You can resolve an ip address from DNS. One example: from jenkins: the ansible container is ansible.my_network. Commented Sep 6, 2021 at 8:00

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.