Docker-compose project is the official open source project of Docker, which is responsible for the rapid choreography of Docker container clusters. The default docker-compose configuration file is docker-comemage. yml. This article will explain the configuration in the file and provide common container configuration, such as: Mysql, Redis, MongoDB, rabbitMQ and other containers configuration.

Docker – compose the installation

  • Method 1: Install using the PIP

If you have a Python PIP environment on your system, you can install it directly with the following command.

pip install docker-compose
# or
pip3 install docker-compose
Copy the code
  • Method 2: Download the installation file

    GitHub:github.com/docker/comp…

    The curl -l https://github.com/docker/compose/releases/download/1.28.0/docker-compose- ` ` uname - s - ` uname -m ` -o/usr /local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose
    Copy the code

    In Linux, the uname command displays information about the PC and operating system. Uname -s Displays the operating system name. Uname -m Displays the computer type.

After the installation, check whether the installation is successful

docker-compose --version
Copy the code

Version Description The installation is successful. If the installation encountered problems can leave a message to contact me.

Docker-comemage. yml file description

Since there are many commands involved in configuration, only the common ones are mentioned here. For details, you can check the official website: official document address

version: '3'

services:
  # mysql service
  mysql:
    # Mirror used --> Mirror name: version
    image: Mysql: 8.0.16  
    # container name
    container_name: mysql8
    # hostname
    hostname: mysql8
    # Environment variable (multiple) --> Environment variable name: the value of the variable
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      TZ: Asia/Shanghai
    # Expose mapped ports (multiple) --> Host ports: in-container ports
    ports:
      - 3306: 3306
    # Resolve container dependencies, startup priorities. In the following example, other services are started before mysql is started
    depends_on:
      - Other services
    You can link to other services
    links:
      - Other services
    # Select a custom netgroup
    networks:
      - custom_net
    # directory mount (multiple) --> Host path: container path
    volumes:
      - ./data:/var/lib/mysql
    https://blog.csdn.net/qq_34924407/article/details/80802634 # container restart strategy
    # no, the default policy, does not restart the container when it exits
    # on-failure, restart container only if container exits abnormally (exit status non-zero)
    # on-failure:3, restart the container up to 3 times if the container exits abnormally
    Always, always restart the container when it exits
    # unless-stopped, the container is always restarted when the container exits, but does not consider containers that were stopped when the Docker daemon started
    restart: on-failure:3
    # execute command
    command: Commands to be executed
    
# Customize network group configuration
networks:
  custom_net:
    external:
      name: app_net
Copy the code

Common configuration instructions are the above, others can see the official website documents.

Docker-compose common command

Docker-compose up docker-compose up -d docker-compose up docker-compose up -d docker-compose up Docker-compose. Yml up -d # specifies the docker-compose template to stop and delete containers, networks, volumes, and images. Docker-compose logs docker-compose pull # check configuration dokcer-compose config -q # Docker-compose restart docker-compose start docker-compose stop docker-compose stopCopy the code

Common container configuration files

Mysql

version: '3'

services:
  # mysql service
  mysql:
    image: Mysql: 8.0.16
    # container name
    container_name: mysql8
    # hostname
    hostname: mysql8
    # Environment variables
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      TZ: Asia/Shanghai
    # Mapping port
    ports:
      - 3306: 3306
    The MySQL data directory is mounted in the
    volumes:
      - ./data:/var/lib/mysql
    https://blog.csdn.net/qq_34924407/article/details/80802634 # container restart strategy
    restart: on-failure:3
Copy the code

Redis

version: '3'

services:
  # redis service
  redis:
    image: redis:latest
    container_name: redis
    hostname: redis
    # execute command
    command: redis-server /usr/local/etc/redis/redis.conf --appendonly yes
    volumes:
      # Mount Redis data
      - ./data:/data
      Here you need to prepare a redis.conf file in the same directory
      - ./redis.conf:/usr/local/etc/redis/redis.conf
    ports:
      - 6379: 6379
    # Environment variables
    environment:
      TZ: Asia/Shanghai
    Container restart policy
    restart: on-failure:3
Copy the code

MangoDB

version: '3'

services:
  mongo:
    image: mongo:latest
    container_name: mongodb
    hostname: mongodb
    restart: on-failure:3
    ports:
      - 27017: 27017
    volumes:
      - ./data/db:/data/db
      - ./log:/data/log
    environment:
      MONGO_INITDB_ROOT_USERNAME: codelong
      MONGO_INITDB_ROOT_PASSWORD: codelong
      TZ: Asia/Shanghai
Copy the code

RabbitMQ

version: '3'

services:
  # the rabbitmq service
  rabbitmq:
    # mirror name
    image: rabbitmq:latest
    # container name
    container_name: rabbitmq
    hostname: rabbitmq
    # port mapping
    ports:
      - 5672: 5672
      - 15672: 15672
      - 4369: 4369
      - 25672: 25672
    # mount
    volumes:
      - ./data:/var/lib/rabbitmq
      - ./log:/var/log/rabbitmq/log
    # Environment variables
    environment:
      - RABBITMQ_DEFAULT_VHOST=codelong_host
      - RABBITMQ_DEFAULT_USER=codelong
      - RABBITMQ_DEFAULT_PASS=codelong
    logging:
      driver: "json-file"
      options:
        max-size: "200k"
        max-file: "10"
    Container restart policy
    restart: on-failure:3
Copy the code

More container configurations are uploaded to GitHub, which is not listed here.