Docker是Go语言开发,是一个开源的应用容器引擎,我们可以将开发出来的应用打包发布到Docker容器中运行,由于Docker容易是轻量级,可移植的优点,所以我们可以将做好的应用容器镜像移植到其他的Linux系统中运行,实现虚拟化。Docker分为企业版和社区版,我们个人学习使用社区版即可。
Docker安装
CentOS 7.6 64位 安装Docker,可参考官方文档地址:https://docs.docker.com/engine/install/centos/
1.移除旧版本Docker,如果未安装过可略过
[root@JD local]# sudo yum remove docker \
> docker-client \
> docker-client-latest \
> docker-common \
> docker-latest \
> docker-latest-logrotate \
> docker-logrotate \
> docker-engine
Loaded plugins: fastestmirror
No Match for argument: docker
No Match for argument: docker-client
No Match for argument: docker-client-latest
No Match for argument: docker-common
No Match for argument: docker-latest
No Match for argument: docker-latest-logrotate
No Match for argument: docker-logrotate
No Match for argument: docker-engine
No Packages marked for removal
2.安装yum-utils软件包
[root@JD local]# yum install -y yum-utils
Loaded plugins: fastestmirror
Determining fastest mirrors
base | 3.6 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/7): epel/x86_64/group_gz | 95 kB 00:00:00
(2/7): base/7/x86_64/group_gz | 153 kB 00:00:00
(3/7): extras/7/x86_64/primary_db | 206 kB 00:00:00
(4/7): epel/x86_64/updateinfo | 1.0 MB 00:00:00
(5/7): updates/7/x86_64/primary_db | 4.5 MB 00:00:00
(6/7): base/7/x86_64/primary_db | 6.1 MB 00:00:00
(7/7): epel/x86_64/primary_db | 6.9 MB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package yum-utils.noarch 0:1.1.31-54.el7_8 will be installed
--> Processing Dependency: python-kitchen for package: yum-utils-1.1.31-54.el7_8.noarch
--> Processing Dependency: libxml2-python for package: yum-utils-1.1.31-54.el7_8.noarch
--> Running transaction check
---> Package libxml2-python.x86_64 0:2.9.1-6.el7.4 will be installed
--> Processing Dependency: libxml2 = 2.9.1-6.el7.4 for package: libxml2-python-2.9.1-6.el7.4.x86_64
---> Package python-kitchen.noarch 0:1.1.1-5.el7 will be installed
--> Processing Dependency: python-chardet for package: python-kitchen-1.1.1-5.el7.noarch
--> Running transaction check
---> Package libxml2.x86_64 0:2.9.1-6.el7_2.3 will be updated
---> Package libxml2.x86_64 0:2.9.1-6.el7.4 will be an update
---> Package python-chardet.noarch 0:2.2.1-3.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================================================
Package Arch Version Repository Size
========================================================================================================================
Installing:
yum-utils noarch 1.1.31-54.el7_8 updates 122 k
Installing for dependencies:
libxml2-python x86_64 2.9.1-6.el7.4 base 247 k
python-chardet noarch 2.2.1-3.el7 base 227 k
python-kitchen noarch 1.1.1-5.el7 base 267 k
Updating for dependencies:
libxml2 x86_64 2.9.1-6.el7.4 base 668 k
Transaction Summary
========================================================================================================================
Install 1 Package (+3 Dependent packages)
Upgrade ( 1 Dependent package)
Total download size: 1.5 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/5): libxml2-python-2.9.1-6.el7.4.x86_64.rpm | 247 kB 00:00:00
(2/5): python-chardet-2.2.1-3.el7.noarch.rpm | 227 kB 00:00:00
(3/5): libxml2-2.9.1-6.el7.4.x86_64.rpm | 668 kB 00:00:00
(4/5): python-kitchen-1.1.1-5.el7.noarch.rpm | 267 kB 00:00:00
(5/5): yum-utils-1.1.31-54.el7_8.noarch.rpm | 122 kB 00:00:00
------------------------------------------------------------------------------------------------------------------------
Total 8.8 MB/s | 1.5 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Updating : libxml2-2.9.1-6.el7.4.x86_64 1/6
Installing : libxml2-python-2.9.1-6.el7.4.x86_64 2/6
Installing : python-chardet-2.2.1-3.el7.noarch 3/6
Installing : python-kitchen-1.1.1-5.el7.noarch 4/6
Installing : yum-utils-1.1.31-54.el7_8.noarch 5/6
Cleanup : libxml2-2.9.1-6.el7_2.3.x86_64 6/6
Verifying : python-chardet-2.2.1-3.el7.noarch 1/6
Verifying : python-kitchen-1.1.1-5.el7.noarch 2/6
Verifying : libxml2-python-2.9.1-6.el7.4.x86_64 3/6
Verifying : libxml2-2.9.1-6.el7.4.x86_64 4/6
Verifying : yum-utils-1.1.31-54.el7_8.noarch 5/6
Verifying : libxml2-2.9.1-6.el7_2.3.x86_64 6/6
Installed:
yum-utils.noarch 0:1.1.31-54.el7_8
Dependency Installed:
libxml2-python.x86_64 0:2.9.1-6.el7.4 python-chardet.noarch 0:2.2.1-3.el7 python-kitchen.noarch 0:1.1.1-5.el7
Dependency Updated:
libxml2.x86_64 0:2.9.1-6.el7.4
Complete!
3.添加Docker镜像仓库地址并更新软件包索引
[root@JD ~]# sudo yum-config-manager \
> --add-repo \
> http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
[root@JD ~]# yum makecache fast
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
base | 3.6 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/2): docker-ce-stable/x86_64/primary_db | 46 kB 00:00:00
(2/2): docker-ce-stable/x86_64/updateinfo | 55 B 00:00:00
Metadata Cache Created
4.安装docker
[root@JD ~]# sudo yum install docker-ce docker-ce-cli containerd.io
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package containerd.io.x86_64 0:1.3.7-3.1.el7 will be installed
--> Processing Dependency: container-selinux >= 2:2.74 for package: containerd.io-1.3.7-3.1.el7.x86_64
---> Package docker-ce.x86_64 3:19.03.13-3.el7 will be installed
--> Processing Dependency: libcgroup for package: 3:docker-ce-19.03.13-3.el7.x86_64
---> Package docker-ce-cli.x86_64 1:19.03.13-3.el7 will be installed
--> Running transaction check
---> Package container-selinux.noarch 2:2.119.2-1.911c772.el7_8 will be installed
--> Processing Dependency: policycoreutils-python for package: 2:container-selinux-2.119.2-1.911c772.el7_8.noarch
---> Package libcgroup.x86_64 0:0.41-21.el7 will be installed
--> Running transaction check
---> Package policycoreutils-python.x86_64 0:2.5-34.el7 will be installed
--> Processing Dependency: policycoreutils = 2.5-34.el7 for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: setools-libs >= 3.3.8-4 for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libsemanage-python >= 2.5-14 for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: audit-libs-python >= 2.1.3-4 for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: python-IPy for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libqpol.so.1(VERS_1.4)(64bit) for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libqpol.so.1(VERS_1.2)(64bit) for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libapol.so.4(VERS_4.0)(64bit) for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: checkpolicy for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libqpol.so.1()(64bit) for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libapol.so.4()(64bit) for package: policycoreutils-python-2.5-34.el7.x86_64
--> Running transaction check
---> Package audit-libs-python.x86_64 0:2.8.5-4.el7 will be installed
--> Processing Dependency: audit-libs(x86-64) = 2.8.5-4.el7 for package: audit-libs-python-2.8.5-4.el7.x86_64
---> Package checkpolicy.x86_64 0:2.5-8.el7 will be installed
---> Package libsemanage-python.x86_64 0:2.5-14.el7 will be installed
---> Package policycoreutils.x86_64 0:2.5-29.el7 will be updated
---> Package policycoreutils.x86_64 0:2.5-34.el7 will be an update
---> Package python-IPy.noarch 0:0.75-6.el7 will be installed
---> Package setools-libs.x86_64 0:3.3.8-4.el7 will be installed
--> Running transaction check
---> Package audit-libs.x86_64 0:2.8.4-4.el7 will be updated
--> Processing Dependency: audit-libs(x86-64) = 2.8.4-4.el7 for package: audit-2.8.4-4.el7.x86_64
---> Package audit-libs.x86_64 0:2.8.5-4.el7 will be an update
--> Running transaction check
---> Package audit.x86_64 0:2.8.4-4.el7 will be updated
---> Package audit.x86_64 0:2.8.5-4.el7 will be an update
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================================================
Package Arch Version Repository Size
========================================================================================================================
Installing:
containerd.io x86_64 1.3.7-3.1.el7 docker-ce-stable 29 M
docker-ce x86_64 3:19.03.13-3.el7 docker-ce-stable 24 M
docker-ce-cli x86_64 1:19.03.13-3.el7 docker-ce-stable 38 M
Installing for dependencies:
audit-libs-python x86_64 2.8.5-4.el7 base 76 k
checkpolicy x86_64 2.5-8.el7 base 295 k
container-selinux noarch 2:2.119.2-1.911c772.el7_8 extras 40 k
libcgroup x86_64 0.41-21.el7 base 66 k
libsemanage-python x86_64 2.5-14.el7 base 113 k
policycoreutils-python x86_64 2.5-34.el7 base 457 k
python-IPy noarch 0.75-6.el7 base 32 k
setools-libs x86_64 3.3.8-4.el7 base 620 k
Updating for dependencies:
audit x86_64 2.8.5-4.el7 base 256 k
audit-libs x86_64 2.8.5-4.el7 base 102 k
policycoreutils x86_64 2.5-34.el7 base 917 k
Transaction Summary
========================================================================================================================
Install 3 Packages (+8 Dependent packages)
Upgrade ( 3 Dependent packages)
Total download size: 94 M
Is this ok [y/d/N]: y
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/14): audit-libs-2.8.5-4.el7.x86_64.rpm | 102 kB 00:00:00
(2/14): audit-libs-python-2.8.5-4.el7.x86_64.rpm | 76 kB 00:00:00
(3/14): audit-2.8.5-4.el7.x86_64.rpm | 256 kB 00:00:00
(4/14): checkpolicy-2.5-8.el7.x86_64.rpm | 295 kB 00:00:00
(5/14): container-selinux-2.119.2-1.911c772.el7_8.noarch.rpm | 40 kB 00:00:00
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-ce-19.03.13-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Public key for docker-ce-19.03.13-3.el7.x86_64.rpm is not installed
(6/14): docker-ce-19.03.13-3.el7.x86_64.rpm | 24 MB 00:00:03
(7/14): libcgroup-0.41-21.el7.x86_64.rpm | 66 kB 00:00:00
(8/14): libsemanage-python-2.5-14.el7.x86_64.rpm | 113 kB 00:00:00
(9/14): policycoreutils-2.5-34.el7.x86_64.rpm | 917 kB 00:00:00
(10/14): python-IPy-0.75-6.el7.noarch.rpm | 32 kB 00:00:00
(11/14): policycoreutils-python-2.5-34.el7.x86_64.rpm | 457 kB 00:00:00
(12/14): setools-libs-3.3.8-4.el7.x86_64.rpm | 620 kB 00:00:00
(13/14): containerd.io-1.3.7-3.1.el7.x86_64.rpm | 29 MB 00:00:07
(14/14): docker-ce-cli-19.03.13-3.el7.x86_64.rpm | 38 MB 00:00:29
------------------------------------------------------------------------------------------------------------------------
Total 2.8 MB/s | 94 MB 00:00:33
Retrieving key from https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
Importing GPG key 0x621E9F35:
Userid : "Docker Release (CE rpm) <docker@docker.com>"
Fingerprint: 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
From : https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Updating : audit-libs-2.8.5-4.el7.x86_64 1/17
Updating : policycoreutils-2.5-34.el7.x86_64 2/17
Installing : libcgroup-0.41-21.el7.x86_64 3/17
Installing : audit-libs-python-2.8.5-4.el7.x86_64 4/17
Installing : setools-libs-3.3.8-4.el7.x86_64 5/17
Installing : libsemanage-python-2.5-14.el7.x86_64 6/17
Installing : checkpolicy-2.5-8.el7.x86_64 7/17
Installing : python-IPy-0.75-6.el7.noarch 8/17
Installing : policycoreutils-python-2.5-34.el7.x86_64 9/17
Installing : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch 10/17
setsebool: SELinux is disabled.
Installing : containerd.io-1.3.7-3.1.el7.x86_64 11/17
Installing : 1:docker-ce-cli-19.03.13-3.el7.x86_64 12/17
Installing : 3:docker-ce-19.03.13-3.el7.x86_64 13/17
Updating : audit-2.8.5-4.el7.x86_64 14/17
Cleanup : policycoreutils-2.5-29.el7.x86_64 15/17
Cleanup : audit-2.8.4-4.el7.x86_64 16/17
Cleanup : audit-libs-2.8.4-4.el7.x86_64 17/17
Verifying : audit-libs-2.8.5-4.el7.x86_64 1/17
Verifying : audit-2.8.5-4.el7.x86_64 2/17
Verifying : containerd.io-1.3.7-3.1.el7.x86_64 3/17
Verifying : policycoreutils-2.5-34.el7.x86_64 4/17
Verifying : 3:docker-ce-19.03.13-3.el7.x86_64 5/17
Verifying : 1:docker-ce-cli-19.03.13-3.el7.x86_64 6/17
Verifying : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch 7/17
Verifying : python-IPy-0.75-6.el7.noarch 8/17
Verifying : checkpolicy-2.5-8.el7.x86_64 9/17
Verifying : policycoreutils-python-2.5-34.el7.x86_64 10/17
Verifying : libsemanage-python-2.5-14.el7.x86_64 11/17
Verifying : setools-libs-3.3.8-4.el7.x86_64 12/17
Verifying : audit-libs-python-2.8.5-4.el7.x86_64 13/17
Verifying : libcgroup-0.41-21.el7.x86_64 14/17
Verifying : policycoreutils-2.5-29.el7.x86_64 15/17
Verifying : audit-libs-2.8.4-4.el7.x86_64 16/17
Verifying : audit-2.8.4-4.el7.x86_64 17/17
Installed:
containerd.io.x86_64 0:1.3.7-3.1.el7 docker-ce.x86_64 3:19.03.13-3.el7 docker-ce-cli.x86_64 1:19.03.13-3.el7
Dependency Installed:
audit-libs-python.x86_64 0:2.8.5-4.el7 checkpolicy.x86_64 0:2.5-8.el7
container-selinux.noarch 2:2.119.2-1.911c772.el7_8 libcgroup.x86_64 0:0.41-21.el7
libsemanage-python.x86_64 0:2.5-14.el7 policycoreutils-python.x86_64 0:2.5-34.el7
python-IPy.noarch 0:0.75-6.el7 setools-libs.x86_64 0:3.3.8-4.el7
Dependency Updated:
audit.x86_64 0:2.8.5-4.el7 audit-libs.x86_64 0:2.8.5-4.el7 policycoreutils.x86_64 0:2.5-34.el7
Complete!
5.启动docker
[root@JD ~]# systemctl start docker
6.查看Docker版本
[root@JD ~]# docker version
Client: Docker Engine - Community
Version: 19.03.13
API version: 1.40
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:03:45 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.13
API version: 1.40 (minimum version 1.12)
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:02:21 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.3.7
GitCommit: 8fba4e9a7d01810a393d5d25a3621dc101981175
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
7.运行hello-world
[root@JD ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete Digest: sha256:4cf9c47f86df71d48364001ede3a4fcd85ae80ce02ebad74156906caff5378bc
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/
8.查看hello-world下载的镜像
[root@JD ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 9 months ago 13.3kB
9.卸载Docker
[root@JD ~]# yum remove docker-ce docker-ce-cli containerd.io
[root@JD ~]# rm -rf /var/lib/docker
10.Docker配置阿里云镜像加速器
登录阿里云找到镜像加速器:
执行上图配置镜像减速器里面命令即可:
[root@JD docker]# sudo mkdir -p /etc/docker
[root@JD docker]# ls
key.json
[root@JD docker]# sudo tee /etc/docker/daemon.json <<-'EOF'
> {
> "registry-mirrors": ["https://vzxmlftm.mirror.aliyuncs.com"]
> }
> EOF
{
"registry-mirrors": ["https://vzxmlftm.mirror.aliyuncs.com"]
}
[root@JD docker]# sudo systemctl daemon-reload
[root@JD docker]# systemctl restart docker
常见问题
1.Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
直接下面命令即可解决。
[root@JD ~]# systemctl daemon-reload
[root@JD ~]# systemctl start docker
[root@JD ~]# docker run hello-world