Docker 包括三个基本概念:
镜像(Image):Docker 镜像(Image),就相当于是一个 root 文件系统。比如官方镜像 ubuntu:16.04 就包含了完整的一套 Ubuntu16.04 最小系统的 root 文件系统。
容器(Container):镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。
仓库(Repository):仓库可看成一个代码控制中心,用来保存镜像。
Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器。
Docker 容器通过 Docker 镜像来创建。
Centos7.5及以上
sudo uname -r
3.10.0-862.el7.x86_64
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
添加阿里yum源
[root@huamin14 ~] cd /etc/yum.repos.d/
[root@huamin14 yum.repos.d]# wget http://mirrors.aliyun.com/repo/Centos-7.repo
[root@huamin14 yum.repos.d]# ls
Centos-7.repo
#清除一下原来的yum缓存
[root@huamin14 yum.repos.d] yum clean all
#重新生成新的yum缓存
[root@huamin14 yum.repos.d] yum makecache
注意新创建的虚拟机环境 可能没有wget命令,所以需要yum -y install wget 安装wget
对此不了解的可以看这篇关于yum源的配置
点击跳转
添加阿里云镜像仓库
安装yum-utils包(提供yum-config-manager 实用程序)并设置稳定存储库。
[root@huamin14 ~] yum -y install yum-utils
[root@huamin14 ~] yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#更新软件包索引
[root@huamin14 ~] yum makecache fast
[root@huamin14 ~] yum -y install docker-ce docker-ce-cli containerd.io
#启动docker
[root@huamin14 ~] systemctl start docker
#查看docker版本
[root@huamin14 ~] docker version
Client: Docker Engine - Community
Version: 20.10.8
API version: 1.41
Go version: go1.16.6
Git commit: 3967b7d
Built: Fri Jul 30 19:55:49 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.8
API version: 1.41 (minimum version 1.12)
Go version: go1.16.6
Git commit: 75249d8
Built: Fri Jul 30 19:54:13 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.9
GitCommit: e25210fe30a0a703442421b0f60afac609f950a3
runc:
Version: 1.0.1
GitCommit: v1.0.1-0-g4144b63
docker-init:
Version: 0.19.0
GitCommit: de40ad0
测试docker,启动docker的 hello-world镜像
以下说明hello-world镜像正常启动
[root@huamin14 ~] docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e
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/
查看docker镜像
[root@huamin14 ~] docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 5 months ago 13.3kB
3.配置使用
安装docker后会自动创建这个文件夹和key.json文件
[root@huamin14 ~] cd /etc/docker/
[root@huamin14 docker] ls
key.json
[root@huamin14 docker] tee /etc/docker/daemon.json <<-'EOF'
> {
> "registry-mirrors": ["https://ghfmhfl6.mirror.aliyuncs.com"]
> }
> EOF
{
"registry-mirrors": ["https://ghfmhfl6.mirror.aliyuncs.com"]
}
#刷新配置文件
[root@huamin14 docker] systemctl daemon-reload
#重启docker
[root@huamin14 docker] systemctl restart docker
# 卸载依赖
[root@huamin14 ~] yum remove docker-ce docker-ce-cli containerd.io
# 删除docker资源目录
# /var/lib/docker docker的默认工作路径
[root@huamin14 ~] rm -rf /var/lib/docker
[root@huamin14 ~] rm -rf /var/lib/containerd