Docker部署
1 docker安装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
rpm -qa | grep docker
yum list docker --show-duplicates
安装docker-ce
yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum list docker-ce --show-duplicates
安装 docker-ce 指定版本
yum install docker-ce-19.03.15-3.el7 docker-ce-cli-19.03.15-3.el7 -y
yum install docker-ce-20.10.9-3.el7 docker-ce-cli-20.10.9-3.el7 -y
查看安装后的版本
~]# rpm -qa | grep docke
docker-ce-19.03.5-3.el7.x86_64
docker-ce-cli-20.10.3-3.el7.x86_64
~]# docker --version
Docker version 20.10.3, build 48d30b5
2 配置文件:
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
mkdir -p /etc/docker
mkdir -p /data/docker
vim /etc/docker/daemon.json
#如果docker不存在手动创建
{
"graph": "/data/docker",
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m",
"max-file": "10"
},
"bip": "10.10.11.1/24",
"oom-score-adjust": -1000,
"registry-mirrors": ["https://7gyvuaak.mirror.aliyuncs.com"],
"storage-driver": "overlay2",
"storage-opts":["overlay2.override_kernel_check=true"],
"default-address-pools" : [
{
"base" : "13.10.0.0/16",
"size" : 24
}
],
"live-restore": true
}
"graph": "/data/docker", docker工作目录
"storage-driver": "overlay2", 存储驱动 overlay2
"insecure-registries": ["registry.access.redhat.com","quay.io"],
"registry-mirrors": ["https://q2gr04ke.airror.allyuncs.com"], 阿里云加速源
"bip": "172.168.11.0/24", docker内部网络
"exec-opts": ["native.cgroupdriver=systemd"], 启动的时候的额外参数 cgroupdriver 是对cpu内核等资源的控制方法
default-address-pools 指定默认创建的network网络段
"live-restore": true 在docker引擎关闭时,跑在宿主机上的容器是否存活
容器的IP地址与虚拟机的地址 第二位和第三位一致,容易排查问题
systemctl daemon-reload
registry-mirror镜像地址: 获得自己专属加速器地址
镜像仓库申请地址:
https://cr.console.aliyun.com/cn-shanghai/instances/repositories
登录后阿里开发者帐户后,https://cr.console.aliyun.com/undefined/instances/mirrors
默认的密码是: 阿里云登录密码
3 docke服务器启动
1
2
3
4
5
6
7
8
9
10
设置开机自启
systemctl enable docker
开启docker
systemctl start docker
无法启动: 主要原因配置文件docker启动的bip与默认的docker不一致
删除默认docker IP
ip link set dev docker0 down
systemctl start NetworkManager
nmcli device connect docker0
4 docker使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
开始使用
命令:
~]# docker info
卸载:
1、查询安装过的包
~]# yum list installed | grep docker
~]# yum -y remove docker-ce.x86_64
~]# yum -y remove docker-ce-cli.x86_64
~]# rm -rf /var/lib/docker*
~]# rm -rf /data/docker
~]# yum list installed | grep docker
5 启动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
启动第一个容器
~]# docker run hello-world
报错
docker: error pulling image configuration: Get https://production.cloudflare.docker.com/registry-v2/docker/registry/v2/blobs/sha256/bf/bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b/data?verify=1614167109-p0e8li9R7xSEwurEFBnARHEzSu0%3D: dial tcp 104.18.122.25:443: i/o timeout.
同步时间(可能不需要)
yum install ntpdate
ntpdate time.windows.com
安装成功效果
[root@fscloude data]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d
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/
登录注册的 dockerhub.com
~]# docker login docker.io
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: fscloude
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
6 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
搜索镜像:
~]# docker search alpine
下载镜像
~]# docker pull alpine 下载最新版
~]# docker pull alpine:3.10.3 或 docker.io/library/alpine:3.10.3 下载指定版本
显示当前安装的镜像
~]# docker image ls
~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
fscloude/alpine latest 28f6e2705743 8 days ago 5.61MB
alpine latest 28f6e2705743 8 days ago 5.61MB
hello-world latest bf756fb1ae65 14 months ago 13.3kB
fscloude/alpine v3.10.3 965ea09ff2eb 16 months ago 5.55MB
alpine 3.10.3 965ea09ff2eb 16 months ago 5.55MB
给镜像打标签
~]# docker tag 0e5574283393 fedora/httpd:version1.0
推送镜像
~]# docker push docker.io/fscloude/alpine:v3.10.3
删除镜像
~]# docker rmi docker.io/fscloude/alpine:latest 只是删除本地的tag标签
Untagged: fscloude/alpine:latest
Untagged: fscloude/alpine@sha256:4661fb57f7890b9145907a1fe2555091d333ff3d28db86c3bb906f6a2be93c87
~]# docker rmi 28f6e2705743 删除alpine这个最新镜像,错误: 因为还有其他标签使用此镜像
Error response from daemon: conflict: unable to delete 28f6e2705743 (must be forced) - image is referenced in multiple repositories
~]# docker rmi -f 28f6e2705743
Untagged: fscloude/alpine:latest 删除成功
Untagged: alpine:latest
Untagged: alpine@sha256:a75afd8b57e7f34e4dad8d65e2c7ba2e1975c795ce1ee22fa34f8cf46f96a3be
Deleted: sha256:28f6e27057430ed2a40dbdd50d2736a3f0a295924016e294938110eeb8439818
Deleted: sha256:cb381a32b2296e4eb5af3f84092a2e6685e88adbc54ee0768a1a1010ce6376c7
~]# docker pull fscloude/alpine 删除之后从自己的 docker.io 里面拉取最新的
容器是由镜像模板实例化而来的
docker本质就是宿主机的一个进程
常用操作
~]# docker ps docker启动的进程
-a 启动和停止的所有进程
7 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
交互式启动
~]# docker run -ti fscloude/alpine /bin/sh docker运行指定镜像 + 命令
非交互式启动:使用创建一个进程自动删除 docker ps -a 查不出来。易用于 仅执行一次的环境
~]# docker run --rm fscloude/alpine:latest /bin/echo "hellow"
非交互式后台启动: 启动时必须指定执行某个命令 否则会直接关闭此启动容器
~]# docker run -d --name my_first_alpine fscloude/alpine:latest /bin/sleep 10 让此容器进入后台10s后退出
注:尽管使用容器启动的进程,但是在宿主机上也能查到此进程,ps -ef,说明容器使用的是宿主机的内核
进入启动容器的内部
~]# docker exec -ti ba5e2a9cc64a /bin/sh
/ # ps -ef
PID USER TIME COMMAND
1 root 0:00 /bin/sleep 300
6 root 0:00 /bin/sh
11 root 0:00 ps -ef
/ #
以守护形式运行容器
Ctrl+P
Ctrl+Q
docker attach dockerfile_ubuntu_web/df0d417a1598
8 docker容器启停
1
2
3
4
5
6
7
8
9
10
11
12
13
容器的启停: 重新启动此容器,重新开始
~]# docker stop ba5e2a9cc64a
~]# docker start ba5e2a9cc64a
~]# docker restart ba5e2a9cc64a
删除容器: 删除之后 docker ps -a查不出来
~]# docker rm bold_feistel(NAMES)
~]# docker rm 10b819f357a9(CONTAINER ID)
~]# docker rm -f 10b819f357a9(CONTAINER ID)
循环删除已退出的容器
~]# for i in `docker ps -a | grep -i exit | awk '{print $1}'`;do docker rm -f $i; done
9 自定义容器
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
提交容器: 将内容修改后的容器,固化成一个新镜像
~]# docker commit -p my_first_alpine6 fscloude/alpine:v3.12.4_test
导出容器
~]# docker save 700e99d2e16d > alpine_3.12.4_test.tar
~]# docker rmi 700e99d2e16d
导入容器
~]# docker load < alpine_3.12.4_test.tar
~]# docker tag 700e99d2e16d fscloude/alpine:v3.12.4_test
单次启动比较好的方式
~]# docker run --rm --name my_second1 -ti fscloude/alpine:v3.12.4_test /bin/sh
查看容器日志: 查看的是启动后的ID
~]# docker logs 7317d943dda2(CONTAINER ID)
docker高(升)级操作
映射端口
docker run -p 容器外端口:容器内端口
~]# docker pull nginx:1.18.0
~]# docker run --rm -d --name my_first_nginx -p81:80 fscloude/nginx:v1.18.0
挂载数据卷
docker run -v 容器外目录:容器内目录
~]# mkdir /root/html && wget www.baidu.com index
html]# docker run -ti --rm -d --name nginx_v1.18.0_baidu -p 82:80 -v /root/html/:/usr/share/nginx/html fscloude/nginx:v1.18.0
检查(查看)容器启动的详细信息
docker inspect 1d1077309873(CONTAINER ID)
传递环境变量
docker run -e 环境变量key=环境变量value
容器内安装软件(工具)
yum/apt-get/apt等
使用dockerfile创建镜像
docker名字必须是: Dockerfile
docker build -t='fscloude/dockerfile_test_first' /data/docker_save/dockerfile/
/data/docker_save/dockerfile/ 此目录下存放Dockerfile文件
# First dockerfile for test
FROM ubuntu:14.04
MAINTAINER fscloude
RUN apt-get update
RUN apt-get install -y nginx
EXPOSE 80
dockerfile执行出现问题

解决
ENV DEBIAN_FRONTEND noninteractive # apt-get安装依赖时并非静默安装,需要交互
FROM 使用方法
FROM <image>
FROM <image>:<tag>
image 镜像名称
tag 版本名称
MAINTAINER 使用方法
MAINTAINER <name>
name 指定镜像的作者信息吗,包含镜像的所有者和联系信息
RUN 使用方法
RUN <command> (shell模式)
RUN [ "executable", "param1", "param2" ] (exec模式)
可以指定其他的shell运行指令
RUN ["/bin/bash", "-c", "echo hello"]
EXPOSE 使用方法
EXPOSE <port> [<port>...]
指定运行该镜像的容器使用的端口
只是说明会用到哪个端口,不会开启
仍然需要使用 docker run 执行指定端口
docker run -ti --rm -d --name dockerfile_ubuntu_web -p 83:80 fscloude/ngindddx:v1.18.0
CMD 和 ENTERYPOINT 指定在容器 运行时 执行的命令
CMD
是容器默认执行的指令,使用 docker run 执行的指令会覆盖 CMD指令
CMD 包含 RUN 使用的两种方式,再加一种
CMD <command> param1 param2 (shell模式)
CMD [ "executable", "param1", "param2" ] (exec模式)
CMD ["param1", "param2"] 作为 ENTERYPOINT 指令的默认参数
eg:
# First dockerfile for test
FROM ubuntu:14.04
MAINTAINER fscloude
RUN apt-get update
RUN apt-get install -y nginx
EXPOSE 80
CMD ["/usr/sbin/nginx", "-g", "dameon off;"]
ADD COPY 和 VOLUME 设置镜像的目录和文件
WORKDIR ENV 和 USER 在构建及运行镜像时的环境设置
ONBUILD 类似触发器的指令
10. 设置代理
docker版本 > 20.10.0
在 /etc/docker/daemon.json 中设置
1 2 3 4 5 6 7 8 9 { "proxies": { "default": { "httpProxy": "http://<proxy-server\>:<port\>", "httpsProxy": "http://<proxy-server\>:<port\>", "noProxy": "localhost,127.0.0.1" } } }docker版本 < 20.10.0
注: 设置 env不生效
1 export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890需要配置代理文件, 如下:
mkdir -p /etc/systemd/system/docker.service.d
vim /etc/systemd/system/docker.service.d/http-proxy.conf
1 2 3 4 [Service] Environment="HTTP_PROXY=http://<proxy-server\>:<port\>" Environment="HTTPS_PROXY=http://<proxy-server\>:<port\>" Environment="NO_PROXY=localhost,127.0.0.1"比如:
1 2 3 4 [Service] Environment="HTTP_PROXY=http://127.0.0.1:43932" Environment="HTTPS_PROXY=http://127.0.0.1:43932" Environment="NO_PROXY=localhost,127.0.0.1"使配置生效:
1 2 3 systemctl daemon-reload systemctl restart docker systemctl status docker
本文由作者按照
CC BY 4.0
进行授权