Detailed examples of Docker-compose networks

Detailed examples of Docker-compose networks

Today I experimented with the network settings under Docker and recorded the process so as not to forget it later.

(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 # The image is busybox
  entrypoint: #Execute the top command after the container is started 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 the container - test1

 test2:
  image: busybox:latest
  entrypoint:
   - top
  networks:
   backend:
    aliases:
     -test2

networks:
 backend:

start up

docker-compose up -d

docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4d05ceb2088d busybox:latest "top" 5 seconds ago Up 4 seconds ibaboss_test2_1
f4ccafa24664 busybox:latest "top" 5 seconds ago Up 4 seconds ibaboss_test1_1

docker exec -it 4d05ceb2088d /bin/sh
/# ping test1
PING test1 (172.19.0.2): 56 data bytes
64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.061 ms
64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.062 ms

ping ibaboss_test1_1
PING ibaboss_test1_1 (172.19.0.2): 56 data bytes
64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.045 ms
64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.056 ms
64 bytes from 172.19.0.2: seq=2 ttl=64 time=0.061 ms

# In the network, you can communicate through the container name or the network alias

The format of the Compose container name is: <project name><service name><serial number>

Although you can customize the project name and service name, if you want to have full control over the naming of the container, you can use this tag to specify it:

container_name: app
cat 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:
   - backend

networks:
 backend:

start up

docker-compose -f docker-compose_v1.yml up -d

docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
132859fc77c2 busybox:latest "top" About a minute ago Up About a minute test2
cd0a78dc9bd4 busybox:latest "top" About a minute ago Up About a minute test1

docker exec -it 132859fc77c2 ping test1
PING test1 (172.19.0.2): 56 data bytes
64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.070 ms
64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.068 ms
64 bytes from 172.19.0.2: seq=2 ttl=64 time=0.059 ms

Replenish:

Docker compose multi-container shared network problem

A few days ago, I planned to use docker compose to write multiple containers to share a container network, but I always got 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. If it is bound to other windows, an error will be reported. In the following code, the mysql service shares the nginx network card. When port:3306 is used in mysql, it will fail to run. Move 3306 to nginx and start it 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:rw
environment:
-MYSQL_ROOT_PASSWORD=123456
depends_on:
- nginx
network_mode: "service:nginx"

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Docker-compose detailed explanation and sample code
  • Detailed steps for installing and setting up Docker-compose
  • Detailed examples of using Docker-Compose
  • Ubuntu 15.10 installation docker and docker-compose tutorial
  • Use docker-compose to build AspNetCore development environment
  • A brief discussion on docker-compose network settings
  • A brief analysis of the problem of mysql being inaccessible when deployed with docker-compose
  • Detailed explanation of achieving high availability of eureka through docker and docker-compose
  • How to deploy gitlab using Docker-compose
  • Detailed explanation of software configuration using docker-compose in linux

<<:  Bootstrap FileInput implements image upload function

>>:  Summary of MySQL injection bypass filtering techniques

Recommend

How to add file prefixes in batches in Linux

You need to add "gt_" in front of the f...

Detailed analysis of the difference between Ref and Reactive in Vue3.0

Table of contents Ref and Reactive Ref Reactive T...

Specific steps for Vue browser to return monitoring

Preface When sharing a page, you hope to click th...

JavaScript css3 to implement simple video barrage function

This article attempts to write a demo to simulate...

Solution to the problem of failure to insert emoji expressions into MySQL

Preface I always thought that UTF-8 was a univers...

How to reset the root password in CentOS7

There are various environmental and configuration...

Summary of pitfalls in virtualbox centos7 nat+host-only networking

Table of contents 1. Problem Background 2. What a...

How to install MySQL 5.7 from source code in CentOS 7 environment

This article describes how to install MySQL 5.7 f...

10 reasons why Linux is becoming more and more popular

Linux has been loved by more and more users. Why ...

How to view and clean up Docker container logs (tested and effective)

1. Problem The docker container logs caused the h...

Linux uses lsof command to check file opening status

Preface We all know that in Linux, "everythi...

How to use docker to deploy dubbo project

1. First, use springboot to build a simple dubbo ...