Docker Compose

Docker Compose uses a docker-compose.yml file to link several containers and achieve fast deployment of a porject with multiple containers.

Example of docker-compose.yml

version: "3.8" # version of yml syntax

services:
    containerA:
        image: mysql
        container_name: mysql2
        ports:
            - "3306:3306"
        environment:
            TZ: Asia/Shanghai
            MYSQL_ROOT_PASSWORD: 123
        volumes:
            - "./mysql/conf:/etc/mysql/conf.d"
        networks:
            - myNetwork
    containerB:
        build:
            context: .
            dockerfile: Dockerfile
        container_name: hmall
        ports:
            - "8080:8080"
        depends_on:
            - mysql2
        networks:
            - myNetwork
    # ...
networks:
    myNetwork:  # the tag of the network in this yml file
        name: my-network    # the real name of the network created

docker compose Commands

docker compose [OPTIONS] [COMMAND]

options:

  • -f: specify the path of the compose file
  • -p: specify the project name
  • -d: run in background

commands:

  • up: create and launch all containers
  • down: stop and remove all containers and network
  • ps: list all launched containers
  • logs: check a container's logs
  • stop: stop a container
  • start: start a container
  • restart: restart a container
  • top: check running containers
  • exec: execute commands in a specified container

To get help, use --help option