前言

把docker-ce安装和使用部分先独立抽取出来,关于Docker更加深入的理解可以参考文章开头推荐的文章

更新历史

2022年05月13日 - 增加Docker离线部署
2018年10月25日 - 更新大量Docker实践操作和精选的Docker入门文章
2018年05月21日 - 初稿

阅读原文 - https://wsgzao.github.io/post/docker/

扩展阅读

Docker - https://www.docker.com/


Docker入门

Docker 一篇通

Docker 入门教程
http://www.ruanyifeng.com/blog/2018/02/docker-tutorial.html

Docker 微服务教程
http://www.ruanyifeng.com/blog/2018/02/docker-wordpress-tutorial.html

Nginx 容器教程
http://www.ruanyifeng.com/blog/2018/02/nginx-docker.html

docker_practice
https://github.com/yeasy/docker_practice

环境说明

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
# 操作系统版本
cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
# 操作系统内核
uname -r
3.10.0-693.21.1.el7.x86_64
# 硬件架构
uname -m
x86_64
# docker版本
docker version

Client:
Version: 18.03.0-ce
API version: 1.37
Go version: go1.9.4
Git commit: 0520e24
Built: Wed Mar 21 23:09:15 2018
OS/Arch: linux/amd64
Experimental: false
Orchestrator: swarm

Server:
Engine:
Version: 18.03.0-ce
API version: 1.37 (minimum version 1.12)
Go version: go1.9.4
Git commit: 0520e24
Built: Wed Mar 21 23:13:03 2018
OS/Arch: linux/amd64
Experimental: false

部署Docker

使用阿里云加速器加快获取docker官方的镜像

步骤一:如果没有阿里云账号,需要注册阿里云开发账号

https://dev.aliyun.com/

步骤二:进入加速器页面获取加速信息

https://cr.console.aliyun.com/#/accelerator

安装/升级你的Docker客户端

推荐安装1.10.0以上版本的Docker客户端,参考文档 docker-ce
https://yq.aliyun.com/articles/110806?spm=5176.8351553.0.0.6a721991ZTvV8S

Docker CE 镜像源站

使用官方安装脚本自动安装

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

CentOS 7 (使用yum进行安装)

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
# step 1: 安装必要的一些系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3: 更新并安装 Docker-CE
sudo yum makecache fast
sudo yum -y install docker-ce
# Step 4: 开启Docker服务
sudo service docker start

# 注意:
# 官方软件源默认启用了最新的软件,您可以通过编辑软件源的方式获取各个版本的软件包。例如官方并没有将测试版本的软件源置为可用,你可以通过以下方式开启。同理可以开启各种测试版本等。
# vim /etc/yum.repos.d/docker-ce.repo
# 将 [docker-ce-test] 下方的 enabled=0 修改为 enabled=1
#
# 安装指定版本的Docker-CE:
# Step 1: 查找Docker-CE的版本:
# yum list docker-ce.x86_64 --showduplicates | sort -r
# Loading mirror speeds from cached hostfile
# Loaded plugins: branch, fastestmirror, langpacks
# docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
# docker-ce.x86_64 17.03.1.ce-1.el7.centos @docker-ce-stable
# docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable
# Available Packages
# Step2 : 安装指定版本的Docker-CE: (VERSION 例如上面的 17.03.0.ce.1-1.el7.centos)
# sudo yum -y install docker-ce-[VERSION]

如何配置镜像加速器

针对Docker客户端版本大于1.10.0的用户

您可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器:

1
2
3
4
5
6
7
8
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://ffia7t7n.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

安装Docker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 使用阿里云作加速
cd /etc/yum.repos.d/ && rm -f *.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum remove -y docker docker-common docker-selinux docker-engine

# 安装docker官方源,国内下载较慢建议切换为阿里云加速
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum-config-manager --enable docker-ce-edge
yum list docker-ce --showduplicates | sort -r |grep stable

# 安装docker,可以选择制定版本
yum install -y docker-ce
systemctl start docker
systemctl enable docker
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://ffia7t7n.mirror.aliyuncs.com"] # 为了后期方便添加阿里云的加速镜像站
}
EOF
systemctl daemon-reload
systemctl restart docker

测试Docker服务正常

例行跑个hello world测试下Docker服务是否正常

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
# 测试docker
docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:97ce6fa4b6cdc0790cda65fe7290b74cfebd9fa0c9b8c38e979330d547d22ce1
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://cloud.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/

安装docker-compose

yum install -y docker-compose

离线安装Docker

先在一台可以联网的相同操作系统环境下下载离线安装包,然后内网服务器构建本地yum源,最后安装docker

下载离线安装包

在可以联网的机器上下载rpm包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#安装yum-plugin-downloadonly插件
yum install -y yum-plugin-downloadonly

# 下载createrepo相关rpm到指定目录,构建本地源之前需要在本地安装createrepo,用于构建本地源
mkdir -p /tmp/repo/createrepo/
yum install --downloadonly --downloaddir=/tmp/repo/createrepo/ createrepo

# 下载docker相关rpm到指定目录
mkdir -p /tmp/repo/docker-ce/
yum install --downloadonly --downloaddir=/tmp/repo/docker-ce/ docker-ce

# 下载docker-compose相关rpm到指定目录
mkdir -p /tmp/repo/docker-compose/
yum install --downloadonly --downloaddir=/tmp/repo/docker-compose/ docker-compose

构建本地yum源

将docker的rpm包拷贝到需要安装docker的局域网集群上面,例如:
/tmp/repo/docker-ce/

将docker的rpm包拷贝到需要安装docker的局域网集群上面,例如:
/tmp/repo/createrepo/

构建本地yum源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 安装createrepo
rpm -ivh /tmp/repo/createrepo/*.rpm
# 备份/etc/yum.repos.d目录下文件
ls /etc/yum.repos.d | xargs -I{} mv /etc/yum.repos.d/{} /etc/yum.repos.d/{}.bk
# 创建新的*.repo文件,如:docker.repo,进行配置
vim docker.repo
[local-yum]
name=local-yum
baseurl=file:////tmp/repo/docker-ce/
enabled=1
gpgcheck=0
# 设置本地源
createrepo -d /tmp/repo/docker-ce/
# 查看看是否能看到自己构建的本地源
yum repolist
# 清除缓存
yum clean all
# 创建缓存
yum makecache
# 看本地源是否配置成功,通过yum list看是否输出了新的rpm包,如果能查询到,证明配置成功
yum list | grep docker
# 离线安装docker
yum install docker -y

Docker常用命令

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# 启动docker服务
systemctl start docker
systemctl enable docker

# 查看当前系统 Docker 信息
docker info

# 拉取 docker 镜像
docker pull image_name
# 从 Docker hub 上下载某个镜像
docker pull centos:latest

# 查看宿主机上的镜像,Docker 镜像保存在 / var/lib/docker 目录下:
docker images

REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 1b30b36ae96a 8 days ago 372MB
zabbix/zabbix-web-nginx-mysql centos-4.0-latest 8be5f91b2fa1 3 weeks ago 415MB
zabbix/zabbix-server-mysql centos-4.0-latest 8e5becf45c4e 3 weeks ago 326MB

# 删除镜像
docker rmi image_name/image_id
docker rmi zabbix/zabbix-web-nginx-mysql:centos-4.0-latest 或者 docker rmi 8be5f91b2fa1

# 查看当前有哪些容器正在运行
docker ps
# 查看所有容器
docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b30307ad65be zabbix/zabbix-web-nginx-mysql:centos-4.0-latest "docker-entrypoint.sh" 7 days ago Exited (255) 8 minutes ago 443/tcp, 0.0.0.0:8080->80/tcp zabbix-web-nginx-mysql
0ad822cd52b7 zabbix/zabbix-server-mysql:centos-4.0-latest "docker-entrypoint.sh" 7 days ago Exited (255) 8 minutes ago 0.0.0.0:10051->10051/tcp zabbix-server-mysql
d01c89a112f7 mysql:5.7 "docker-entrypoint.s…" 7 days ago Exited (255) 8 minutes ago 3306/tcp, 33060/tcp mysql-server

# 启动、停止、重启容器命令:
docker start container_name/container_id
docker stop container_name/container_id
docker restart container_name/container_id

#后台启动一个容器后,如果想进入到这个容器,可以使用 attach
docker attach container_name/container_id

# 删除容器
docker rm container_name/container_id

# 查看容器日志
docker logs -f container_name/container_id

# 查看容器IP地址
docker inspect container_name/container_id

# 进入容器
docker exec -it container_name/container_id bash

# 从Docker容器与宿主机相互传输文件
[root@localhost tmp]# docker cp --help

Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Copy files/folders between a container and the local filesystem

docker cp zabbix_config.sql mysql-server:/tmp
docker cp mysql-server:/tmp/zabbix_config.sql /tmp


# 批量删除所有已经退出的容器
docker rm -v $(docker ps -aq -f status=exited)


# docker help
[root@centos7lined1 wangao]# docker help

Usage: docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
--config string Location of client config files (default "/root/.docker")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit

Management Commands:
config Manage Docker configs
container Manage containers
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes

Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes

Docker Compose

Compose 是 Docker 公司推出的一个工具软件,可以管理多个 Docker 容器组成一个应用。你需要定义一个 YAML 格式的配置文件docker-compose.yml,写好多个容器之间的调用关系。然后,只要一个命令,就能同时启动 / 关闭这些容器。

1
2
3
4
5
6
# 查看版本
docker-compose --version
# 启动所有服务
docker-compose up
# 关闭所有服务
docker-compose stop

Docker 离线环境部署

https://docs.cyberwatch.fr/deploy/en/2_deploy_cyberwatch/offline/swarm/install_docker.html

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
# install
dpkg -i *.deb
dpkg -i containerd.io_*_amd64.deb
dpkg -i docker-ce-cli_*_amd64.deb
dpkg -i docker-ce_*_amd64.deb

# check
docker info
docker run --rm hello-world

# check error log if start failed
journalctl -xeu docker.service

# remove
apt-get purge docker-ce
apt-get purge docker-ce-cli
apt-get purge containerd.io

# check
apt list --installed | grep docker
apt list --installed | grep containerd.io

# clean file
rm -rf /var/lib/docker
rm -rf /var/run/docker
rm -rf /etc/docker
rm -rf /run/docker.sock
rm -rf /var/lib/containerd
rm -rf /run/containerd
rm -rf /opt/containerd

参考阅读

docker 命令详解 - https://github.com/KeKe-Li/docker-directive

Docker — 从入门到实践

文章目录
  1. 1. 前言
  2. 2. 更新历史
  3. 3. Docker入门
  4. 4. 环境说明
  5. 5. 部署Docker
    1. 5.1. 使用阿里云加速器加快获取docker官方的镜像
    2. 5.2. 安装/升级你的Docker客户端
    3. 5.3. 如何配置镜像加速器
    4. 5.4. 安装Docker
    5. 5.5. 测试Docker服务正常
    6. 5.6. 安装docker-compose
  6. 6. 离线安装Docker
    1. 6.1. 下载离线安装包
    2. 6.2. 构建本地yum源
  7. 7. Docker常用命令
  8. 8. Docker Compose
  9. 9. Docker 离线环境部署
  10. 10. 参考阅读