安装 docker

这里使用阿里云的源进行安装。

安装任何组件前的好习惯更新

1
apt-get update

安装必要的系统工具

1
apt-get -y install apt-transport-https ca-certificates curl software-properties-common

安装 GPG 证书

1
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/debian/gpg | sudo apt-key add -

写入软件源

1
add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/debian $(lsb_release -cs) stable"

更新并安装 docker-ce

1
2
apt-get -y update
apt-get -y install docker-ce

正常配置好 docker 之后,每次运行 docker 命令需要在前面加:sudo,如:sudo docker images。通过将当前用户添加进 docker 组,以后可以直接运行(docker images)。

1
2
sudo groupadd docker
sudo gpasswd -a ${USER} docker

重启 docker 服务,并退出当前用户重新登陆。

1
2
3
sudo service docker restart     # 重启docker服务
su root # 切换到root用户
su ${USER} # 再切换到原来的应用用户以上配置才生效

备注:

以上命令无需更改,直接拷贝运行即可( ${USER}命令能直接识别到当前用户名)。

查看 docker 版本

1
docker -v

系统显示:

1
Docker version 19.03.8, build afacb8b7f0

docker-ce 安装成功。

使用国内镜像

Docker 官方镜像经常掉线,只能换国内源。

国内的镜像源有:

  • docker 官方中国区 https://registry.docker-cn.com
  • 网易 http://hub-mirror.c.163.com
  • ustc http://docker.mirrors.ustc.edu.cn
  • 阿里云 http://<阿里云镜像加速器 ID>.mirror.aliyuncs.com

阿里云的镜像加速需要到阿里云 容器镜像服务 - 镜像加速器 获取阿里云镜像加速器 ID。

编辑源配置文件

配置文件路径为: /etc/docker/daemon.json
没有该文件的话,请先建一个。

1
nano /etc/docker/daemon.json

修改为以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"registry-mirrors" : [
"http://<阿里云镜像加速器 ID>.mirror.aliyuncs.com",
"http://registry.docker-cn.com",
"http://docker.mirrors.ustc.edu.cn",
"http://hub-mirror.c.163.com"
],
"insecure-registries" : [
"registry.docker-cn.com",
"docker.mirrors.ustc.edu.cn"
],
"debug" : true,
"experimental" : true
}

registry-mirrors 千万不要用 https,而是用 http,否则会显示 No certs for egitstry.docker.com
insecure-registries 不要任何 http 头,否则无法通过。

重启服务

重启让新配置源生效。

1
2
systemctl daemon-reload
systemctl restart docker

容器测试

直接运行 hello-world 来测试容器,系统会自动下载 hello-world 镜像。

1
docker run hello-world

系统显示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world

1b930d010525: Pull complete
Digest: sha256:f9dfddf63636d84ef479d645ab5885156ae030f611a56f3a7ac7f2fdd86d7e4e
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/

至此,docker 安装并运行成功。

安装 docker-compose

安装 docker-compose

运行命令:

1
curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

速度慢可以换国内源:

1
curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

给 docker-compose 执行权限,运行命令:

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

检查

1
docker-compose --version

系统显示

1
docker-compose version 1.23.2, build 1110ad01

配置 docker-compose

docker-compose 配置模板示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
version: "3"
services:
<service-name>: # 服务名称,可配置多个服务
image: <image-name> # 镜像名称
hostname: <hostname> # 主机名称,设置后进入容器便于识别。
container_name: <container-name> # 容器名称
environment: # 环境变量设置(命令行中的 -e 设置),以下是 mariadb 的示例。
TZ: Asia/Shanghai
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_USER: username
MYSQL_PASSWORD: userpassword
MYSQL_DATABASE:
MYSQL_ALLOW_EMPTY_PASSWORD:
MYSQL_RANDOM_ROOT_PASSWORD:
command: # 附加参数示例,以下是 mariadb 的示例。
-character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--explicit_defaults_for_timestamp=true
--lower_case_table_names=1
--max_allowed_packet=128M
--sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO"
volumes: # 卷映射设置示例
- /home/<username>/docker/mariadb/data:/var/lib/mysql:rw # 方式一:直接映射
- mariadb:/var/lib/mysql:rw # 方式二:通过申明的卷 mariadb 映射
ports: # 端口映射示例
- 127.0.0.1:80:80 # 端口映射到指定 ip 的端口
- 443:443 # 端口映射到所有端口
restart: always # 系统重启后,容器也自动启动。
networks: # 网络设置示例(本例使用自定义网络 localnet,需要提前创建好该网络。)
- localnet
networks:
localnet: # 自定义网络 localnet 设置。
external: true
volumes: # 卷设置
mariadb:

附:docker-compose 安装方法二

上述安装方法由于服务器问题,经常下载不成功。

这里介绍另外一种方法,通过 pip 安装。

1
2
apt-get install python3-pip
pip3 install docker-compose