Docker镜像管理篇
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.Docker镜像概述
1>.什么是docker镜像
Docker镜像含有启动容器所需要的文件系统及所需要的内容,因此镜像主要用于创建并启动docker容器。
Docker镜像含里面是一层层文件系统,叫做联合文件系统(Union FS),联合文件系统开源将几层目录挂载到一起,形成一个虚拟文件系统,虚拟文件系统的目录结构就像普通的Linux系统的目录结构一样,docker通过这些文件再加上宿主机的内核提供了一个Linux的虚拟环境,每一层文件系统我们叫做layer。 联合文件系统(Union FS)可以对每一层(layer)文件系统设置三种权限,即只读(readonly),读写(readwrite)和写出(whiteout-able)。严格来说,docker镜像中每一层文件系统都是只读的或者说是禁止直接修改,因为大家都知道容器是基于镜像启动的,如果A,B用户都基于同一个镜像启动的容器进行开发,如果镜像内容不一致会导致两个人拿到的初始代码不一致! 构建镜像的时候,从一个最基础的操作系统开始,每个构建的操作都相当于做一层的修改,增加了一层文件系统,一层层网上叠加,上层的修改会覆盖底层该位置的可见性。这很容易理解,就像上层把底层遮住了一样,当使用镜像的时候,我们只会看到一个完全的整体,不知道里面有几层文件系统也不需要知道里面有几层,如下图所示。
2>.容器,镜像和父镜像关系
3>.docker镜像(image)和Linux文件系统对比
一个典型的Linux文件系统由bootfs(全称为"boot file system")和rootfs("root file system")两部分组成。bootfs主要包含bootloader和kernel,bootloader主要用于引导加载kernel,当kernel被加载到内存中后bootfs会被卸载(umount)掉。rootfs包含就是典型Linux系统中的"/bin","/dev","/etc","/home","/proc"等标准目录和文件。
docker image中最基础的两层结构,不同的Linux发行版(如Ubuntu和CentOS)在rootfs这一层会有所区别,但是对于docker镜像通常都比较小,官方提供的CentOS基础镜像在220MB左右,一些其它版本镜像甚至不到10MB,docker镜像直接调用宿主机的内核,镜像中只提供rootfs,也就是只需要包括最基本的命令,工具和程序库就可以了,比如alpine镜像仅有5.59M左右。
如下图所示,两个不同的docker image在同一个宿主机内核上实现不同的rootfs。
二.Docker镜像的管理命令
docker命令是常使用的docker客户端命令,其后面可以加不同的参数以实现响应的功能,常用的镜像管理相关命令如下。
[[email protected] ~]# 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. [[email protected] ~]# [[email protected] ~]#
1>.搜索镜像
[[email protected] ~]# docker search --help Usage: docker search [OPTIONS] TERM Search the Docker Hub for images Options: -f, --filter filter Filter output based on conditions provided --format string Pretty-print search using a Go template --limit int Max number of search results (default 25) --no-trunc Don't truncate output [[email protected] ~]# [[email protected] ~]#
[[email protected] ~]# docker search alpine NAME DESCRIPTION STARS OFFICIAL AUTOMATED alpine A minimal Docker image based on Alpine Linux… 5995 [OK] mhart/alpine-node Minimal Node.js built on Alpine Linux 452 anapsix/alpine-java Oracle Java 8 (and 7) with GLIBC 2.28 over A… 439 [OK] frolvlad/alpine-glibc Alpine Docker image with glibc (~12MB) 228 [OK] gliderlabs/alpine Image based on Alpine Linux will help you wi… 181 alpine/git A simple git container running in alpine li… 112 [OK] mvertes/alpine-mongo light MongoDB container 108 [OK] yobasystems/alpine-mariadb MariaDB running on Alpine Linux [docker] [am… 57 [OK] kiasaki/alpine-postgres PostgreSQL docker image based on Alpine Linux 45 [OK] alpine/socat Run socat command in alpine container 44 [OK] davidcaste/alpine-tomcat Apache Tomcat 7/8 using Oracle Java 7/8 with… 40 [OK] zzrot/alpine-caddy Caddy Server Docker Container running on Alp… 35 [OK] easypi/alpine-arm AlpineLinux for RaspberryPi 32 jfloff/alpine-python A small, more complete, Python Docker image … 32 [OK] byrnedo/alpine-curl Alpine linux with curl installed and set as … 28 [OK] hermsi/alpine-sshd Dockerize your OpenSSH-server with rsync and… 26 [OK] etopian/alpine-php-wordpress Alpine WordPress Nginx PHP-FPM WP-CLI 23 [OK] hermsi/alpine-fpm-php Dockerize your FPM PHP 7.4 upon a lightweigh… 22 [OK] zenika/alpine-chrome Chrome running in headless mode in a tiny Al… 16 [OK] bashell/alpine-bash Alpine Linux with /bin/bash as a default she… 14 [OK] davidcaste/alpine-java-unlimited-jce Oracle Java 8 (and 7) with GLIBC 2.21 over A… 13 [OK] spotify/alpine Alpine image with `bash` and `curl`. 9 [OK] tenstartups/alpine Alpine linux base docker image with useful p… 9 [OK] roribio16/alpine-sqs Dockerized ElasticMQ server + web UI over Al… 7 [OK] cfmanteiga/alpine-bash-curl-jq Docker Alpine image with Bash, curl and jq p… 5 [OK] [[email protected] ~]# [[email protected] ~]#
2>.下载镜像
[[email protected] ~]# docker pull --help Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST] Pull an image or a repository from a registry Options: -a, --all-tags Download all tagged images in the repository --disable-content-trust Skip image verification (default true) [[email protected] ~]#
[[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest c7460dfcab50 4 days ago 126MB [[email protected] ~]# [[email protected] ~]# docker pull alpine Using default tag: latest latest: Pulling from library/alpine e6b0cf9c0882: Pull complete Digest: sha256:2171658620155679240babee0a7714f6509fae66898db422ad803b951257db78 Status: Downloaded newer image for alpine:latest [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest c7460dfcab50 4 days ago 126MB alpine latest cc0abc535e36 2 weeks ago 5.59MB [[email protected] ~]# [[email protected] ~]#
[[email protected] ~]# docker run -it --rm alpine #如果想要单次运行容器,当容器结束时该容器的数据默认也会一并删除掉。 / # / # cat /etc/issue Welcome to Alpine Linux 3.11 Kernel \r on an \m (\l) / # / # apk add vim #安装vim工具 fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz (1/6) Installing xxd (8.2.0-r0) (2/6) Installing lua5.3-libs (5.3.5-r2) (3/6) Installing ncurses-terminfo-base (6.1_p20191130-r0) (4/6) Installing ncurses-terminfo (6.1_p20191130-r0) (5/6) Installing ncurses-libs (6.1_p20191130-r0) (6/6) Installing vim (8.2.0-r0) Executing busybox-1.31.1-r8.trigger OK: 42 MiB in 20 packages / # / # apk add nginx #安装nginx工具 (1/3) Installing pcre (8.43-r0) (2/3) Installing nginx (1.16.1-r4) Executing nginx-1.16.1-r4.pre-install (3/3) Installing nginx-vim (1.16.1-r4) Executing busybox-1.31.1-r8.trigger OK: 43 MiB in 23 packages / # / # netstat -natlp Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 172.17.0.4:43840 151.101.228.249:80 TIME_WAIT - / # / # mkdir /run/nginx / # / # nginx #启动nginx / # / # netstat -natlp Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 22/nginx: master pr tcp 0 0 172.17.0.4:43840 151.101.228.249:80 TIME_WAIT - tcp 0 0 :::80 :::* LISTEN 22/nginx: master pr / # / # exit [[email protected] ~]# [[email protected] ~]#
3>.查看本地镜像
[[email protected] ~]# 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. [[email protected] ~]# [[email protected] ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest c7460dfcab50 4 days ago 126MB alpine latest cc0abc535e36 2 weeks ago 5.59MB [[email protected] ~]# [[email protected] ~]#
[[email protected] ~]# docker images --help Usage: docker images [OPTIONS] [REPOSITORY[:TAG]] List images Options: -a, --all Show all images (default hides intermediate images) --digests Show digests -f, --filter filter Filter output based on conditions provided --format string Pretty-print images using a Go template --no-trunc Don't truncate output -q, --quiet Only show numeric IDs [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest c7460dfcab50 4 days ago 126MB alpine latest cc0abc535e36 2 weeks ago 5.59MB [[email protected] ~]# [[email protected] ~]#
4>.查看镜像的历史
[[email protected] ~]# docker history --help Usage: docker history [OPTIONS] IMAGE Show the history of an image Options: --format string Pretty-print images using a Go template -H, --human Print sizes and dates in human readable format (default true) --no-trunc Don't truncate output -q, --quiet Only show numeric IDs [[email protected] ~]#
5>.镜像导出(镜像的导出使用命令临时导出可以,在生产环境不推荐使用,生产环境应该用私有仓库管理批量的镜像导出)
[[email protected] ~]# docker save --help Usage: docker save [OPTIONS] IMAGE [IMAGE...] Save one or more images to a tar archive (streamed to STDOUT by default) Options: -o, --output string Write to a file, instead of STDOUT [[email protected] ~]# [[email protected] ~]#
[[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest c7460dfcab50 4 days ago 126MB alpine latest cc0abc535e36 2 weeks ago 5.59MB [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# ll /mnt/ total 0 [[email protected] ~]# [[email protected] ~]# docker save nginx -o /mnt/nginx.tar.gz #使用"-o"选项显式指定导出镜像到指定位置 [[email protected] ~]# [[email protected] ~]# ll /mnt/ total 127124 -rw-------. 1 root root 130174464 Jan 14 17:21 nginx.tar.gz [[email protected] ~]# [[email protected] ~]#
[[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest c7460dfcab50 4 days ago 126MB alpine latest cc0abc535e36 2 weeks ago 5.59MB [[email protected] ~]# [[email protected] ~]# ll /mnt/ total 127124 -rw-------. 1 root root 130174464 Jan 14 17:21 nginx.tar.gz [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# docker save nginx > /mnt/nginx-2020.tar.gz #也可以使用系统的重定向功能导出镜像文件。 [[email protected] ~]# [[email protected] ~]# ll /mnt/ total 254248 -rw-r--r--. 1 root root 130174464 Jan 14 17:23 nginx-2020.tar.gz -rw-------. 1 root root 130174464 Jan 14 17:21 nginx.tar.gz [[email protected] ~]# [[email protected] ~]#
[[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest c7460dfcab50 4 days ago 126MB alpine latest cc0abc535e36 2 weeks ago 5.59MB [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# ll /mnt/ total 0 [[email protected] ~]# [[email protected] ~]# docker save nginx -o /mnt/nginx.tar.gz [[email protected] ~]# [[email protected] ~]# ll /mnt/ total 127124 -rw-------. 1 root root 130174464 Jan 14 17:21 nginx.tar.gz [[email protected] ~]# [[email protected] ~]# tar -xf /mnt/nginx-2020.tar.gz -C /mnt/ #解压导出的配置的镜像文件,我们可以查看该镜像的组织结构。 tar: manifest.json: implausibly old time stamp 1970-01-01 08:00:00 tar: repositories: implausibly old time stamp 1970-01-01 08:00:00 [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# ll /mnt/ total 254264 drwxr-xr-x. 2 root root 50 Jan 10 06:20 bc0c1f0bf5d3d116df7db18438f547181e0695612a45744f7964713c1126cebc -rw-r--r--. 1 root root 6670 Jan 10 06:20 c7460dfcab502275e9c842588df406444069c00a48d9a995619c243079a4c2f7.json drwxr-xr-x. 2 root root 50 Jan 10 06:20 da4b42b1c7856aeb6c959188b9f628c0f928bec556dac7f1c87853e2519f3d5e drwxr-xr-x. 2 root root 50 Jan 10 06:20 fb21d4be2316838d12beecf48b8d7e832e099e2ac44d8ecd5b3e61180fbb7c9d -rw-r--r--. 1 root root 355 Jan 1 1970 manifest.json -rw-r--r--. 1 root root 130174464 Jan 14 17:23 nginx-2020.tar.gz -rw-------. 1 root root 130174464 Jan 14 17:21 nginx.tar.gz -rw-r--r--. 1 root root 88 Jan 1 1970 repositories [[email protected] ~]# [[email protected] ~]# cat /mnt/repositories #记录了nginx镜像的版本号 {"nginx":{"latest":"da4b42b1c7856aeb6c959188b9f628c0f928bec556dac7f1c87853e2519f3d5e"}} [[email protected] ~]# [[email protected] ~]# cat /mnt/manifest.json #包含了镜像的相关配置,如配置文件(使用"Config"指定),镜像的版本号(使用"RepoTags"指定),分层信息(使用"Layers"指定,很明显该数组中有三个元素,说明有存储了三个分层的信息,继续往下看可探索究竟)。 [{"Config":"c7460dfcab502275e9c842588df406444069c00a48d9a995619c243079a4c2f7.json","RepoTags":["nginx:latest"],"Layers":["bc0c1f0bf5d3d116df7db18438f547181e0695612a45744f7964713c1126cebc/layer.tar","fb21d4be2316838d12beecf48b8d7e832e099e2ac44d8ecd5b3e61180fbb7c9d/layer. tar","da4b42b1c7856aeb6c959188b9f628c0f928bec556dac7f1c87853e2519f3d5e/layer.tar"]}] [[email protected] ~]# [[email protected] ~]# ll /mnt/bc0c1f0bf5d3d116df7db18438f547181e0695612a45744f7964713c1126cebc/ total 70792 -rw-r--r--. 1 root root 406 Jan 10 06:20 json -rw-r--r--. 1 root root 72479232 Jan 10 06:20 layer.tar -rw-r--r--. 1 root root 3 Jan 10 06:20 VERSION [[email protected] ~]# [[email protected] ~]# ll /mnt/da4b42b1c7856aeb6c959188b9f628c0f928bec556dac7f1c87853e2519f3d5e/ total 12 -rw-r--r--. 1 root root 1680 Jan 10 06:20 json -rw-r--r--. 1 root root 3584 Jan 10 06:20 layer.tar -rw-r--r--. 1 root root 3 Jan 10 06:20 VERSION [[email protected] ~]# [[email protected] ~]# ll /mnt/fb21d4be2316838d12beecf48b8d7e832e099e2ac44d8ecd5b3e61180fbb7c9d/ total 56328 -rw-r--r--. 1 root root 482 Jan 10 06:20 json -rw-r--r--. 1 root root 57670144 Jan 10 06:20 layer.tar -rw-r--r--. 1 root root 3 Jan 10 06:20 VERSION [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# mkdir /mnt/fb21d4be2316838d12beecf48b8d7e832e099e2ac44d8ecd5b3e61180fbb7c9d/layer [[email protected] ~]# [[email protected] ~]# tar -xf /mnt/fb21d4be2316838d12beecf48b8d7e832e099e2ac44d8ecd5b3e61180fbb7c9d/layer.tar -C /mnt/fb21d4be2316838d12beecf48b8d7e832e099e2ac44d8ecd5b3e61180fbb7c9d/layer [[email protected] ~]# [[email protected] ~]# ll /mnt/fb21d4be2316838d12beecf48b8d7e832e099e2ac44d8ecd5b3e61180fbb7c9d/layer total 4 drwxr-xr-x. 16 root root 4096 Jan 10 06:20 etc drwxr-xr-x. 5 root root 56 Jan 10 06:20 lib drwxrwxrwt. 2 root root 6 Jan 10 06:20 tmp drwxr-xr-x. 7 root root 66 Dec 24 08:00 usr drwxr-xr-x. 5 root root 41 Dec 24 08:00 var [[email protected] ~]# [[email protected] ~]# mkdir /mnt/da4b42b1c7856aeb6c959188b9f628c0f928bec556dac7f1c87853e2519f3d5e/layer [[email protected] ~]# [[email protected] ~]# tar -xf /mnt/da4b42b1c7856aeb6c959188b9f628c0f928bec556dac7f1c87853e2519f3d5e/layer.tar -C /mnt/da4b42b1c7856aeb6c959188b9f628c0f928bec556dac7f1c87853e2519f3d5e/layer [[email protected] ~]# [[email protected] ~]# ll /mnt/da4b42b1c7856aeb6c959188b9f628c0f928bec556dac7f1c87853e2519f3d5e/layer total 0 drwxr-xr-x. 3 root root 17 Dec 24 08:00 var [[email protected] ~]# [[email protected] ~]# mkdir /mnt/bc0c1f0bf5d3d116df7db18438f547181e0695612a45744f7964713c1126cebc/layer [[email protected] ~]# [[email protected] ~]# tar -xf /mnt/bc0c1f0bf5d3d116df7db18438f547181e0695612a45744f7964713c1126cebc/layer.tar -C /mnt/bc0c1f0bf5d3d116df7db18438f547181e0695612a45744f7964713c1126cebc/layer [[email protected] ~]# [[email protected] ~]# ll /mnt/bc0c1f0bf5d3d116df7db18438f547181e0695612a45744f7964713c1126cebc/layer total 12 drwxr-xr-x. 2 root root 4096 Dec 24 08:00 bin drwxr-xr-x. 2 root root 6 Nov 10 20:17 boot drwxr-xr-x. 2 root root 6 Dec 24 08:00 dev drwxr-xr-x. 28 root root 4096 Dec 24 08:00 etc drwxr-xr-x. 2 root root 6 Nov 10 20:17 home drwxr-xr-x. 7 root root 85 Dec 24 08:00 lib drwxr-xr-x. 2 root root 34 Dec 24 08:00 lib64 drwxr-xr-x. 2 root root 6 Dec 24 08:00 media drwxr-xr-x. 2 root root 6 Dec 24 08:00 mnt drwxr-xr-x. 2 root root 6 Dec 24 08:00 opt drwxr-xr-x. 2 root root 6 Nov 10 20:17 proc drwx------. 2 root root 37 Dec 24 08:00 root drwxr-xr-x. 3 root root 30 Dec 24 08:00 run drwxr-xr-x. 2 root root 4096 Dec 24 08:00 sbin drwxr-xr-x. 2 root root 6 Dec 24 08:00 srv drwxr-xr-x. 2 root root 6 Nov 10 20:17 sys drwxrwxrwt. 2 root root 6 Dec 24 08:00 tmp drwxr-xr-x. 10 root root 105 Dec 24 08:00 usr drwxr-xr-x. 11 root root 139 Dec 24 08:00 var [[email protected] ~]# [[email protected] ~]# ll /mnt/da4b42b1c7856aeb6c959188b9f628c0f928bec556dac7f1c87853e2519f3d5e/layer/var/ total 0 drwxr-xr-x. 3 root root 19 Jan 10 06:20 log [[email protected] ~]# [[email protected] ~]# ll /mnt/bc0c1f0bf5d3d116df7db18438f547181e0695612a45744f7964713c1126cebc/layer/var/ total 0 drwxr-xr-x. 2 root root 6 Nov 10 20:17 backups drwxr-xr-x. 5 root root 48 Dec 24 08:00 cache drwxr-xr-x. 7 root root 67 Dec 24 08:00 lib drwxrwsr-x. 2 root ftp 6 Nov 10 20:17 local lrwxrwxrwx. 1 root root 9 Dec 24 08:00 lock -> /run/lock drwxr-xr-x. 3 root root 71 Dec 24 08:00 log drwxrwsr-x. 2 root mem 6 Dec 24 08:00 mail drwxr-xr-x. 2 root root 6 Dec 24 08:00 opt lrwxrwxrwx. 1 root root 4 Dec 24 08:00 run -> /run drwxr-xr-x. 2 root root 18 Dec 24 08:00 spool drwxrwxrwt. 2 root root 6 Nov 10 20:17 tmp [[email protected] ~]# [[email protected] ~]# ll /mnt/fb21d4be2316838d12beecf48b8d7e832e099e2ac44d8ecd5b3e61180fbb7c9d/layer/var/ total 0 drwxr-xr-x. 6 root root 61 Jan 10 06:20 cache drwxr-xr-x. 6 root root 55 Jan 10 06:20 lib drwxr-xr-x. 4 root root 76 Jan 10 06:20 log [[email protected] ~]# [[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest c7460dfcab50 4 days ago 126MB alpine latest cc0abc535e36 2 weeks ago 5.59MB [[email protected] ~]# [[email protected] ~]# docker run -it --rm nginx bash root@18e69ea2b98f:/# root@18e69ea2b98f:/# ls -l / total 8 drwxr-xr-x. 2 root root 4096 Dec 24 00:00 bin drwxr-xr-x. 2 root root 6 Nov 10 12:17 boot drwxr-xr-x. 5 root root 360 Jan 14 09:48 dev drwxr-xr-x. 1 root root 66 Jan 14 09:48 etc drwxr-xr-x. 2 root root 6 Nov 10 12:17 home drwxr-xr-x. 1 root root 56 Jan 9 22:20 lib drwxr-xr-x. 2 root root 34 Dec 24 00:00 lib64 drwxr-xr-x. 2 root root 6 Dec 24 00:00 media drwxr-xr-x. 2 root root 6 Dec 24 00:00 mnt drwxr-xr-x. 2 root root 6 Dec 24 00:00 opt dr-xr-xr-x. 136 root root 0 Jan 14 09:48 proc drwx------. 2 root root 37 Dec 24 00:00 root drwxr-xr-x. 3 root root 30 Dec 24 00:00 run drwxr-xr-x. 2 root root 4096 Dec 24 00:00 sbin drwxr-xr-x. 2 root root 6 Dec 24 00:00 srv dr-xr-xr-x. 13 root root 0 Jan 13 08:35 sys drwxrwxrwt. 1 root root 6 Jan 9 22:20 tmp drwxr-xr-x. 1 root root 66 Dec 24 00:00 usr drwxr-xr-x. 1 root root 17 Dec 24 00:00 var root@18e69ea2b98f:/# root@18e69ea2b98f:/# root@18e69ea2b98f:/# ls -l /var/ #不难发现,容器中我们能看到的目录或文件,是上面三个分层文件对应的"/var"目录总和。因此我们在实际生活中压根就不关心有多少个分层,而是关心的镜像的使用。 total 0 drwxr-xr-x. 2 root root 6 Nov 10 12:17 backups drwxr-xr-x. 1 root root 61 Jan 9 22:20 cache drwxr-xr-x. 1 root root 55 Jan 9 22:20 lib drwxrwsr-x. 2 root staff 6 Nov 10 12:17 local lrwxrwxrwx. 1 root root 9 Dec 24 00:00 lock -> /run/lock drwxr-xr-x. 1 root root 19 Jan 9 22:20 log drwxrwsr-x. 2 root mail 6 Dec 24 00:00 mail drwxr-xr-x. 2 root root 6 Dec 24 00:00 opt lrwxrwxrwx. 1 root root 4 Dec 24 00:00 run -> /run drwxr-xr-x. 2 root root 18 Dec 24 00:00 spool drwxrwxrwt. 2 root root 6 Nov 10 12:17 tmp root@18e69ea2b98f:/# root@18e69ea2b98f:/# exit exit [[email protected] ~]#
6>.镜像导入(镜像的导入使用命令临时导入可以,但在生产环境不推荐使用,生产环境应该用私有仓库管理批量的镜像导入)
[[email protected] ~]# docker load --help Usage: docker load [OPTIONS] Load an image from a tar archive or STDIN Options: -i, --input string Read from tar archive file, instead of STDIN -q, --quiet Suppress the load output [[email protected] ~]#
[[email protected] ~]# docker images #查看当前宿主机现有的镜像 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest c7460dfcab50 4 days ago 126MB alpine latest cc0abc535e36 2 weeks ago 5.59MB [[email protected] ~]# [[email protected] ~]# ll /mnt/ total 0 [[email protected] ~]# [[email protected] ~]# docker save nginx -o /mnt/nginx.tar.gz #我选择将nginx的镜像导出 [[email protected] ~]# [[email protected] ~]# ll /mnt/ total 127124 -rw-------. 1 root root 130174464 Jan 14 19:38 nginx.tar.gz [[email protected] ~]# [[email protected] ~]# hostname -i 192.168.6.201 [[email protected] ~]# [[email protected] ~]# scp /mnt/nginx.tar.gz docker202.yinzhengjie.org.cn:~ #将导出的镜像拷贝到其它宿主机上 The authenticity of host 'docker202.yinzhengjie.org.cn (192.168.6.202)' can't be established. ECDSA key fingerprint is SHA256:pCZRyla5kyGXvY13DnVliTHvaDFovhdVv9KflRDHuNw. ECDSA key fingerprint is MD5:30:3c:4f:ca:9d:f8:40:6e:30:4d:ce:40:32:64:46:e8. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'docker202.yinzhengjie.org.cn,192.168.6.202' (ECDSA) to the list of known hosts. [email protected]'s password: nginx.tar.gz 100% 124MB 124.1MB/s 00:01 [[email protected] ~]# [[email protected] ~]# ssh docker202.yinzhengjie.org.cn #远程到docker202.yinzhengjie.org.cn便于演示docker的 [email protected]'s password: Last login: Mon Jan 13 17:01:25 2020 from 192.168.0.1 [[email protected] ~]# [[email protected] ~]# hostname -i 192.168.6.202 [[email protected] ~]# [[email protected] ~]# docker --version #查看当前docker的版本 Docker version 19.03.5, build 633a0ea [[email protected] ~]# [[email protected] ~]# ll total 127124 -rw-------. 1 root root 130174464 Jan 14 19:39 nginx.tar.gz [[email protected] ~]# [[email protected] ~]# docker images #很明显当前宿主机是没有docker镜像的 REPOSITORY TAG IMAGE ID CREATED SIZE [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# docker load -i nginx.tar.gz #我们将nginx镜像导入到当前宿主机上 556c5fb0d91b: Loading layer [==================================================>] 72.48MB/72.48MB 17fde96446df: Loading layer [==================================================>] 57.67MB/57.67MB c26e88311e71: Loading layer [==================================================>] 3.584kB/3.584kB Loaded image: nginx:latest [[email protected] ~]# [[email protected] ~]# docker images #镜像导入成功啦 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest c7460dfcab50 4 days ago 126MB [[email protected] ~]#
[[email protected] ~]# docker images #查看当前宿主机现有的镜像 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest c7460dfcab50 4 days ago 126MB alpine latest cc0abc535e36 2 weeks ago 5.59MB [[email protected] ~]# [[email protected] ~]# ll /mnt/ total 0 [[email protected] ~]# [[email protected] ~]# docker save nginx -o /mnt/nginx.tar.gz #我选择将nginx的镜像导出 [[email protected] ~]# [[email protected] ~]# ll /mnt/ total 127124 -rw-------. 1 root root 130174464 Jan 14 19:38 nginx.tar.gz [[email protected] ~]# [[email protected] ~]# hostname -i 192.168.6.201 [[email protected] ~]# [[email protected] ~]# scp /mnt/nginx.tar.gz docker202.yinzhengjie.org.cn:~ #将导出的镜像拷贝到其它宿主机上 The authenticity of host 'docker202.yinzhengjie.org.cn (192.168.6.202)' can't be established. ECDSA key fingerprint is SHA256:pCZRyla5kyGXvY13DnVliTHvaDFovhdVv9KflRDHuNw. ECDSA key fingerprint is MD5:30:3c:4f:ca:9d:f8:40:6e:30:4d:ce:40:32:64:46:e8. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'docker202.yinzhengjie.org.cn,192.168.6.202' (ECDSA) to the list of known hosts. [email protected]'s password: nginx.tar.gz 100% 124MB 124.1MB/s 00:01 [[email protected] ~]# [[email protected] ~]# ssh docker202.yinzhengjie.org.cn #远程到docker202.yinzhengjie.org.cn便于演示docker的 [email protected]'s password: Last login: Mon Jan 13 17:01:25 2020 from 192.168.0.1 [[email protected] ~]# [[email protected] ~]# hostname -i 192.168.6.202 [[email protected] ~]# [[email protected] ~]# docker --version #查看当前docker的版本 Docker version 19.03.5, build 633a0ea [[email protected] ~]# [[email protected] ~]# ll total 127124 -rw-------. 1 root root 130174464 Jan 14 19:39 nginx.tar.gz [[email protected] ~]# [[email protected] ~]# docker images #很明显当前宿主机是没有docker镜像的 REPOSITORY TAG IMAGE ID CREATED SIZE [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# docker load < nginx.tar.gz #我们也可以使用系统的标准输入来导入镜像哟~ 556c5fb0d91b: Loading layer [==================================================>] 72.48MB/72.48MB 17fde96446df: Loading layer [==================================================>] 57.67MB/57.67MB c26e88311e71: Loading layer [==================================================>] 3.584kB/3.584kB Loaded image: nginx:latest [[email protected] ~]# [[email protected] ~]# docker images #镜像导入成功啦 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest c7460dfcab50 4 days ago 126MB [[email protected] ~]#
7>.删除镜像(温馨提示:通过镜像启动容器时镜像不能被删除,除非将容器全部关闭)
[[email protected] ~]# docker image rm --help Usage: docker image rm [OPTIONS] IMAGE [IMAGE...] Remove one or more images Aliases: rm, rmi, remove Options: -f, --force Force removal of the image --no-prune Do not delete untagged parents [[email protected] ~]#
[[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest c7460dfcab50 4 days ago 126MB [[email protected] ~]# [[email protected] ~]# docker image rm -f nginx:latest Untagged: nginx:latest Deleted: sha256:c7460dfcab502275e9c842588df406444069c00a48d9a995619c243079a4c2f7 Deleted: sha256:3e51598e49c550f8b212a07c6ff2ed47a09eeb637f67d1b3c5468e9a8ee646e3 Deleted: sha256:a8b9a5643b3cc8082997d3d2fbaf4b53213ff80aa4169226be8b3768ae6e3605 Deleted: sha256:556c5fb0d91b726083a8ce42e2faaed99f11bc68d3f70e2c7bbce87e7e0b3e10 [[email protected] ~]# [[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE [[email protected] ~]# [[email protected] ~]#
[[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest c7460dfcab50 4 days ago 126MB [[email protected] ~]# [[email protected] ~]# docker image rmi nginx #使用rmr别名删除镜像。 Untagged: nginx:latest Deleted: sha256:c7460dfcab502275e9c842588df406444069c00a48d9a995619c243079a4c2f7 Deleted: sha256:3e51598e49c550f8b212a07c6ff2ed47a09eeb637f67d1b3c5468e9a8ee646e3 Deleted: sha256:a8b9a5643b3cc8082997d3d2fbaf4b53213ff80aa4169226be8b3768ae6e3605 Deleted: sha256:556c5fb0d91b726083a8ce42e2faaed99f11bc68d3f70e2c7bbce87e7e0b3e10 [[email protected] ~]# [[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE [[email protected] ~]# [[email protected] ~]#
[[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest c7460dfcab50 4 days ago 126MB [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# docker image remove nginx #使用remove别名来删除镜像 Untagged: nginx:latest Deleted: sha256:c7460dfcab502275e9c842588df406444069c00a48d9a995619c243079a4c2f7 Deleted: sha256:3e51598e49c550f8b212a07c6ff2ed47a09eeb637f67d1b3c5468e9a8ee646e3 Deleted: sha256:a8b9a5643b3cc8082997d3d2fbaf4b53213ff80aa4169226be8b3768ae6e3605 Deleted: sha256:556c5fb0d91b726083a8ce42e2faaed99f11bc68d3f70e2c7bbce87e7e0b3e10 [[email protected] ~]# [[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE [[email protected] ~]# [[email protected] ~]#
[[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest c7460dfcab50 4 days ago 126MB [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# docker image rm nginx #也可以不是使用选项和版本号就删除镜像啦~ Untagged: nginx:latest Deleted: sha256:c7460dfcab502275e9c842588df406444069c00a48d9a995619c243079a4c2f7 Deleted: sha256:3e51598e49c550f8b212a07c6ff2ed47a09eeb637f67d1b3c5468e9a8ee646e3 Deleted: sha256:a8b9a5643b3cc8082997d3d2fbaf4b53213ff80aa4169226be8b3768ae6e3605 Deleted: sha256:556c5fb0d91b726083a8ce42e2faaed99f11bc68d3f70e2c7bbce87e7e0b3e10 [[email protected] ~]# [[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE [[email protected] ~]# [[email protected] ~]#
三.制作Docker镜像
博主推荐阅读: https://www.cnblogs.com/yinzhengjie/p/12190111.html