安装docker

安装docker

服务器的系统是centos7,所以这里写的是在centos7上安装docker;

可以用以下命令来查看centos版本

1
uname -r
1
2
[root@192 ~]# uname -r
3.10.0-1160.el7.x86_64

确定centos内核版本高于3.10后,接下来开始安装:

1. 安装所需的软件包
yum-utils 提供了 yum-config-manager ,并且 device mapper 存储驱动程序需要 device-mapper- persistent-data 和 lvm2

1
sudo yum install -y yum-utils device-mapper-persistent-data lvm2

2. 添加阿里云Docker仓库配置
创建一个新的仓库文件 /etc/yum.repos.d/docker.repo,内容如下:

1
2
3
4
5
6
7
8
sudo tee /etc/yum.repos.d/docker.repo <<EOF
[docker-ce-stable]
name=Docker CE Stable - \$basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/\$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
EOF

3. 安装Docker

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

4. 启动Docker并设置为开机启动

1
2
sudo systemctl start docker
sudo systemctl enable docker

5. 验证安装是否成功(有client和service两部分表示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
[root@192 ~]# docker version
Client: Docker Engine - Community
Version: 26.1.4
API version: 1.45
Go version: go1.21.11
Git commit: 5650f9b
Built: Wed Jun 5 11:32:04 2024
OS/Arch: linux/amd64
Context: default

Server: Docker Engine - Community
Engine:
Version: 26.1.4
API version: 1.45 (minimum version 1.24)
Go version: go1.21.11
Git commit: de5c9cf
Built: Wed Jun 5 11:31:02 2024
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.33
GitCommit: d2d58213f83a351ca8f528a95fbd145f5654e957
runc:
Version: 1.1.12
GitCommit: v1.1.12-0-g51d5e94
docker-init:
Version: 0.19.0
GitCommit: de40ad0

检查Docker的状态,可以使用以下命令:

1
sudo systemctl status docker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@192 ~]# sudo systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since 二 2024-08-06 15:50:16 IST; 24s ago
Docs: https://docs.docker.com
Main PID: 31511 (dockerd)
CGroup: /system.slice/docker.service
└─31511 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

8月 06 15:50:10 192.168.1.13 systemd[1]: Starting Docker Application Container Engine...
8月 06 15:50:10 192.168.1.13 dockerd[31511]: time="2024-08-06T15:50:10.212305950+05:30" level=info msg="Starting up"
8月 06 15:50:15 192.168.1.13 dockerd[31511]: time="2024-08-06T15:50:15.391483995+05:30" level=info msg="Loading containers: start."
8月 06 15:50:15 192.168.1.13 dockerd[31511]: time="2024-08-06T15:50:15.926383273+05:30" level=info msg="Firewalld: interface docker0 already part of docker zone, returning"
8月 06 15:50:16 192.168.1.13 dockerd[31511]: time="2024-08-06T15:50:16.004835784+05:30" level=info msg="Loading containers: done."
8月 06 15:50:16 192.168.1.13 dockerd[31511]: time="2024-08-06T15:50:16.022793917+05:30" level=info msg="Docker daemon" commit=de5c9cf containerd-snapshotter=false storage-driver=ov...ersion=26.1.4
8月 06 15:50:16 192.168.1.13 dockerd[31511]: time="2024-08-06T15:50:16.022888167+05:30" level=info msg="Daemon has completed initialization"
8月 06 15:50:16 192.168.1.13 dockerd[31511]: time="2024-08-06T15:50:16.048834856+05:30" level=info msg="API listen on /run/docker.sock"
8月 06 15:50:16 192.168.1.13 systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.

至此,docker就安装完成了

设置docker镜向源
解决国内访问Docker Hub的问题

  • 方式一
1
2
3
4
5
6
7
8
9
10
11
12
13
14
sudo mkdir -p /etc/docker

sudo tee /etc/docker/daemon.json <<EOF
{
"registry-mirrors": [
"https://docker.m.daocloud.io",
"https://huecker.io",
"https://dockerhub.timeweb.cloud",
"https://noohub.ru"
]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
  • 方式二

阿里云提供了镜像源:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors 登录后你会获得一个专属的地址使用命令设置国内镜像源:通过vim /etc/docker/daemon.json 进入修改添加 registry-mirrors 内容后重启 Docker

1
"registry-mirrors": ["https://***替换为你的地址***.mirror.aliyuncs.com"]

docker命令参考

查看目前本地docker的镜像

1
docker images

删除docker中的镜像

1
docker rmi 镜像id

删除docker中所有镜像

1
docker rmi $(docker images -q)

查看正在运行的容器

1
docker ps

查看所有容器(包括已停止的)

1
sudo docker ps -a

删除docker中的容器

1
docker rm 容器id

删除docker中所有容器

1
docker rm $(docker ps -aq)

停止容器

1
docker stop 容器id