Cấu hình Docker Compose

Các mục cấu hình cấp cao

  • version: Chỉ định phiên bản của file cấu hình
  • services: Định nghĩa các dịch vụ cần chạy, bao gồm thông tin ảnh và cài đặt
  • networks: Cấu hình mạng dùng chung cho các dịch vụ
  • volumes: Định nghĩa các volume để lưu trữ dữ liệu

Ví dụ minh họa:

version: "3.8"
services:
  redis:
    image: redis:alpine
    ports:
      - "6379"
    networks:
      - frontend
    deploy:
      replicas: 2
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure

  db:
    image: postgres:9.4
    volumes:
      - db-data:/var/lib/postgresql/data
    networks:
      - backend

  result:
    image: nginx
    ports:
      - "5001:80"
    networks:
      - backend
    depends_on:
      - db
    deploy:
      replicas: 1
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure

  worker:
    image: nginx
    networks:
      - frontend
      - backend
    deploy:
      mode: replicated
      replicas: 1
      labels: [APP=VOTING]
      restart_policy:
        condition: on-failure
        delay: 10s
        max_attempts: 3
        window: 120s

networks:
  frontend:
  backend:
volumes:
  db-data:

Lệnh cấu hình trong SERVICES

1. container_name

Xác định tên cụ thể cho container

version: "3"
services:
  redis:
    image: redis:alpine
    container_name: redis_test

2. image

Chỉ định tên hoặc ID của ảnh Docker

version: "3"
services:
  redis:
    image: redis:alpine

3. build

Xác định đường dẫn tới thư mục chứa Dockerfile để xây dựng ảnh

version: '3'
services:
  webapp:
    build: ./dir

Có thể sử dụng thêm các tùy chọn như contextdockerfile:

version: '3'
services:
  webapp:
    build:
      context: ./dir
      dockerfile: Dockerfile-name

4. command

Ghi đè lệnh mặc định khi container khởi động

# Dưới dạng shell
command: bundle exec thin -p 3000
# Dưới dạng exec
command: [bundle, exec, thin, -p, 3000]

5. depends_on

Xác định thứ tự khởi động giữa các service

version: '3'
services:
  web:
    image: redis:alpine
    container_name: redis_test
    depends_on:
      - db

6. environment

Đặt biến môi trường

environment:
  RACK_ENV: development
  SHOW: 'true'
  SESSION_SECRET:

environment:
  - RACK_ENV=development
  - SHOW=true
  - SESSION_SECRET

7. expose

Khai báo cổng nội bộ mà không ánh xạ ra máy chủ

expose:
 - "3000"
 - "8000"

8. ports

Ánh xạ cổng giữa host và container

ports:
 - "3000"
 - "3000-3005"
 - "8000:8000"
 - "9090-9091:8080-8081"
 - "49100:22"
 - "127.0.0.1:8001:8001"
 - "127.0.0.1:5000-5010:5000-5010"
 - "6060:6060/udp"

9. extra_hosts

Thêm các bản ghi host vào /etc/hosts của container

extra_hosts:
 - "somehost:162.242.195.82"
 - "otherhost:50.31.209.229"

10. networks

Liên kết container với mạng đã định nghĩa

services:
  some-service:
    networks:
     - some-network
     - other-network
networks:
  some-network:
  other-network:

11. entrypoint

Xác định lệnh chạy đầu tiên khi container khởi động

12. user

Chạy ứng dụng với người dùng cụ thể

13. working_dir

Thiết lập thư mục làm việc bên trong container

14. restart

Chiến lược khởi động lại container sau khi dừng

restart: always

15. alias

Tên thay thế cho dịch vụ trong mạng

services:
  some-service:
    networks:
      some-network:
        aliases:
         - alias1
         - alias3
      other-network:
        aliases:
         - alias2

Lệnh cấu hình VOLUME

Định nghĩa đường dẫn mount cho volume

volumes:
 - /var/lib/mysql
 - cache/:/tmp/cache
 - ~/configs:/etc/configs/:ro

Mạng trong Docker Compose

1. Mạng mặc định

Khi không khai báo mạng, các container sẽ được thêm vào mạng app_default

version: '3'
services:
  web:
    image: nginx:latest
    container_name: web
    depends_on:
      - db
    ports:
      - "9090:80"
    links:
      - db
  db:
    image: mysql
    container_name: db

2. Tạo mạng tùy chỉnh

version: '3'
services:
  proxy:
    build: ./proxy
    networks:
      - front
  app:
    build: ./app
    networks:
      - front
      - back
  db:
    image: postgres
    networks:
      - back

networks:
  front:
    driver: custom-driver-1
  back:
    driver: custom-driver-2
    driver_opts:
      foo: "1"
      bar: "2"

3. Mạng mặc định tùy chỉnh

version: '2'
services:
  web:
    build: .
    ports:
      - "8000:8000"
  db:
    image: postgres

networks:
  default:
    driver: custom-driver-1

4. Sử dụng mạng sẵn có

networks:
  default:
    external:
      name: my-pre-existing-network

Lệnh Docker Compose phổ biến

1. docker-compose up

Khởi tạo và chạy ứng dụng từ file cấu hình

2. docker-compose stop

Dừng tất cả container nhưng không xóa chúng

3. docker-compose rm

Xóa container đã dừng

4. docker-compose restart

Khởi động lại container đã dừng

5. docker-compose ps

Xem trạng thái container đang chạy

6. docker-compose down

Dừng và xóa container cũng như mạng

Thử nghiệm: Chạy nhiều container Nginx

version: "3.8"
services:
  nginx1: 
    image: nginx:latest 
    container_name: nginx11
    ports:
      - "8083:80"
    expose:
      - "80"
  nginx2: 
    image: nginx:latest 
    container_name: nginx2
    ports:
      - "8084:80"
    expose:
      - "80"

Xây dựng bằng Dockerfile

Nội dung Dockerfile:

ARG VERSION=latest
FROM nginx:${VERSION}
EXPOSE 80

Nội dung docker-compose.yml:

version: "3.8"
services:
    webapp: 
      container_name: mydockerfile
      ports:
        - "8091:80"
      build: 
        context: /home/admin1/yyx/
        dockerfile: dockerfile

Thẻ: docker Compose yaml dockerfile Volume

Đăng vào ngày 24 tháng 6 lúc 09:14