The introduction and use of docker compose

Keywords: Linux Docker Nginx network CentOS

docker compose: a tool for docker automatic arrangement

Preface

In the last two articles, I played with Dockerfile again, mainly building various service images based on Dockerfile. This paper will introduce and demonstrate the concept and use of docker compose, which is an automatic choreographer tool of docker.

What is docker compose?

Before we used Dockerfile to build image, we need to use docker build, docker run and other commands to operate {create, start, stop and so on} container after writing Dockerfile and other necessary files. However, in the microservice architecture, an application system generally contains several servers, and each microservice usually deploys multiple instances. If each service needs to be started and stopped manually, the efficiency is too low, and the maintenance is also very inconvenient.

Therefore, there is docker compose, which can easily and efficiently manage containers. It is an application tool for defining and running multi container dockers.

Let's use the following docker compose tool in combination with the actual example. Then we will introduce the fields commonly used in the layout file and the relevant commands of docker compose.

Building nginx container service based on docker compose tool

First of all, we need to create a directory, which needs to contain a nginx directory for building Dockerfile, the home page test directory and the docker-compose.yml file

Therefore, on the one hand, we need to install the docker installation environment and the docker compose tool;

Moreover, it is still necessary to write the Dockerfile and running script of nginx service, and test and verify them in combination with the specified page directory;

Next, write the docker-compose.yml file in the format of YML, and execute the corresponding command.

Here is the directory structure of this case

[root@localhost opt]# tree compose_nginx/
compose_nginx/
├── docker-compose.yml
├── nginx
│?? ├── Dockerfile
│?? └── nginx.sh
└── wwwroot
    └── index.html

2 directories, 4 files

The Dockerfile and its corresponding files do not need to be changed. What we need to do is to write a homepage

[root@localhost opt]# cat compose_nginx/wwwroot/index.html 
<h1>this is test web</h1>

Then write the docke-compose.yml file

[root@localhost opt]# cat compose_nginx/docker-compose.yml 
version: '3'
services:
  nginx:
    hostname: nginx
    build:
      context: ./nginx
      dockerfile: Dockerfile
    ports:
      - 1216:80
      - 1217:443
    networks:
      - cluster
    volumes:
      - ./wwwroot:/usr/local/nginx/html
networks:
  cluster:

docker-compose.yml file explanation

Version: declared version, currently 3
 Service: specific service
 nginx: service name
 Hostname: container hostname
 build: create
 context: provides the directory provided by resources and materials, which is used to create the path of the container
 Dockerfile: dockerfile
 ports: the port provided, equivalent to the setting of docker run-p
 networks: network name. If it is a cluster, the name setting needs to be consistent (the same network environment)
volumes: data volume, this case is to provide the homepage
 Networks: the sub option of services refers to the exposed network name

Start operation

[root@localhost opt]# docker-compose -f compose_nginx/docker-compose.yml up -d
#-f -- specify file-d -- background up -- start
....//The first execution takes a long time and displays a lot of information, which is omitted here.

The operation results and some explanations are as follows

If the above command is executed repeatedly, the following results will appear, so this WARNING is a friendly prompt!

[root@localhost opt]# docker-compose -f compose_nginx/docker-compose.yml up -d
compose_nginx_nginx_1 is up-to-date

View mirrors and containers

[root@localhost opt]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
compose_nginx_nginx   latest              67f9a64cc32d        5 minutes ago       587MB
centos                7                   5e35e350aded        5 months ago        203MB
[root@localhost opt]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS              PORTS                                         NAMES
8f3d48d7c765        compose_nginx_nginx   "/nginx.sh"         5 minutes ago       Up 5 minutes        0.0.0.0:1216->80/tcp, 0.0.0.0:1217->443/tcp   compose_nginx_nginx_1

Test validation:

Docker compose configuration common fields (. In yml file)

Docker compose common commands (bash terminal)

Posted by czs on Wed, 22 Apr 2020 09:05:49 -0700