docker 安装
环境要求
本次使用的是云服务器,版本是 centos,要求版本在3.10以上
[root@iZbp15293q8kgzhur7n6kvZ /]# uname -r
3.10.0-1160.108.1.el7.x86_64
[root@iZbp15293q8kgzhur7n6kvZ /]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
安装 docker
- 官方安装参考手册:https://docs.docker.com/engine/install/centos/
- 保证环境是满足要求的
- 卸载以前旧的 docker
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 安装 gcc 环境
yum -y install gcc
yum -y install gcc-c++
- 安装 docker 需要的仓库地址配置
yum install -y yum-utils
# download.docker.com 很卡,不推荐使用
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 在这里就要使用国内的镜像。
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
- 安装 docker
# 更新 yum 索引
yum makecache fast
# 安装 docker
yum install -y docker-ce docker-ce-cli containerd.io
- 查看 docker 是否安装成功
# 查看 docker 版本
[root@iZbp15293q8kgzhur7n6kvZ /]# docker -v
Docker version 25.0.4, build 1a576c5
- 启动docker
systemctl start docker
- 下载并运行 hello-world 镜像
docker run hello-world
[root@iZbp15293q8kgzhur7n6kvZ /]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:6352af1ab4ba4b138648f8ee88e63331aae519946d3b67dae50c313c6fc8200f
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 已经启动成功了
卸载 docker
如果以后想要卸载 docker,执行以下命令
# 停止docker服务
systemctl stop docker
# 删除docker
yum remove docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
# 删除docker产生的文件夹
rm -rf /var/lib/docker
rm -rf /var/lib/containerd
阿里云镜像加速
在阿里云工作台中搜索容器镜像服务 ACR,每个人的镜像加速器都是不同的,执行以下命令即可
了解 hello-world 做了什么
docker run 镜像名称
- 寻找镜像(如果本地存在,则通过镜像启动容器,不存在,去公开仓库 dockerhub 寻找镜像)
- 下载镜像(如果公开仓库 dockerhub 存在这个镜像,拉取到本地,然后执行,不存在,就直接报错)
下载一个不存在的镜像
# 随便输入一个不存在的镜像,找不到就报错
[root@iZbp15293q8kgzhur7n6kvZ /]# docker run asjfdhakjshfdajshfakjsfhd
Unable to find image 'asjfdhakjshfdajshfakjsfhd:latest' locally
docker: Error response from daemon: pull access denied for asjfdhakjshfdajshfakjsfhd, repository does not exist or may require 'docker login': denied: requested access to the resource is denied.
See 'docker run --help'.
查看已经本地下载好的镜像
docker images
[root@iZbp15293q8kgzhur7n6kvZ /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d2c94e258dcb 10 months ago 13.3kB