前言
虚拟机在进行操作前建议进行一下快照,以防安装过程中把虚拟的服务器搞乱
安装yum-utils 工具
用于增加国内yum源仓库
yum install -y yum-utils
设置 yum 仓库地址
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
执行过程:
[root@agljc ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
已加载插件:fastestmirror, langpacks
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
更新 yum 缓存
yum makecache fast #提前在本地缓存一份,用来提高搜索安装软件的速度
开始安装
yum install -y docker-ce docker-ce-cli containerd.io
docker-ce : docker 引擎,社区版。 需要 docker-ce-cli。
docker-ce-cli : docker 引擎的命令行界面,社区版
containerd.io : 守护进程 containerd。 它在 docker 包上独立工作,并且是 docker 包所必需的。
docker信息查看
启动后 执行docker info可以看到如下信息
Client:
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.10.2
Path: /usr/libexec/docker/cli-plugins/docker-buildx
compose: Docker Compose (Docker Inc.)
Version: v2.16.0
Path: /usr/libexec/docker/cli-plugins/docker-compose
scan: Docker Scan (Docker Inc.)
Version: v0.23.0
Path: /usr/libexec/docker/cli-plugins/docker-scan
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 23.0.1
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Using metacopy: false
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 2456e983eb9e37e47538f59ea18f2043c9a73640
runc version: v1.1.4-0-g5fd4c4d
init version: de40ad0
Security Options:
seccomp
Profile: builtin
Kernel Version: 3.10.0-957.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.683GiB
Name: agljc.com
ID: 57aecf58-1bac-422c-aa33-7e1ed84094cc
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
127.0.0.0/8
Docker Root Dir: /var/lib/docker
这里我们需要提前避坑,将这个默认的安装路径软链接到数据盘
systemctl stop docker #停止服务
mv /var/lib/docker/ /data/
ln -s /data/docker /var/lib/docker
ll /var/lib/docker
lrwxrwxrwx. 1 root root 12 3月 2 23:09 /var/lib/docker -> /data/docker
systemctl start docker
docker操作
systemctl start docker #启动
systemctl status docker #docker服务状态查看
systemctl status docker #停止
systemctl enable docker #设置为开启自启
systemctl disable docker #禁止开机启动
测试
[root@agljc docker]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:6e8b6f026e0b9c419ea0fd02d3905dd0952ad1feea67543f525c73a0a790fefb
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/
此时测试成功
[root@agljc docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 17 months ago 13.3kB
此时我们执行上面的命令可以看到下载下来的镜像
阿里云镜像加速
找到/etc/docker/daemon.json文件并修改,添加代码如下,如果在/etc/docker下找不到daemon.json文件,可以自己新建
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn",
"http://hub-mirror.c.163.com"
]
}
重启服务:
systemctl daemon-reload
systemctl restart docker
安装成功
此时我们已经将docker安装成功,接下来我们可以使用docker安装我们经常使用的测试环境例如MySQL、Redis、MQ等