Jerry's WIKIJerry's WIKI
Overview
  • ๐Ÿž Web
  • ๐Ÿ“ Components
  • ๐Ÿ’ก Skills
  • ๐ŸŽฑ Specification
  • ๐Ÿ–ฅ Workflows
  • ๐Ÿ›  Tools
  • ๐ŸŒ๏ธ Linux
  • ๐Ÿ“ฆ Container
  • โ™จ๏ธ Language
Coffee
  • ็ฎ€ไฝ“ไธญๆ–‡
  • English
GitHub
Overview
  • ๐Ÿž Web
  • ๐Ÿ“ Components
  • ๐Ÿ’ก Skills
  • ๐ŸŽฑ Specification
  • ๐Ÿ–ฅ Workflows
  • ๐Ÿ›  Tools
  • ๐ŸŒ๏ธ Linux
  • ๐Ÿ“ฆ Container
  • โ™จ๏ธ Language
Coffee
  • ็ฎ€ไฝ“ไธญๆ–‡
  • English
GitHub
  • ๐Ÿณ Docker

    • Basic
    • CI/CD
    • Volume
    • Network
    • Commands
  • ๐ŸŽ› Docker compose
  • ๐Ÿ•ธ Docker swarm
  • ๐Ÿ™ K8s

Docker compose

Index

  • Container Shared Network
  • Multiple Containers Sharing Files and Directories

ใ€Conceptใ€‘

  • When multiple containers need to communicate or depend on each other, we can arrange them together according to certain rules. This is done by declaring them in a docker-compose.yml file.
  • The docker-compose.yml is based on version 3. For details on each option, refer to the: v3 configuration reference
  • Even for a single container, I prefer to write a docker-compose.yml file, as it provides a clearer view of what configurations are made within that container.

Container Shared Network

  • If containers need to communicate with each other, they must be on the same network configuration (special handling is required in Docker Swarm mode).
  • Multiple containers on the same network do not need to be orchestrated within a single docker-compose.yml.
  • Communication between containers can be done via IP:port. Alternatively, containers can communicate using ContainerName:port, but the network orchestration and configuration must be handled carefully to account for differences.

Here is an example using Redis master-slave (multiple Redis instances need to communicate to back up data):


master

version: "3.5"

services:
  redis-master:
    image: redis:latest
    container_name: redis-master
    restart: always
    ports:
      - "6371:6379"
    networks:
      - proxy
    privileged: true # ๆ‹ฅๆœ‰rootๆƒ้™,ๅฏไปฅๆŒ‚ๅœจๅœจๅ…ถไป–่ฎพๅค‡ไธŠ
    volumes:
      - $PWD/conf/redis.conf:/usr/local/etc/redis/redis.conf:rw
      - $PWD/data:/data:rw
    command: redis-server /usr/local/etc/redis/redis.conf

# ๅŠ ๅ…ฅๅทฒๆœ‰็ฝ‘็ปœ
networks:
  proxy:
    external: true

slave

version: "3.5"

services:
  redis-2:
    image: redis:latest
    container_name: redis-2
    restart: always
    ports:
      - "6372:6379"
    networks:
      - proxy
    volumes:
      - $PWD/conf2/redis.conf:/usr/local/etc/redis/redis.conf:rw
      - $PWD/data2:/data:rw
    command: redis-server /usr/local/etc/redis/redis.conf

  redis-3:
    image: redis:latest
    container_name: redis-3
    restart: always
    ports:
      - "6373:6379"
    networks:
      - proxy
    volumes:
      - $PWD/conf3/redis.conf:/usr/local/etc/redis/redis.conf:rw
      - $PWD/data3:/data:rw
    command: redis-server /usr/local/etc/redis/redis.conf

# ๅŠ ๅ…ฅๅทฒๆœ‰็ฝ‘็ปœ
networks:
  proxy:
    external: true

ใ€Warningใ€‘

Looking at the yml file alone doesn't reveal the communication setup; you need to check the corresponding configuration๏ผš

  • master-redis-config
  • slave1-redis-config
  • slave2-redis-config

Here, pay attention to the slaveof field in the Redis configuration for the slave. You can think about why the port is 6379 instead of 6371 ๐Ÿ˜Ž

Multiple Containers Sharing Files and Directories

  • When multiple containers need to share data, it is necessary to mount it to a shared volume, and then specify the volume mapping when orchestrating or starting the containers.
  • If the data in the shared volume is highly sensitive and important, it is best to perform regular backups.
  • If multiple containers need to share data, it is recommended to create a shared volume in advance and then specify the usage of the existing shared volume.

version: "3.5"
services:
  web:
    image: alpine
    volumes:
      - mydata:/data
  web1:
    image: alpine
    volumes:
      - mydata:/data
volumes:
  mydata:
  dbdata:

version: "3.5"
  services:
    web:
      image: alpine
      volumes:
        - mydata:/data
    web1:
      image: alpine
      volumes:
        - mydata:/data
volumes:
  mydata:
    external: true
Edit this page
Update At:
Contributor: ็”ฐๆœๅธ†
Prev
Commands
Next
Docker swarm