Some basic knowledge of docker-compose - the road to dream building

1. Duplicates

cat replicas_test.yml

version: '3'
services:
  replicas_test:
    image: centos:7.9
    restart: always
    command: ["sh","-c","sleep 36000"]
    deploy:
      replicas: 2
    healthcheck:
      test: ["CMD-SHELL", "hostname"]
      interval: 10s
      timeout: 5s
      retries: 3

Control the number of created service containers through configuration  deploy.replicas , but not all scenarios are applicable. For example, some components of Hadoop are not applicable. For example, when it is required to set the host name and container name, it is not suitable to adjust the number of containers through this parameter. .

2. Resource isolation

cat resources_test.yml

version: '3'
services:
  resources_test:
    image: centos:7.9
    restart: always
    command: ["sh","-c","sleep 36000"]
    deploy:
      replicas: 2
      resources:
        # 容器资源申请的最大值,容器最多能适用这么多资源
        limits:
          cpus: '1'
          memory: 100M
        # 所需资源的最小值,跟k8s里的requests一样,就是运行容器的最小值
        reservations:
          cpus: '0.5'
          memory: 50M
    healthcheck:
      test: ["CMD-SHELL", "hostname"]
      interval: 10s
      timeout: 5s
      retries: 3
# 检查验证

docker stats

3. Network network

Network is a very important knowledge point in the container , so here we focus on examples to see if different docker-compose projects are accessed by name. By default, each docker-compose is a project (different directories , multiple compose in the same directory belong to a project), each project will generate a network by default. Note that by default only names within the same network can reach each other. How to access by name in different projects, and then explain with an example

cat test1/test1.yml

version: '3'
services:
  test1:
    image: centos:7.9
    container_name: c_test1
    hostname: h_test1
    restart: always
    command: ["sh","-c","sleep 36000"]
    healthcheck:
      test: ["CMD-SHELL", "hostname"]
      interval: 10s
      timeout: 5s
      retries: 3

cat test2/test2.yml

version: '3'
services:
  test2:
    image: centos:7.9
    container_name: c_test2
    hostname: h_test2
    restart: always
    command: ["sh","-c","sleep 36000"]
    healthcheck:
      test: ["CMD-SHELL", "hostname"]
      interval: 10s
      timeout: 5s
      retries: 3
docker-compose -f test1/test1.yml up -d
docker-compose -f test2/test2.yml up -d

# 查看network,会生成两个network,如果两个yaml文件在同一个目录下,只会生成一个,它们也就属于同一个network下,是可以通过名称相互访问的。这里是在不同的目录下,就会生成两个network,默认情况下,不同的network是隔离的,不能通过名称访问的。yaml文件所在的目录名就是项目名称。这个项目名称是可以通过参数指定的,下面会细讲。
docker network ls

# 互ping
docker exec -it c_test1 ping c_test2
docker exec -it c_test1 ping h_test2

docker exec -it c_test2 ping c_test1
docker exec -it c_test2 ping h_test1

# 卸载
docker-compose -f test1/test1.yml down
docker-compose -f test2/test2.yml down

 Next, we add the network and then test and verify

Create a new network in  the definition, and refer to the network created by test1 test1/network_test1.yml below , then these two projects are in the same network, pay attention to the order of executiontest2/network_test2.yml

version: '3'
services:
  network_test1:
    image: centos:7.9
    container_name: c_network_test1
    hostname: h_network_test1
    restart: always
    command: ["sh","-c","sleep 36000"]
    # 使用network
    networks:
      - test1_network
    healthcheck:
      test: ["CMD-SHELL", "hostname"]
      interval: 10s
      timeout: 5s
      retries: 3

# 定义创建新网络
networks:
  test1_network:
    driver: bridge
version: '3'
services:
  network_test2:
    image: centos:7.9
    container_name: c_network_test2
    hostname: h_network_test2
    restart: always
    networks:
      - test1_network
    command: ["sh","-c","sleep 36000"]
    healthcheck:
      test: ["CMD-SHELL", "hostname"]
      interval: 10s
      timeout: 5s
      retries: 3

# 引用test1的网络
networks:
  # 项目名_网络名,可以通过docker network ls查看network名称
  test1_test1_network:
    external: true
docker-compose -f test1/network_test1.yml up -d
docker-compose -f test2/network_test2.yml up -d

# 查看网络
docker network ls

# 互ping
docker exec -it c_network_test1 ping -c3 c_network_test2 
docker exec -it c_network_test1 ping -c3 h_network_test2
docker exec -it c_network_test2 ping -c3 c_network_test1
docker exec -it c_network_test2 ping -c3 h_network_test1

# 卸载,注意顺序,要先卸载应用方,要不然network被应用了是删除不了的
docker-compose -f test2/network_test2.yml down
docker-compose -f test1/network_test1.yml down

4. Items

 The default project name is the name of the directory where the current yml file is located. The network name generated when explaining the network above will also be the first project name, but the project name can be customized. The example is as follows:

 

# test.yaml
version: '3'
services:
  test:
    image: centos:7.9
    restart: always
    command: ["sh","-c","sleep 36000"]
    healthcheck:
      test: ["CMD-SHELL", "hostname"]
      interval: 10s
      timeout: 5s
      retries: 3

# 先不加参数
docker-compose -f test.yaml up -d
# 查看网络
docker network ls

# 使用参数自定义项目名称,-p, --project-name,有四种写法

docker-compose -p=p001 -f test.yaml up -d
docker-compose -p p002 -f test.yaml up -d

docker-compose --project-name=p003 -f test.yaml up -d
docker-compose --project-name p004 -f test.yaml up -d

# 查看网络
docker network ls

# 查看所有项目
docker-compose ls

Guess you like

Origin blog.csdn.net/qq_34777982/article/details/131043470