Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Docker-compose networks

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you Docker-compose networks example analysis, I believe most people do not know how, so share this article for your reference, I hope you have a lot of harvest after reading this article, let us go to understand it together!

(System: Centos 7.4, docker version: 18.03.1-ce, docker-compose version 1.18.0)

cat docker-compose.yml version: '3'services: test1: image: busybox:latest #mirror as busybox entrypoint: #Execute the top command after the container starts, so that the container cannot exit immediately - top networks: backend: #Use the specified network backend, and set the network alias to test1, aliases: #After setting the network alias, you can ping test1 in other containers to access this container - test1 test2: image: busybox:latest entrypoint: - top networks: backend: aliases: - test2networks: backend:

start

docker-compose up -ddocker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES4d05ceb2088d busybox:latest "top" 5 seconds ago Up 4 seconds ibaboss_test2_1f4ccafa24664 busybox:latest "top" 5 seconds ago Up 4 seconds ibaboss_test1_1docker exec-it 4d05ceb2088d/bin/sh/#ping test1PING test1 (www.example.com): 172.19.0.2 (172.19.0.2): 56 data bytes64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.045 ms64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.056 ms64 bytes from 172.19.0.2: seq = 2 ttl = 64 time = 0.061 ms #Communication in the network can be done by container name or alias of the network

The container name format for Compose is:

Although you can customize the project name and service name, if you want complete control over the naming of containers, you can use this tag to specify:

container_name: appcat docker-compose_v1.yml version: '3'services: test1: image: busybox:latest entrypoint: - top container_name: test1 networks: - backend test2: image: busybox:latest entrypoint: - top container_name: test2 networks: - backendnetworks: backend:

start

docker-compose -f docker-compose_v1.yml up -ddocker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES132859fc77c2 busybox:latest "top" About a minute ago Up About a minute test2cd0a78dc9bd4 busybox:latest "top" About a minute ago Up About a minute test1docker exec -it 132859fc77c2 ping test1PING test1 (172.19.0.2): 56 data bytes64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.070 ms64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.068 ms64 bytes from 172.19.0.2: seq=2 ttl=64 time=0.059 ms

Additional:

docker compose multi-container sharing network problem

A few days ago, I planned to use docker compose to write a multi-container sharing a container network. The result was always an error. Today, I tried it again and found that it was caused by the port binding problem. The port can only be bound to the container that generates the network card. Binding to other windows will result in an error. The following code mysql service shares the network card of nginx. When port: 3306 is used in mysql, it will fail to run. Move 3306 to nginx to start successfully.

version:'3.3'services:nginx:image:"lnp_php"#container_name: "lnmp_nginx" ports: -"80:80"-"443:443"-"3306:3306" expose: -"3306" volumes: -/home/www/php:/home/www: rw #depends_on: #-mysql #links: #-mysql mysql: image: "mysql"#container_name: "lnmp_mysql"#ports: #-"3306:3306"#expose:#-"3306" volumes: -/home/docker/conf/mysql_w:/etc/mysql: rwenvironment: -MYSQL_ROOT_PASSWORD = 123456depends_on: -nginxnetwork_mode: "service: nginx" The above is "Docker-compose networks sample analysis" All the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report