首页 > 其他分享 >【Docker】镜像的创建、管理与发布

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

时间:2023-10-02 11:32:09浏览次数:36  
标签:创建 image redis hello --- 镜像 Docker docker


镜像的获取

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

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

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

镜像的基本操作

跟镜像相关的命令如下:

$ 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<[email protected]>

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<[email protected]>
 ---> 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


标签:创建,image,redis,hello,---,镜像,Docker,docker
From: https://blog.51cto.com/morris131/7682457

相关文章

  • NO.8 静态库+动态库的创建(Linux环境)
    一、动态库/共享库(sharedlibrary)共享库的命名:共享库的命名一般分为三个部分:前缀:lib库名称:自己定义即可,如test后缀:.so所以最终的静态库的名字应该为:libtest.so 共享库的制作:步骤1:生成目标文件.o,此时要加编译选项:-fPIC(fpic)gcc......
  • 描绘几个我想创建的程序
    在学习编程的过程中老师建议我们描述几个我想创建的编程,这里介绍几个我的想法。1.广告拦截器。现在我们打开很多软件都会被一大群烦人的广告所困扰,而且跳过广告的显示不明显还极易误触,很大程度上干扰了我们的工作和生活。我希望开发一个可自动帮我们点击跳过广告的编程,以方便我......
  • git远程空仓库创建以及本地项目关联远程仓库
    远程服务器上创建空仓库远程服务器:进入git仓库目录:cd/home/gitmkdirjava-test.gitcdjava-test.gitgitinit--barechown-Rgit:gitjava-test.git本地项目:cd 项目目录gitinitgitcheckout-b develop在项目里面,添加readme.md写一点内容gitadd.gitcommit......
  • Nginx简介与Docker Compose部署指南
    Nginx是一款高性能的开源Web服务器和反向代-理服务器,以其卓越的性能、可伸缩性和灵活性而闻名。它在全球范围内广泛用于托管Web应用程序、负载均衡、反向代-理和更多场景中。在本文中,我们将首先介绍Nginx的基本概念,然后演示如何使用DockerCompose轻松部署Nginx服务器。什么是Nginx......
  • 联想个人云开启Docker并安装常用的Docker
    联想个人云开启Docker并安装常用的Docker启用Docker利用联想个人云的远程协助功能开启SSH并提升普通用户权限后,可安装(开启Docker)新版的固件docker已经安装了,不知道出于什么原因没有启动:[~]#dockerimagelsCannotconnecttotheDockerdaemonatunix:///var/run/docker.s......
  • 镜像源地址
    博客地址:https://www.cnblogs.com/zylyehuo/清华:https://pypi.tuna.tsinghua.edu.cn/simple中国科技大学https://pypi.mirrors.ustc.edu.cn/simple/华中理工大学:http://pypi.hustunique.com/山东理工大学:http://pypi.sdutlinux.org/豆瓣:http://pypi.douban.com/simple/......
  • SpringCloud微服务学习笔记(二)【Feign,Gateway,Docker】
    Feign先来看我们以前利用RestTemplate发起远程调用的代码:存在下面的问题:•代码可读性差,编程体验不统一•参数复杂URL难以维护Feign是一个声明式的http客户端,官方地址:https://github.com/OpenFeign/feign其作用就是帮助我们优雅的实现http请求的发送,解决上面提到的问题。基......
  • driverindexer 是一个用于管理Windows系统驱动程序的工具。它被用于创建、修改和维护W
    driverindexer是一个用于管理Windows系统驱动程序的工具。它被用于创建、修改和维护Windows操作系统的驱动程序索引。驱动程序索引是一个记录系统中所有已安装驱动程序信息的数据库。它包含了驱动程序的名称、文件路径、版本号等详细信息。Windows系统通过查询驱动程序索引来查......
  • 线程的三种创建方式
    一、线程的三种创建方式:1、继承Tread类,重写run方法;2、实现 Runnable接口,实现run方法;3、实现Callable接口,实现call方法,该方式可以获取线程的执行结果。二、继承Tread类1、创建步骤:创建一个继承于Thread类的子类重写Thread类的run()方法->此线程执行的操作声明在......
  • vmware创建虚拟机后必备操作
    时区设置成东八区rm-f/etc/localtimeln-s/usr/share/zoneinfo/Asia/Shanghai/etc/localtime桥接模式联网#VMWareWorkstation->虚拟机->设置->网络适配器->选择“桥接模式”->确定#如果设置之前,虚拟机已经启动,那么重启虚拟机。#配置开机后激活网口vim......