Skip to content

Latest commit

 

History

History
211 lines (178 loc) · 6.06 KB

2-docker-compose.md

File metadata and controls

211 lines (178 loc) · 6.06 KB

Compose Logo

NPM Version


Index


Getting Started

  • Allows you to start multiple containers at the same time
  • docker-compose.yml: required file that will serve as a guide to run the containers correctly
  • used to describe different containers
version: '3.7'

services:
  my-app:
    image: custom-node
    container_name: my-app
    build: ./app
    volumes:
      - './app:/app'
      - '/app/node_modules'
    ports:
      - '8080:8080'

  backend:
    image: backend
    container_name: my-server
    build:
      context: ./backend
      target: dev
    networks:
      - backend
    env_file:
      - .env
    volumes:
      - './backend:/server'
      - '/server/node_modules'
    ports:
      - '3080:3080'

Example of docker-compose.yml file

Docker Compose Reference

build


  • Specifies the build configuration for creating container image from source

command


  • Overrides the default command declared by the container images
  • Can also be a list, similar to Dockerfile
command: bundle exec thin -p 3000

command: [ "bundle", "exec", "thin", "-p", "3000" ]

depends-on


  • Expresses startup and shutdown dependencies between services
  • Compose implementations MUST create services in dependency order
  • Compose implementations MUST remove services in dependency order
services:
  web:
    build: .
    depends_on:
      - db
      - redis
  redis:
    image: redis
  db:
    image: postgres

Here, db and redis are created before web. Then web, is removed before db and redis

environment


  • Defines environment variables set in the container
  • Can use either array or map
  • Boolean should be enclosed in QUOTE
  • Array syntax :
environment:
  - RACK_ENV=development
  - SHOW=true
  - USER_INPUT

expose


  • Defines ports that Compose implementation MUST expose from container
  • Ports must be accessible to linked services and should not be published to the host machine
expose:
  - "3000"
  - "8000"

networks


  • Defines the networks that service containers are attached to
services:
  some-service:
    networks:
      - some-network
      - other-network

ports


  • Expose container ports
  • Port mapping MUST NOT be used with network_mode: host
ports:
  - "3000"
  - "3000-3005"
  - "8000:8000"
  - "9090-9091:8080-8081"
  - "49100:22"

restart


  • Define the policy that platform will apply on container termination
    • no: The default restart policy. Does not restart a container under any circumstances.
    • always: The policy always restarts the container until its removal.
    • on-failure: The policy restarts a container if the exit code indicates an error.
    • unless-stopped: The policy restarts a container irrespective of the exit code but will stop restarting when the service is stopped or removed.
restart: always

volume


  • Defines mount hosts paths or named volumes that MUST be accessible by service containers
  • If the mount is a host path and only used by a single service, it MAY be declared as part of the service definition instead of the top-level volumes key.
  • To reuse a volume across multiple services, a named volume MUST be declared in the top-level volumes key.

This example shows a named volume (db-data) being used by the backend service, and a bind mount defined for a single service

services:
  backend:
    image: awesome/backend
    volumes:
      - type: volume
        source: db-data
        target: /data
        volume:
          nocopy: true
      - type: bind
        source: /var/run/postgres/postgres.sock
        target: /var/run/postgres/postgres.sock

volumes:
  db-data:
  • There is some target :
    • type: the mount type volume, bind, tmpfs or npipe
    • source: the source of the mount, a path on the host for a bind mount, or the name of a volume defined in the top-level volumes key. Not applicable for a tmpfs mount.
    • target: the path in the container where the volume is mounted
    • read_only: flag to set the volume as read-only

EVERYTHING IS HERE

Sources