Docker

用于打包,解决环境搭建问题

基于Go

安装(linux-CentOS)

查看官方的帮助文档

卸载旧的版本

1
2
3
4
5
6
7
8
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine

使用仓库安装

1
2
3
4
5
6
7
8
9
yum install -y yum-utils
# 设置镜像地址
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
#这里使用阿里云的镜像
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

更新Docker

1
2
yum makecache
yum update

安装Docker引擎

1
yum install docker-ce docker-ce-cli containerd.io

可能会报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1. 首先更新yum

$ yum update

2. 以下方法任选一种
# 方法1: 安装特定版本的docker-ce和containerd.io
$ yum list docker-ce --showduplicates | sort -r

docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable
...

$ sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io

# 方法2: 通过阿里云镜像库安装符合最新docker-ce版本的containerd.io
$ yum install -y https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/edge/Packages/containerd.io-1.3.7-3.1.el8.x86_64.rpm
yum install -y https://download.docker.com/linux/centos/8/x86_64/edge/Packages/docker-ce-19.03.13-3.el8.x86_64.rpm
yum install -y https://download.docker.com/linux/centos/8/x86_64/edge/Packages/docker-ce-cli-19.03.13-3.el8.x86_64.rpm
#装第三个的时候反复timeout,人晕了

启动Docker

1
systemctl start docker

检查安装

1
2
3
4
#检查版本号
docker version
#运行hello world
docker run hello-world

报错

1
2
3
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: TLS handshake timeout.
See 'docker run --help'.

timeout,应该是镜像源的问题,这个阿里云真够恶心的,用了阿里自己的镜像下rpm的时候都会timeout。

1
2
3
4
5
[root@Shyee local]# yum install -y https://download.docker.com/linux/centos/8/x86_64/edge/Packages/docker-ce-cli-19.03.13-3.el8.x86_64.rpm
Last metadata expiration check: 2:09:13 ago on Tue 19 Jan 2021 06:27:23 PM CST.
[MIRROR] docker-ce-cli-19.03.13-3.el8.x86_64.rpm: Curl error (28): Timeout was reached for https://download.docker.com/linux/centos/8/x86_64/edge/Packages/docker-ce-cli-19.03.13-3.el8.x86_64.rpm [Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds]
[MIRROR] docker-ce-cli-19.03.13-3.el8.x86_64.rpm: Curl error (28): Timeout was reached for https://download.docker.com/linux/centos/8/x86_64/edge/Packages/docker-ce-cli-19.03.13-3.el8.x86_64.rpm [Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds]
[MIRROR] docker-ce-cli-19.03.13-3.el8.x86_64.rpm: Curl error (28): Timeout was reached for https://download.docker.com/linux/centos/8/x86_64/edge/Packages/docker-ce-cli-19.03.13-3.el8.x86_64.rpm [Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds]

尝试解决:

/etc/docker新增daemon.json,添加镜像

1
2
3
{
"registry-mirrors": ["http://hub-mirror.c.163.com"]
}

重启

1
systemctl restart docker.service

报错

1
docker: Get https://registry-1.docker.io/v2/library/hello-world/manifests/sha256:90659bf80b44ce6be8234e6ff90a1ac34acbeb826903b02cfa0da11c82cbc042: net/http: TLS handshake timeout.

尝试解决:

更改镜像:

1
{"registry-mirrors":["https://registry.docker-cn.com","https://pee6w651.mirror.aliyuncs.com"]}

后来才知道,阿里云有个容器镜像服务,可以开启,并拿到自己的镜像加速器,也是如上配置。

成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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/

检查镜像

1
2
3
4
5
docker images

#运行结果
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 12 months ago 13.3kB

卸载Docker

1
2
3
yum remove docker-ce docker-ce-cli containerd.io

rm -rf /var/lib/docker

底层原理

C/S结构

通过socket连接

1、Docker有着比虚拟机更少的抽象层。

2、docker利用的是宿主机的内核,vm需要是Guest Os。