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

Detailed explanation of the life cycle of Angular components (Part 2)

Table of contents 1. View hook 1. Things to note ...

Simple Implementation of HTML to Create Personal Resume

Resume Code: XML/HTML CodeCopy content to clipboa...

How to create scheduled tasks using crond tool in Linux

Preface Crond is a scheduled execution tool under...

A permanent solution to MYSQL's inability to recognize Chinese

In most cases, MySQL does not support Chinese whe...

iframe src assignment problem (server side)

I encountered this problem today. I reassigned the...

Detailed explanation of command to view log files in Linux environment

Table of contents Preface 1. cat command: 2. more...

Detailed introduction to CSS font, text, and list properties

1. Font properties color, specifies the color of ...

mysql5.7.22 download process diagram

1. Go to the official website www.mysql.com and s...

How to assign a public IP address to an instance in Linux

describe When calling this interface, you need to...

Detailed explanation of how to use eslint in vue

Table of contents 1. Description 2. Download rela...

The implementation process of long pressing to identify QR code in WeChat applet

Preface We all know that the QR codes in official...

MySQL master-slave replication delay causes and solutions

Table of contents A brief overview of the replica...