Score:1

docker - create two container got two different network id instead of using default bridge network

uz flag

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?

Score:0
ro flag

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.

uz flag
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```?
uz flag
How do I include static IP in you solution?
João Alves avatar
ro flag
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 (https://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`.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.