使用Docker repository安装Docker
要安装 Docker Engine,需要 CentOS 7 或 8 的维护版本。
安装yum-utils包
sudo yum install -y yum-utils
创建repository
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
可以选择启用nightly 或者 test repositories.
# 启用nightly repository
sudo yum-config-manager --enable docker-ce-nightly
# 启用test repository
sudo yum-config-manager --enable docker-ce-test
# --disable可以停用相关repository
安装最新版docker引擎
sudo yum install docker-ce docker-ce-cli containerd.io
*安装特定版本Docker引擎
# 查看可安装版本,按字典降序排列
yum list docker-ce --showduplicates | sort -r
sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
启动Docker
sudo systemctl start docker
测试
sudo docker run hello-world
关闭docker
sudo systemctl stop docker.socket
使用rpm包安装(本地安装)
从download.docker.com/linux/centos/centos的版本号/x86_64/stable/Packages/下载rpm包
安装
sudo yum install *.rpm
测试
sudo docker run hello-world
配置阿里云镜像
阿里云镜像加速器网址:
https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
登录阿里云账号
选在相应系统,并按照其操作文档的步骤执行。
执行完所有命令后,运行HelloWorld测试
sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Already exists
Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
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/
run命令逻辑
首先,到本地寻找相关镜像,找到镜像直接运行。
如果本地没有此镜像,会到网络搜索镜像。
当搜索不到时,会报错;当搜索到镜像后,会将其拉取会本地,然后在执行该镜像。
底层原理—为什么Docker会比Vmware虚拟机更快
- docker有着比虚拟机更少的抽象层
docker直接使用物理机的硬件资源,不需要虚拟机实现硬件资源虚拟化。因此在CPU、内存利用率上docker将会在效率上有明显优势。 - docker利用的是宿主机的内核,而不需要加载操作系统OS内核
新建一个docker容器时,docker不需要像虚拟机那样重新加载一个操作系统内核。docker直接利用宿主主机的操作系统,省略了引寻、加载操作系统内核放回等过程,使得docker新建一个容器只需要几秒钟的时间。