Introduction to docker-compose and simple use

1 installation

1.1 docker

Follow the steps below to install:

# 更新现有的包
sudo apt update

# 添加Docker PGP公钥
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -

# 配置Docker APT库
echo 'deb [arch=amd64] https://download.docker.com/linux/debian buster stable' | sudo tee /etc/apt/sources.list.d/docker.list

# 更新一下APT列表
sudo apt-get update

# 安装Docker,在安装前先删除原来系统中可能自带的Docker:
sudo apt-get remove docker docker-engine docker.io

# 安装Docker:
sudo apt-get install docker-ce

# 看看安装成功了没:
sudo docker run hello-world

# 设置启动
sudo systemctl start docker
sudo systemctl enable docker

1.2 docker-compose

Get the executable file to the local (sudo may be required)

curl -L https://github.com/docker/compose/releases/download/1.25.0-rc1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

Set permissions for executable files

chmod +x /usr/local/bin/docker-compose

Determine whether the installation is successful

docker-compose version

The final effect:

2 Introduction to docker-compose

First of all, you need to know what scenarios docker-compose is designed to solve.

Running docker requires long and complicated commands. Such as:

docker run \
  --detach \
  --name registry \
  --hostname registry \
  --volume $(pwd)/app/registry:/var/lib/registry \
  --publish 5000:5000 \
  --restart unless-stopped \
  registry:latest

In order to facilitate the operation, this docker run can be written as a shell file.

Web mirroring is indeed more complicated than this. A simple docker run command is not enough to start a web service. Usually multiple mirrors are needed, and there will be some priority relationships between the mirrors. The orchestration of this relationship is called docker's orchestration.

Docker provides a command line tool docker-compose to help complete docker's orchestration. To use docker-compose, you need to write a docker-compose.yml file (configuration file).
E.g:

version: "3"
services:
  web:
    image: beginor/geoserver:2.11.1
    container_name: geoserver-web
    hostname: geoserver-web
    ports:
      - 8080:8080
    volumes:
      - ./web/data_dir:/geoserver/data_dir
      - ./web/logs:/geoserver/logs
    restart: unless-stopped
    links:
      - database:database
  database:
    image: beginor/postgis:9.3
    container_name: postgis
    hostname: postgis
    ports:
      - 5432:5432
    volumes:
      - ./database/data:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: 1q2w3e4R
    restart: unless-stopped

The above yml file defines two services web and database. A service corresponds to an instance of a container when it is running. The above file indicates that two instances are to be started.

When deploying, you usually need to put the docker-compose.yml file in a directory to represent an application, and docker will create an independent network for this application.

It can be used docker-compose up -dto start the program.

~/Desktop/source » docker-compose up -d                                                                                                                       255 ↵ littlechieh6@MacBook-Pro
Creating network "source_app1" with the default driver
Building server
Step 1/6 : FROM ubuntu:16.04
16.04: Pulling from library/ubuntu
be8ec4e48d7f: Pull complete
33b8b485aff0: Pull complete
d887158cc58c: Pull complete
05895bb28c18: Pull complete
Digest: sha256:3355b6e4ba1b12071ba5fe9742042a2f10b257c908fbdfac81912a16eb463879
Status: Downloaded newer image for ubuntu:16.04
……

To stop above the container, simply enter docker-compose downcan.

As can be seen from the above command, docker-compose can not only automatically create a network according to the configuration file docker-compose.yml, start the corresponding container instance, but also delete the container instance according to the configuration.

3 Use of docker-compose

Commonly used commands:

# 启动 docker-compose 的实例
docker-compose up -d

# 关闭 docker-compose 的实例
docker-compose down

# 列出本机正在运行的容器
docker container ls
docker ps

# 用来查看 docker 容器的输出,即容器里面 Shell 的标准输出。
$ docker container logs [containerID]

# 进入容器
$ docker container exec -it [containerID] /bin/bash

appendix

Reference tutorial:

  1. 使用 docker-compose 替代 docker run:https://beginor.github.io/2017/06/08/use-compose-instead-of-run.html
  2. Kali Linux 2020.1 Release Docker-CE: https://zhuanlan.zhihu.com/p/110675842

Guess you like

Origin blog.csdn.net/qq_43085611/article/details/112994515