【Docker】镜像的创建、管理与发布

news2024/9/23 9:22:39

镜像的获取

镜像可以从以下方式获得:

  • 从远程镜像仓库拉取,可以是公有仓库,也可以是私有仓库
  • 从Dockerfile构建
  • 从文件导入(离线)
  • 从容器提交

1653292048113.png

镜像的基本操作

跟镜像相关的命令如下:

$ docker image --help

Usage:  docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Run 'docker image COMMAND --help' for more information on a command.

docker image build

docker image build命令用于从Docker文件构建镜像。

一个简单的构建redis镜像的dockerfile如下:

from centos

maintainer morris131<morris131@163.com>

env WORK_DIR /usr/local

workdir $WORK_DIR

run yum install -y wget gcc gcc-c++ automake autoconf libtool make

run wget https://download.redis.io/releases/redis-6.2.5.tar.gz

run tar xzf redis-6.2.5.tar.gz && cd redis-6.2.5 && make

expose 6379

env REDIS_HOME $WORK_DIR/redis-6.2.5

env PATH $PATH:$REDIS_HOME/src

entrypoint ["redis-server"]

使用docker image build构建镜像:

$ docker image build -f redis.dockerfile -t redis:v6.2 .
Sending build context to Docker daemon  326.3MB
Step 1/11 : from centos
 ---> 5d0da3dc9764
Step 2/11 : maintainer morris131<morris131@163.com>
 ---> Using cache
 ---> 5cda723c8e72
Step 3/11 : env WORK_DIR /usr/local
 ---> Using cache
 ---> c533d0edc8eb
Step 4/11 : workdir $WORK_DIR
 ---> Using cache
 ---> cb590797901e
Step 5/11 : run yum install -y wget gcc gcc-c++ automake autoconf libtool make
 ---> Using cache
 ---> 3ad6eccd91bc
Step 6/11 : run wget https://download.redis.io/releases/redis-6.2.5.tar.gz
 ---> Using cache
 ---> 009fdc775e47
Step 7/11 : run tar xzf redis-6.2.5.tar.gz && cd redis-6.2.5 && make
 ---> Using cache
 ---> 55980a8ed7d4
Step 8/11 : expose 6379
 ---> Using cache
 ---> a0031636b6a8
Step 9/11 : env REDIS_HOME $WORK_DIR/redis-6.2.5
 ---> Using cache
 ---> 9fe2c6f4f2ea
Step 10/11 : env PATH $PATH:$REDIS_HOME/src
 ---> Using cache
 ---> a41d5925a64f
Step 11/11 : entrypoint ["redis-server"]
 ---> Using cache
 ---> dfa712f480f7
Successfully built dfa712f480f7
Successfully tagged redis:v6.2

选项说明:

  • --file, -f:Dockerfile的名称(默认为’PATH/Dockerfile’)
  • --tag, -t:名称和可选的“name:tag”格式的标签
  • path:指定构建镜像时的相对目录,当前目录用.代替,例如dockerfile经常会COPY一些文件,如果这些文件指定的是相对路径,那么这个相对路径是基于path指定的目录。

docker image history

docker image history显示镜像的历史记录,应该是镜像的分层结构。

$ docker image history redis:v6.2
IMAGE          CREATED         CREATED BY                                      SIZE      COMMENT
dfa712f480f7   20 months ago   /bin/sh -c #(nop)  ENTRYPOINT ["redis-server…   0B
a41d5925a64f   20 months ago   /bin/sh -c #(nop)  ENV PATH=/usr/local/sbin:…   0B
9fe2c6f4f2ea   20 months ago   /bin/sh -c #(nop)  ENV REDIS_HOME=/usr/local…   0B
a0031636b6a8   20 months ago   /bin/sh -c #(nop)  EXPOSE 6379                  0B
55980a8ed7d4   20 months ago   /bin/sh -c tar xzf redis-6.2.5.tar.gz && cd …   160MB
009fdc775e47   20 months ago   /bin/sh -c wget https://download.redis.io/re…   2.47MB
3ad6eccd91bc   20 months ago   /bin/sh -c yum install -y wget gcc gcc-c++ a…   270MB
cb590797901e   20 months ago   /bin/sh -c #(nop) WORKDIR /usr/local            0B
c533d0edc8eb   20 months ago   /bin/sh -c #(nop)  ENV WORK_DIR=/usr/local      0B
5cda723c8e72   20 months ago   /bin/sh -c #(nop)  MAINTAINER morris131<morr…   0B
5d0da3dc9764   23 months ago   /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B
<missing>      23 months ago   /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B
<missing>      23 months ago   /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0…   231MB

docker image import

docker image import将导出的压缩容器文件导入为镜像。与docker container export配合使用。

$ docker image import nginx.tar.gz nginx:v2.0
sha256:9b1c17b86c32c5a4fa2e811612dc2242ebb1725ff73391cd38c143d22f09ad87

docker image inspect

docker image inspect显示一个或多个镜像的详细信息。

$ docker image inspect redis:v6.2

docker image save

docker image save将一个或多个镜像保存到tar存档(默认情况下流式传输到STDOUT)。

$ docker image save -o nginx-v1.0.tar nginx:v1.0

docker image load

docker image load从tar存档或STDIN加载镜像。

$ docker image load -i nginx-v1.0.tar
Loaded image: nginx:v1.0

docker image ls

docker image ls列出镜像。

$ docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        v1.0      0b36bf0f81dd   2 days ago      141MB
nginx        latest    f652ca386ed1   21 months ago   141MB

docker image prune

docker image prune清理镜像。

$ docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

后文会详细介绍docker image prunedocker image rm的区别。

docker image rm

docker image rm根据镜像名称或者镜像ID删除一个或多个镜像。

$ docker image rm 0b36bf0f81dd
Untagged: nginx:v1.0
Deleted: sha256:0b36bf0f81dda62c4ced29e84d0b44a7c812bb643b2c1d898df0b8897f58c964

docker image pull

docker image pull默认从官方的远程镜像仓库Docker Hub拉取镜像,如果不指定版本,会默认拉取最新版本,也可以指定远程镜像仓库的地址。

$ docker image pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

指定版本拉取:

$ docker pull nginx:1.20.0

这里使用的是阿里云的镜像服务:https://cr.console.aliyun.com/

需要先登陆阿里云的镜像仓库:

$ docker login --username=xxxx registry.cn-hangzhou.aliyuncs.com
Password:
WARNING! Your password will be stored unencrypted in /home/morris/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

指定镜像仓库拉取:

$ docker pull registry.cn-hangzhou.aliyuncs.com/morris131/mysql:5.7.35

docker image tag

docker image tag为本地镜像打上标签。

$ docker tag nginx:latest registry.cn-hangzhou.aliyuncs.com/morris131/nginx:1.20.0

打上标签后,IMAGE ID不变,这样就无法根据IMAGE ID进行删除镜像,因为一个IMAGE ID指向了多个镜像,可以根据REPOSITORY:TAG进行删除。

docker image push

docker image push将本地镜像推送到远程的镜像仓库。

$ docker push registry.cn-hangzhou.aliyuncs.com/morris131/nginx:1.20.0

镜像下载的加速

由于docker镜像访问很慢,所以使用阿里的镜像加速器。

参考:阿里官方教材

可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器

$ mkdir -p /etc/docker

$ vi /etc/docker/daemon.json
{
  "registry-mirrors": ["https://imjw08kj.mirror.aliyuncs.com"]
}

$ systemctl daemon-reload

$ systemctl restart docker

镜像的清理和删除

镜像的删除:删除某个镜像时,需要指定镜像的ID或者仓库+标签。

镜像的清理:主要用于资源的释放,不需要指定镜像的ID或者仓库+标签,可以清理中间的镜像层、缓存等系统存储空间。

未使用镜像

未使用镜像表示尚未在容器中分配或使用它。使用docker ps -a命令列出所有已退出以及当前正在运行的容器。在这个列表中的镜像都是“使用过的镜像”,而不在这个列表的镜像就是未使用镜像。

虚悬镜像(Dangling Image)

虚悬镜像 (Dangling Image) 指的是仓库名(镜像名)和标签TAG为<none>的镜像。

可以通过下面的命令来查看:

$ docker image ls -f dangling=true
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        <none>    7ab27dbbfbdf   2 years ago   133MB

可以使用docker image prune来清理虚悬镜像镜像。

$ docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: nginx@sha256:ea4560b87ff03479670d15df426f7d02e30cb6340dcd3004cdfc048d6a1d54b4
deleted: sha256:7ab27dbbfbdf4031f0603a4b597cc43031ff883b54f9329f0309c80952dda6f5
deleted: sha256:5b2a9404d052ae4205f6139190fd4b0921ddeff17bf2aaf4ee97f79e1a8242fe
deleted: sha256:03ebf76f0cbf5fd32ca010bb589c2139ce7e44c050fe3de2d77addf4cfd25866
deleted: sha256:0191669d087dce47072254a93fe55cbedd687f27d3798e2260f846e8f8f5729a
deleted: sha256:17651c6a0ba04d31da14ac6a86d8fb3f600883f9e155558e8aad0b94aa6540a2
deleted: sha256:5a673ff4c07a1b606f2ad1fc53697c99c45b0675734ca945e3bb2bd80f43feb8
deleted: sha256:02c055ef67f5904019f43a41ea5f099996d8e7633749b6e606c400526b2c4b33

Total reclaimed space: 133.1MB

批量清除镜像

  • docker image prune:可以清除虚悬镜像。
  • docker image prune -a:可以清除虚悬镜像和未使用镜像。

关于scratch镜像

Scratch是一个空的Docker镜像。

hello.c

#include <stdio.h>
int main()
{
    printf("hello docker\n");
}

编译成一个二进制文件:

$ gcc --static -o hello hello.c
$ ./hello
hello docker

通过scratch来构建一个基础镜像。

hello.dockerfile

FROM scratch
ADD hello /
CMD ["/hello"]

构建后查看镜像的大小为872KB:

$ docker build -t hello -f hello.dockerfile .
Sending build context to Docker daemon  875.5kB
Step 1/3 : FROM scratch
 --->
Step 2/3 : ADD hello /
 ---> a0c817454691
Step 3/3 : CMD ["/hello"]
 ---> Running in ae204eb2c697
Removing intermediate container ae204eb2c697
 ---> 114a8fd42e2f
Successfully built 114a8fd42e2f
Successfully tagged hello:latest

$ docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
hello        latest    114a8fd42e2f   17 hours ago   872kB

查看hello执行文件的大小为872KB,发现镜像的大小和hello执行文件的大小是一样的,这样可以证明Scratch是一个空的Docker镜像。

$ ll --si
total 889k
drwxrwxr-x 2 morris morris 4.1k Sep  4 16:53 ./
drwxrwxr-x 3 morris morris 4.1k Sep  4 16:53 ../
-rwxrwxr-x 1 morris morris 872k Sep  4 16:53 hello*
-rw-rw-r-- 1 morris morris   64 Sep  4 16:52 hello.c
-rw-rw-r-- 1 morris morris   40 Sep  4 16:51 hello.dockerfile

另外可以通过docker history查看镜像只有hello执行文件这一层有size,正好是镜像的大小。

$ docker history hello
IMAGE          CREATED        CREATED BY                                      SIZE      COMMENT
114a8fd42e2f   17 hours ago   /bin/sh -c #(nop)  CMD ["/hello"]               0B
a0c817454691   17 hours ago   /bin/sh -c #(nop) ADD file:198e5a2b23e222ef7…   872kB

使用hello镜像创建一个容器运行:

$ docker container run --name hello hello
hello docker

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/986496.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

linux 文件属性相关

文章目录 文件存储介绍文件颜色含义软链接和硬链接 文件目录操作压缩解压 tar目录切换 cd创建目录 mkdir删除目录 rmdir rm复制文件或目录 cp移动文件或目录 mv查看指定目录下的内容 ls文本查看 cat more tail文本编辑 vim 查找查找文件 find查找文本 grep查找文件 whereis Lin…

语义分割实践思考记录(个人备忘录)

一、任务管理器、NVDIA的GPU利用率显示[1][2] 若需要在任务管理器中查看基于Pytorch框架的GPU利用率&#xff0c;那么&#xff0c;我们需要将监控面板监测内容调整为cuda。图一&#xff08;左&#xff09;即为英伟达命令行工具面板。 图一 英伟达GPU使用率监控 二、基于混淆矩阵…

如何配置Header Editor

。什么是Header Editor 当我们通过网页自带inspect中的network找到返回的response之后&#xff0c;如果需要查看具体的response内容。Header Editor可以自动将response格式美化 。如何配置 1/ 首先需要通过chrome插件安装好Header Editor 2/ 点击网页右上角图标&#xff0c…

HAProxy终结TLS双向认证代理EMQX集群

文章目录 1. 背景介绍2. 系统架构3. 证书签发3.1 创建根证书3.2 创建中间证书3.3 创建设备证书3.4 创建服务端证书 4. HAProxy开启双向认证5. 验证6. 总结 1. 背景介绍 MQTT协议已经成为当前物联网领域的关键技术之一&#xff0c;当前市面上主流的实现MQTT协议的产品主要有 EMQ…

2023年高教社杯数学建模国赛C题详细版思路

C 题 蔬菜类商品的自动定价与补货决策 2023年国赛如期而至&#xff0c;为了方便大家尽快确定选题&#xff0c;这里将对C题进行解题思路说明&#xff0c;以分析C题的主要难点、出题思路以及选择之后可能遇到的难点进行说明&#xff0c;方便大家尽快找到C题的解题思路。 难度排…

第六章 图 三、图的存储结构(邻接表、十字链表、邻接多重表)

一、邻接表法 &#xff08;1&#xff09;无向图 1.定义 与树的孩子表示法相似&#xff0c;以顺序表的数据域为头结点&#xff0c;往后存储一个单向链表&#xff0c;链表内的值表示与此结点相邻的结点。 2.度的计算 只需遍历某一结点的后接链表&#xff0c;就可以算出该节点…

Linux命令200例:Yum强大的包管理工具使用(常用)

&#x1f3c6;作者简介&#xff0c;黑夜开发者&#xff0c;CSDN领军人物&#xff0c;全栈领域优质创作者✌。CSDN专家博主&#xff0c;阿里云社区专家博主&#xff0c;2023年6月csdn上海赛道top4。 &#x1f3c6;数年电商行业从业经验&#xff0c;历任核心研发工程师&#xff0…

【Python小项目之Tkinter应用】随机点名/抽奖工具小优化:实现输入框人数限定与人名显示优化,保证结果人名在窗口内显示,如果内容显示超出则弹出警告窗口

文章目录 前言一、实现思路二、关键代码三、完整代码总结前言 老规矩,先看效果展示: 优化前: 对比来看,当显示人数较多时,优化前的窗口输出框在窗口分辨率不满足显示空间时,会造成人名显示不全的问题,而且只能单行显示,并无任何提示,这样会让用户误认为程序执行正确…

socket的使用 | TCP/IP协议下服务器与客户端之间传送数据

服务器端代码&#xff1a; import java.io.*; import java.net.ServerSocket; import java.net.Socket;public class theServer {public static void main(String[] args) throws IOException {ServerSocket serverSocket new ServerSocket(9999); // 该行代码作用&#xff1…

Win7旗舰版64位桌面创建32位IE方法

很多Win7 64位旗舰版用户系统桌面上的IE8浏览器&#xff0c;打开后都是64位的&#xff0c;而很多网站并不兼容64位的IE浏览器&#xff0c;其实在Win764位系统中IE是分为64位和32位的&#xff0c;出现这样的情况可能是桌面上的IE图标指响的是64位的IE&#xff0c;我们只要重新添…

温控仪的工作原理

温控仪是调控一体化智能温度控制仪表&#xff0c;它采用了全数字化集成设计&#xff0c;具有温度曲线可编程或定点恒温控制、多重PID调节、输出功率限幅曲线编程、手动/自动切换、软启动、报警开关量输出、实时数据查询、与计算机通讯等功能&#xff0c;将数显温度仪表和ZK晶闸…

Discourse 应该保留多少备份

近期&#xff0c;我们在对 Discourse 进行恢复的时候&#xff0c;我们发现新的备份可能会导致不是所有的数据都能恢复到服务上。 这时候我们应该考虑让 Discourse 保留多少备份的问题&#xff1f; 在默认情况下&#xff0c;我们设置 Discourse 的备份是保留 5 个。这是官方的…

c语言初阶指针

目录 何为指针 地址大小 野指针 成因 如何规避 有效性 指针计算 -整数 ​编辑 指针比较运算 指针-指针 ​编辑 数组与指针关系 二级指针 指针数组 应用 何为指针 指针就是指针变量&#xff0c;用来存放内存空间的一个编号&#xff0c;将指针比作我们宾馆的客人&a…

Web存储

目录 什么是 HTML5 Web 存储? 方法 cookie webStorage 会话存储 sessionStorage 本地存储localStorage 什么是 HTML5 Web 存储? 使用HTML5可以在本地存储用户的浏览数据。 早些时候,本地存储使用的是 cookie。但是Web 存储需要更加的安全与快速. 这些数据不会被保存在服…

Go 接口和多态

在讲解具体的接口之前&#xff0c;先看如下问题。 使用面向对象的方式&#xff0c;设计一个加减的计算器 代码如下&#xff1a; package mainimport "fmt"//父类&#xff0c;这是结构体 type Operate struct {num1 intnum2 int }//加法子类&#xff0c;这是结构体…

线性空间和线性变化

目录 考点一、线性空间的基与维数 1、线性空间 2、基底 3、子空间&#xff08;线性子空间&#xff09; ​编辑4、生成子空间 &#xff08;1&#xff09;、v1 n v2 &#xff08;2&#xff09;、v1 v2 5、求和子空间的方法 6、维数定理 7、例题 &#xff08;1&#xf…

解锁前端Vue3宝藏级资料 第四章 VUE常用 UI 库 2 ( ailwind 后台框架)

4.5 ailwind 上面介绍的都是国内比较优秀的UI框架&#xff0c;现在我们在介绍一款国外比较流行的CSS UI框架ailwind 。官方网站https://tailwindcss.com/docs/guides/vite#vue CSShttps://flowbite.com/docs/getting-started/introduction/ 。这个ailwind 架构需要自己去写一些…

《基于区块链的数据资产评估实施指南》技术研讨会成功召开

2023年9月1日&#xff0c;《基于区块链的数据资产评估实施指南》&#xff08;以下简称《指南》&#xff09;技术研讨会在深圳召开&#xff0c;竹云科技作为主要参编单位出席此次研讨会。 中国科协决策咨询首席专家王春晖&#xff0c;中国社会科学院博士于小丽&#xff0c;中国…

Leetcode 易错题整理(三)73. 77. 78. 81. 90. 95.105. 130.

73. 矩阵置零 给定一个 *m* x *n* 的矩阵&#xff0c;如果一个元素为 0 &#xff0c;则将其所在行和列的所有元素都设为 0 。请使用 原地 算法**。** 示例 1&#xff1a; 输入&#xff1a;matrix [[1,1,1],[1,0,1],[1,1,1]] 输出&#xff1a;[[1,0,1],[0,0,0],[1,0,1]]笨方法&…

在很多公司里面会使用打tag的方式保留版本

&#xff1a;git tag|grep "xxx-dev“等分支来查看 2&#xff1a;git cherry-pick XXXXX 然后就是查看有冲突这些 git status 会出现相关的异常 然后解决相关的冲突 git add . git cherry-pick --continue git push XXX HEAD:refs/for/XXX 第一&#xff1a;git ta…