一、简介

Docker 镜像是容器运行的基础,它包含了容器运行所需的文件系统、应用程序及其依赖。镜像是不可变的,每次修改都会生成一个新的镜像。以下是对 Docker 镜像操作的详细介绍,包括常用的命令及其参数解释。

二、dockers-ce 部署

1、基于Rocky(类红帽系)

https://developer.aliyun.com/mirror/docker-ce/?spm=a2c6h.25603864.0.0.12da0poJESO

dnf -y install dnf-plugins-core

yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo --配源 添加软件源信息

yum install docker-ce  --版本较新

systemctl enable --now docker --起服务

docker version --验证版本

2、基于openEuler

dnf -y install dnf-plugins-core

dnf config-manager --add-repo  https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

vim /etc/yum.repos.d/docker-ce.repo  --将配置文件内的变量$releaserver批量改为8(:% s/$releaserver/8/)

yum -y install docker-ce

systemctl enable --now docker --起服务

docker version --验证版本

三、镜像操作

准备(配置docker镜像加速)

使用国内镜像源
你可以通过配置Docker的daemon.json文件来使用国内的镜像源,这样可以加快镜像的下载速度。以下是一些可用的国内镜像源:

DaoCloud 镜像站:https://docker.m.daocloud.io
网易云:https://hub-mirror.c.163.com
百度云:https://mirror.baidubce.com
南京大学镜像站:https://docker.nju.edu.cn


还有一些代理或三方的镜像源,在最新的更新中被标记为正常工作。不过,实际速度可能会因地区、网络条件以及镜像源的负载情况而有所不同。建议可以根据自己的实际情况,选择一个或几个进行测试,以确定哪个镜像源在您所在地区的速度最快。

docker.unsee.tech
dockerpull.org
docker.1panel.live
dockerhub.icu

具体配置

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": [
        "https://docker.m.daocloud.io",
        "https://hub-mirror.c.163.com",
        "https://mirror.baidubce.com",
        "https://docker.nju.edu.cn"
    ]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

(一)拉取镜像

基本命令

docker pull <镜像名>:<标签>

参数解释

docker pull:从远程仓库拉取镜像。

<镜像名>:镜像的名称,例如 nginx、ubuntu。

<标签>:镜像的版本或变体,例如 latest、1.20、alpine。如果未指定标签,默认为 latest。

示例

docker pull nginx:latest --从 Docker Hub 拉取最新版本的 nginx 镜像。

(二)查看本地镜像

 基本命令

docker images

参数解释
docker images:列出本地所有镜像。

-a:显示所有镜像,包括中间层镜像。

--filter:根据条件过滤镜像,例如按标签、仓库等。

--format:自定义输出格式。

示例

docker images

输出示例:
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        latest    1234567890ab   1 week ago      133MB
ubuntu       20.04     0987654321cd   2 months ago    72MB


(三)删除镜像

 基本命令

docker rmi <镜像ID或名称>:<标签>

参数解释
docker rmi:删除本地镜像。

<镜像ID或名称>:镜像的唯一标识符或名称。

<标签>:镜像的标签。如果未指定标签,默认删除所有标签的镜像。

 示例

docker rmi nginx:latest --删除本地的 nginx:latest 镜像。

(四)构建镜像

基本命令

docker build -t <镜像名>:<标签> <上下文路径>

 参数解释
docker build:从 Dockerfile 构建镜像。

-t:指定构建后的镜像名称和标签。

<镜像名>:<标签>:镜像名称和标签,例如 myapp:1.0。

<上下文路径>:Dockerfile 所在路径或上下文目录,默认为当前目录。

 示例

docker build -t myapp:1.0 .
--在当前目录下构建镜像,命名为 myapp,标签为 1.0。

(五)导出和加载镜像

1、导出镜像

docker save <镜像名>:<标签> > <文件名>.tar

参数解释
docker save:将镜像导出为 .tar 文件。

<镜像名>:<标签>:指定要导出的镜像。

>:将输出重定向到文件。

2. 加载镜像

docker load -i <文件名>.tar

 参数解释
docker load:从 .tar 文件加载镜像。

-i:指定输入文件路径。

 示例

docker save nginx:latest > nginx.tar
docker load -i nginx.tar


(六)查看镜像历史

 基本命令

docker history <镜像名>:<标签>

参数解释
docker history:查看镜像的构建历史。

<镜像名>:<标签>:指定镜像。

示例

docker history nginx:latest

输出示例:

IMAGE ID       CREATED         CREATED BY                                      SIZE
1234567890ab   1 week ago      /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon of... 0B
<missing>      1 week ago      /bin/sh -c #(nop) EXPOSE 80/tcp                 0B
<missing>      1 week ago      /bin/sh -c #(nop) ENTRYPOINT ["/docker-entryp... 0B
<missing>      1 week ago      /bin/sh -c #(nop) COPY file:abc123... in /      133MB


(七)镜像标签管理

重新标记镜像

docker tag <原镜像名>:<原标签> <新镜像名>:<新标签>

参数解释
docker tag:为镜像重新标记。

<原镜像名>:<原标签>:原镜像的名称和标签。

<新镜像名>:<新标签>:新的镜像名称和标签。

 示例

docker tag nginx:latest my-nginx:1.0
--将 nginx:latest 重新标记为 my-nginx:1.0。

(八)推送镜像到远程仓库

基本命令

docker push <镜像名>:<标签>

参数解释
docker push:将本地镜像推送到远程仓库(如 Docker Hub、私有仓库)。

<镜像名>:<标签>:镜像名称和标签。如果镜像属于私有仓库,需要包含仓库地址,例如 myregistry.com/myapp:1.0。

示例

docker push myapp:1.0
--将本地的 myapp:1.0 镜像推送到 Docker Hub。

四、总结

Docker 镜像操作是 Docker 使用的核心内容之一。通过掌握以下命令,可以高效地管理镜像:

拉取镜像:docker pull

查看本地镜像:docker images

删除镜像:docker rmi

构建镜像:docker build

导出和加载镜像:docker save 和 docker load

查看镜像历史:docker history

重新标记镜像:docker tag

推送镜像到远程仓库:docker push

合理使用这些命令,可以帮助开发者和运维人员更好地管理镜像,确保应用的高效部署和维护。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐