Mẫu file cơ bản

# docker-compose.yml
version: '2'

services:
  web:
    build: .
    # build from Dockerfile
    context: ./Path
    dockerfile: Dockerfile
    ports:
     - "5000:5000"
    volumes:
     - .:/code
  redis:
    image: redis

Commands

docker-compose start
docker-compose stop
docker-compose pause
docker-compose unpause
docker-compose ps
docker-compose up
docker-compose down

Reference

Building

web:
  # build từ Dockerfile
  build: .
  args:     # Add build arguments
    APP_HOME: app
  # build từ custom Dockerfile
  build:
    context: ./dir
    dockerfile: Dockerfile.dev
  # build từ image
  image: ubuntu
  image: ubuntu:14.04
  image: tutum/influxdb
  image: example-registry:4000/postgresql
  image: a4bc65fd

Ports

  ports:
    - "3000"
    - "8000:80"  # host:container
  # expose ports đến linked services (not to host)
  expose: ["3000"]

Commands

  # command để execute
  command: bundle exec thin -p 3000
  command: [bundle, exec, thin, -p, 3000]
  # ghi đè entrypoint
  entrypoint: /app/start.sh
  entrypoint: [php, -d, vendor/bin/phpunit]

Environment variables

  # environment vars
  environment:
    RACK_ENV: development
  environment:
    - RACK_ENV=development
  # environment vars từ file
  env_file: .env
  env_file: [.env, .development.env]

Dependencies

  # làm cho "db" service có sẵn như là hostname "database"
  links:
    - db:database
    - redis
  # đảm bảo rằng "db" start trước
  depends_on:
    - db

Other options

  # Sử dụng file yaml khác kèm theo
  extends:
    file: common.yml  # optional
    service: webapp
  volumes:
    - /var/lib/mysql
    - ./_data:/var/lib/mysql

Các tính năng nâng cao

Labels

services:
  web:
    labels:
      com.example.description: "Accounting web app"

DNS servers

services:
  web:
    dns: 8.8.8.8
    dns:
      - 8.8.8.8
      - 8.8.4.4

Devices

services:
  web:
    devices:
    - "/dev/ttyUSB0:/dev/ttyUSB0"
services:
  web:
    external_links:
      - redis_1
      - project_db_1:mysql

Hosts

services:
  web:
    extra_hosts:
      - "somehost:192.168.1.100"

Network

# tạo một network với tên là "frontend"
networks:
  frontend:

External network

# Join vào network đã tồn tại
networks:
  default:
    external:
      name: frontend

Volume

# Mount host paths hoặc volume đã được đặt tên, cụ thể như một tùy chọn nhỏ trong service
  db:
    image: postgres:latest
    volumes:
      - "/var/run/postgres/postgres.sock:/var/run/postgres/postgres.sock"
      - "dbdata:/var/lib/postgresql/data"

volumes:
  dbdata:
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments