docker笔记

Centos7 安装docker

  • 卸载旧版本

  • 旧版本的 Docker 称为 docker 或者 docker-engine,使用以下命令卸载旧版本:

1
2
3
4
5
6
7
8
9
10
[root@VM-16-13-centos ~]#  sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine

使用 yum 安装

执行以下命令安装依赖包:

1
[root@VM-16-13-centos ~]#  sudo yum install -y yum-utils

安装

1
2
3
4
5
6
7
8
9
[root@VM-16-13-centos ~]# sudo yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@VM-16-13-centos ~]# sudo sed -i 's/download.docker.com/mirrors.aliyun.com\/docker-ce/g' /etc/yum.repos.d/docker-ce.repo
## 注意可能会有安装完 没有/etc/docker/daemon.json的情况
# 官方源
# $ sudo yum-config-manager \
# --add-repo \
# https://download.docker.com/linux/centos/docker-ce.repo
  • 更新 yum 软件源缓存,并安装 docker-ce

    1
    [root@VM-16-13-centos ~]# sudo yum install docker-ce docker-ce-cli containerd.io

Centos7 手动安装遇到问题:

1
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
  • 解决方法

进入 /etc/docker,没有daemon.json文件就自己新建一个:

l
1
2
3
[root@VM-16-13-centos ~]# cd /etc/docker
[root@VM-16-13-centos docker]# ls ## 注意用别的方法安装的 这个地方也可能没有daemon.json
key.json daemon.json
  • 编辑daemon.json文件:地址替换为自己想用的 建议用国内镜像
1
2
3
4
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}

  • 重启docker
1
systemctl restart docker.service

脚本安装

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
###### centos7脚本安装 docker
[root@VM-16-13-centos ~]# curl -fsSL get.docker.com -o get-docker.sh #安装 脚本
[root@VM-16-13-centos ~]# sudo sh get-docker.sh --mirror Aliyun #使用阿里云镜像
# Executing docker install script, commit: 93d2499759296ac1f9c510605fef85052a2c32be
+ sh -c 'yum install -y -q yum-utils'
Package yum-utils-1.1.31-54.el7_8.noarch already installed and latest version
+ sh -c 'yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo'
Loaded plugins: fastestmirror, langpacks
adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file https://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
+ '[' stable '!=' stable ']'
+ sh -c 'yum makecache'
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
docker-ce-stable | 3.5 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
os | 3.6 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/13): docker-ce-stable/7/x86_64/updateinfo | 55 B 00:00:00
(2/13): docker-ce-stable/7/x86_64/primary_db | 75 kB 00:00:00
(3/13): docker-ce-stable/7/x86_64/filelists_db | 31 kB 00:00:00
(4/13): docker-ce-stable/7/x86_64/other_db | 123 kB 00:00:00
(5/13): epel/7/x86_64/prestodelta | 4.5 kB 00:00:00
(6/13): extras/7/x86_64/filelists_db | 277 kB 00:00:00
(7/13): extras/7/x86_64/other_db | 147 kB 00:00:00
(8/13): epel/7/x86_64/other_db | 3.4 MB 00:00:00
(9/13): os/7/x86_64/filelists_db | 7.2 MB 00:00:00
(10/13): os/7/x86_64/other_db | 2.6 MB 00:00:00
(11/13): updates/7/x86_64/other_db | 1.0 MB 00:00:00
(12/13): updates/7/x86_64/filelists_db | 7.8 MB 00:00:00
(13/13): epel/7/x86_64/filelists_db | 12 MB 00:00:01
Metadata Cache Created
+ '[' -n '' ']'
+ sh -c 'yum install -y -q docker-ce'
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-ce-20.10.14-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Public key for docker-ce-20.10.14-3.el7.x86_64.rpm is not installed
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
Warning: RPMDB altered outside of yum.
setsebool: SELinux is disabled.
+ version_gte 20.10
+ '[' -z '' ']'
+ return 0
+ sh -c 'yum install -y -q docker-ce-rootless-extras'
Package docker-ce-rootless-extras-20.10.14-3.el7.x86_64 already installed and latest version

================================================================================

To run Docker as a non-privileged user, consider setting up the
Docker daemon in rootless mode for your user:

dockerd-rootless-setuptool.sh install

Visit https://docs.docker.com/go/rootless/ to learn about rootless mode.


To run the Docker daemon as a fully privileged service, but granting non-root
users access, refer to https://docs.docker.com/go/daemon-access/

WARNING: Access to the remote API on a privileged Docker daemon is equivalent
to root access on the host. Refer to the 'Docker daemon attack surface'
documentation for details: https://docs.docker.com/go/attack-surface/

================================================================================

常见操作

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
[root@VM-16-13-centos ~]# systemctl enable docker   #启动docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@VM-16-13-centos ~]# systemctl start docker #启动docker
[root@VM-16-13-centos ~]# docker pull hello-world #从 Docker 镜像仓库获取镜像
Using default tag: latest
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:bfea6278a0a267fad2634554f4f0c6f31981eea41c553fdf5a83e95a41d40c38
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
[root@VM-16-13-centos ~]# docker image ls #列出镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 6 months ago 13.3kB
[root@VM-16-13-centos ~]# docker system df #查看镜像、容器、数据卷所占用的空间。
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 1 0 13.26kB 13.26kB (100%)
Containers 0 0 0B 0B
Local Volumes 0 0 0B 0B
Build Cache 0 0 0B 0B
[root@VM-16-13-centos ~]# docker image ls -f dangling=true #查看虚悬镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@VM-16-13-centos ~]# docker image prune #删除虚悬镜像
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
#### 再顺便多拉两个镜像
[root@VM-16-13-centos ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
c229119241af: Pull complete
2215908dc0a2: Pull complete
08c3cb2073f1: Pull complete
18f38162c0ce: Pull complete
10e2168f148a: Pull complete
c4ffe9532b5f: Pull complete
Digest: sha256:2275af0f20d71b293916f1958f8497f987b8d8fd8113df54635f2a5915002bf1
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@VM-16-13-centos ~]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
c229119241af: Already exists
bed41eb8190a: Pull complete
5e59eaa723f1: Pull complete
fd5ad7669819: Pull complete
566c064eef6e: Pull complete
20c7cfac25de: Pull complete
Digest: sha256:69a3ab2516b560690e37197b71bc61ba245aafe4525ebdece1d8a0bc5669e3e2
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
[root@VM-16-13-centos ~]# docker image ls -a #-a 显示 中间层镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest bba24acba395 8 days ago 113MB
nginx latest 12766a6745ee 8 days ago 142MB
hello-world latest feb5d9fea6a5 6 months ago 13.3kB
[root@VM-16-13-centos ~]# docker image ls hello #列出部分镜像 要不写出全称 要不 用通配符
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@VM-16-13-centos ~]# docker image ls hello* # 通配符
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 6 months ago 13.3kB
[root@VM-16-13-centos ~]# docker image ls -q #以特定格式显示
bba24acba395
12766a6745ee
feb5d9fea6a5
[root@VM-16-13-centos ~]# docker image ls --format "{{.ID}}: {{.Repository}}"
bba24acba395: redis
12766a6745ee: nginx
feb5d9fea6a5: hello-world
[root@VM-16-13-centos ~]# docker image ls --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}"
IMAGE ID REPOSITORY TAG
bba24acba395 redis latest
12766a6745ee nginx latest
feb5d9fea6a5 hello-world latest