This is the 8th day of my participation in the More text Challenge. For details, see more text Challenge

  • 1.Docker Quick Start
  • 2.Docker Quickstart 2
  • 3.Docker Quickstart 3
  • 4.Docker Quick Start

Use the Docker Compose

Docker Compose is a tool that helps define and share multi-container applications. With Compose, you can create a YAML file to define the service and run or disassemble all the services with a single command.

Conclusion has the following advantages:

  1. All services can be run or disassembled with a single command
  2. It’s easy to get other people to contribute to your project
  3. Someone just needs to clone your source code, start it, and write a program

If you have Docker Desktop/Toolkit installed for Windows or Mac, you already have Docker Compose! If you are using a Linux machine, you will need to make the instructions installed.

View version:

Docker-compose version docker-compose version 1.29.2, build 5becea4c docker-py version: 5.0.0 3.9.0 OpenSSL Version: OpenSSL 1.1.1h 22 Sep 2020Copy the code

Create a file

Create a file called docker-compose. Yml

Define application

The previous example works like this:

docker run -dp 3000:3000 \ -w /app -v "$(pwd):/app" \ --network todo-app \ -e MYSQL_HOST=mysql \ -e MYSQL_USER=root \ -e  MYSQL_PASSWORD=secret \ -e MYSQL_DB=todos \ node:12-alpine \ sh -c "yarn install && yarn run dev"Copy the code

To:

version: "3.7"

services:
  app:
    image: node:12-alpine
    command: sh -c "yarn install && yarn run dev"
    ports:
      - 3000: 3000
    working_dir: /app
    volumes:
      - ./:/app
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: root
      MYSQL_PASSWORD: secret
      MYSQL_DB: todos
Copy the code

Defining MySQL Services

The previous example works like this:

docker run -d \
  --network todo-app --network-alias mysql \
  -v todo-mysql-data:/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD=secret \
  -e MYSQL_DATABASE=todos \
  mysql:5.7
Copy the code

To:

version: "3.7"

services:
  app:
    # The app service definition
  mysql:
    image: Mysql: 5.7
    volumes:
      - todo-mysql-data:/var/lib/mysql
    environment: 
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: todos

volumes:
  todo-mysql-data:
Copy the code

Complete yml

version: "3.7"

services:
  app:
    image: node:12-alpine
    command: sh -c "yarn install && yarn run dev"
    ports:
      - 3000: 3000
    working_dir: /app
    volumes:
      - ./:/app
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: root
      MYSQL_PASSWORD: secret
      MYSQL_DB: todos

  mysql:
    image: Mysql: 5.7
    volumes:
      - todo-mysql-data:/var/lib/mysql
    environment: 
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: todos

volumes:
  todo-mysql-data:
Copy the code

Run this file to start the application and mysql services, which by default will help us create on the same network

-d: runs in the background

docker-compose up -d Creating network "app_default" with the default driver Creating volume "app_todo-mysql-data" with default driver Creating app_app_1 ... done Creating app_mysql_1 ... The done mysql_1 | 2019-10-03 T03:07:16. 083639 z 0 mysqld [Note] : ready for connections. Mysql_1 | Version: '5.7.27 sockets: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL) app_1 | Connected to mysql db at host mysql app_1 | Listening on port 3000Copy the code

Note: Wait for the database before starting the application, Docker does not provide service choreography capabilities, such as K8S to do it

See above in Docker Dashboard

Tear it all down

The application groups seen above will be stopped and deleted except for volumes. You can specify the –volumes parameter. Deleting a volume on Docker Dashboard does not delete the volume

docker-compose down

docker-compose down --volumes
Copy the code

Note: Docker Desktop latest version of the sidebar with Volumes

Quick query of common commands

View image: docker image ls View container: docker ps Monitor logs: docker logs-f < container-id >