A question about networking

Hello,
I networked a few containers that were in two YAML files:
First Container:

version: '3.9'
networks:
  Container1:
    driver: bridge
    name: My_Net
...

Second Container:

version: '3.9'
networks:
  Container2:
    external: true
    name: My_Net
...

Now I want to add another YAML file to this network. Can the configuration of this third container be something like the following?

version: '3.9'
networks:
  Container3:
    external: true
    name: My_Net
...

Cheers.

Hello.

I think it will work well if you set it up on a network called My_Net that is already created.

And since we use the same network, I think container 2 and container 3 will be well communicated.

Did I not figure out the problem?
Please reply.

Best Regards.
limsangwoon

1 Like

Hello,
Thank you so much for your reply.
So, I have to use external: true for the third container. I want all three containers to be able to communicate with each other.

1 Like

Hello,
2 containers with each different network area have different bands, so communication is impossible.

But you can add two networks to one container as shown in the picture. How about communicating this way?

The command used this and if thereโ€™s any other way Iโ€™d like to share it
docker network connect My_Net <container name>

Best Regards
limsangwoons

1 Like

Hello,
Thanks again.
But with the above configuration, containers can ping each other. Do I still need the docker network connect My_Net <container name> command?

hello

Then, I think I need more detailed file information, so can you deliver a file that runs even the container?

Best Regards
limsangwoons

Hi,
Thanks again.
Sure.
YAML file 1:

version: '3.9'
networks:
  NginxPHP:
    driver: bridge
    name: My_Net
services:
  web:
    image: nginx:latest
    container_name: Nginx
    ports:
      - '8080:80'
    volumes:
#      - ./default.conf:/etc/nginx/conf.d/default.conf
      - /var/www/html:/usr/share/nginx/html
    networks:
      - NginxPHP
    depends_on:
       - php-fpm
  php-fpm:
    image: php:8-fpm
    container_name: PHP
    volumes:
       - /var/www/html:/usr/share/nginx/html/
       - /var/www/html:/usr/local/apache2/htdocs/
    networks:
      - NginxPHP

YAML file 2:

version: '3.9'
networks:
  Apache:
    external: true
    name: My_Net
services:
    httpd:
        ports:
            - '80:80'
        volumes:
            - /var/www/html:/usr/local/apache2/htdocs/
#            - ./httpd.conf:/usr/local/apache2/conf/httpd.conf
        image: 'httpd:latest'
        container_name: Apache
        networks:
            - Apache

As you can see, all 3 containers can ping each other.

Hello,
When containers ping each other, it means that the connection between them is established. Is it right? Is it necessary to docker network connect My_Net <container name> command?

Cheers.

Hello.

Iโ€™m sorry. I made a mistake.

After all, all three containers use the same my_net, so itโ€™s right to communicate well with each other.

You can forget the command I gave you.

Best Regards,
limsangwoon

1 Like

Hi,
Thanks again.
I remove the networks section from the YAML files and use the following commands instead:

# docker network create My_Net
# docker network connect My_Net Nginx
# docker network connect My_Net PHP
# docker network connect My_Net Apache

Is it the same thing?

be right, be correct

1 Like