Docker学习(2) Docker镜像

Docker镜像

1、base镜像

base镜像:一个基础镜像,指完全从零开始构建的镜像,不会依赖其他镜像,甚至会成为被依赖的镜像。

下面为通过docker拉取base镜像,以centos为例

[root@wangliukun ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@wangliukun ~]# docker images centos
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
centos       latest    5d0da3dc9764   2 years ago   231MB
centos       latest    5d0da3dc9764   2 years ago   231MB
[root@wangliukun ~]# 

查看宿主机的kernel版本信息

[root@wangliukun ~]#  uname -r
#查看宿主机kernel版本信息
3.10.0-1160.108.1.el7.x86_64
[root@wangliukun ~]#  docker run -it centos
#jinrucentos base镜像
[root@b9062c6f2de7 /]# uname -r
#查看centos base镜像的kernel版本信息
3.10.0-1160.108.1.el7.x86_64
[root@b9062c6f2de7 /]# 

2、镜像本质

docker镜像只是一个只读的文件系统,有一层一层的文件系统组成,每一层仅包含前一层的差异部分,这种层级文件系统称为UnionFS。

查看UnionFS:15.04镜像的层级结构。

[root@wangliukun ~]# docker pull ubuntu:15.04
15.04: Pulling from library/ubuntu
[DEPRECATION NOTICE] Docker Image Format v1, and Docker Image manifest version 2, schema 1 support will be removed in an upcoming release. Suggest the author of docker.io/library/ubuntu:15.04 to upgrade the image to the OCI Format, or Docker Image manifest v2, schema 2. More information at https://docs.docker.com/go/deprecated-image-specs/
9502adfba7f1: Pull complete 
4332ffb06e4b: Pull complete 
2f937cc07b5f: Pull complete 
a3ed95caeb02: Pull complete 
Digest: sha256:2fb27e433b3ecccea2a14e794875b086711f5d49953ef173d8a03e8707f1510f
#镜像ID号
Status: Downloaded newer image for ubuntu:15.04
docker.io/library/ubuntu:15.04
[root@wangliukun ~]# 

查看镜像

[root@wangliukun ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    d2c94e258dcb   10 months ago   13.3kB
hello-world   latest    feb5d9fea6a5   2 years ago     13.3kB
centos        latest    5d0da3dc9764   2 years ago     231MB
centos        latest    5d0da3dc9764   2 years ago     231MB
#ubuntu镜像
ubuntu        15.04     d1b55fd07600   8 years ago     131MB
#查看Ubuntu镜像构建过程
[root@wangliukun ~]# docker history d1b55fd07600 
IMAGE          CREATED       CREATED BY                                       SIZE      COMMENT
d1b55fd07600   8 years ago   /bin/sh -c #(nop) CMD ["/bin/bash"]              0B        
<missing>      8 years ago   /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$…   1.88kB    
<missing>      8 years ago   /bin/sh -c echo '#!/bin/sh' > /usr/sbin/poli…   701B      
<missing>      8 years ago   /bin/sh -c #(nop) ADD file:3f4708cf445dc1b53…   131MB     
[root@wangliukun ~]# 

3、查找本地镜像

Docker本地镜像通常是存储在服务器上的,验证本地镜像的存储路径

[root@wangliukun ~]# docker info | grep "docker Root Dir"
docker Root Dir: /var/lib/Docker

#显示所有镜像(默认不显示中间层镜像):
[root@wangliukun ~]# docker images -a
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    d2c94e258dcb   10 months ago   13.3kB
hello-world   latest    feb5d9fea6a5   2 years ago     13.3kB
centos        latest    5d0da3dc9764   2 years ago     231MB
centos        latest    5d0da3dc9764   2 years ago     231MB
ubuntu        15.04     d1b55fd07600   8 years ago     131MB
#只显示本地所有镜像id号
[root@wangliukun ~]# docker images -q
d2c94e258dcb
feb5d9fea6a5
5d0da3dc9764
5d0da3dc9764
d1b55fd07600
#使用不截断模式显示完整镜像ID号
[root@wangliukun ~]# docker images --no-trunc
REPOSITORY    TAG       IMAGE ID                                                                  CREATED         SIZE
hello-world   latest    sha256:d2c94e258dcb3c5ac2798d32e1249e42ef01cba4841c2234249495f87264ac5a   10 months ago   13.3kB
hello-world   latest    sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412   2 years ago     13.3kB
centos        latest    sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6   2 years ago     231MB
centos        latest    sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6   2 years ago     231MB
ubuntu        15.04     sha256:d1b55fd07600b2e26d667434f414beee12b0771dfd4a2c7b5ed6f2fc9e683b43   8 years ago     131MB
[root@wangliukun ~]# 

4、构建镜像

docker commit

使用docker commit命令构建镜像

启动Centos镜像,并给Centos的镜像安装vim,设置开机自启

[root@wangliukun ~]# docker run -it centos
[root@b443fa7b5465 /]# yum -y install vim
Failed to set locale, defaulting to C.UTF-8
CentOS Linux 8 - AppStream                              41  B/s |  38  B     00:00    
Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist

下载vim出现问题

原因:yum源停止维护

解决办法:需手动更新yum源

[root@b443fa7b5465 /]# cd /etc/yum.repos.d/
[root@b443fa7b5465 yum.repos.d]# ls
CentOS-Linux-AppStream.repo          CentOS-Linux-FastTrack.repo
CentOS-Linux-BaseOS.repo             CentOS-Linux-HighAvailability.repo
CentOS-Linux-ContinuousRelease.repo  CentOS-Linux-Media.repo
CentOS-Linux-Debuginfo.repo          CentOS-Linux-Plus.repo
CentOS-Linux-Devel.repo              CentOS-Linux-PowerTools.repo
CentOS-Linux-Extras.repo             CentOS-Linux-Sources.repo
[root@b443fa7b5465 yum.repos.d]# mkdir Centos
#将旧yum源文件打包
[root@b443fa7b5465 yum.repos.d]# mv CentOS-Linux-* Centos/
[root@b443fa7b5465 yum.repos.d]# ls
Centos
#下载新的阿里云yum源
[root@b443fa7b5465 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2495  100  2495    0     0   3519      0 --:--:-- --:--:-- --:--:--  3514
#清理缓存
[root@b443fa7b5465 yum.repos.d]# yum makecache
Failed to set locale, defaulting to C.UTF-8
CentOS-8.5.2111 - Base - mirrors.aliyun.com            2.3 MB/s | 4.6 MB     00:02    
CentOS-8.5.2111 - Extras - mirrors.aliyun.com           11 kB/s |  10 kB     00:00    
CentOS-8.5.2111 - AppStream - mirrors.aliyun.com       3.0 MB/s | 8.4 MB     00:02    
Last metadata expiration check: 0:00:01 ago on Mon Mar 11 04:45:53 2024.
Metadata cache created.

重新下载vim

[root@b443fa7b5465 yum.repos.d]# yum -y install vim
Failed to set locale, defaulting to C.UTF-8
Last metadata expiration check: 0:00:29 ago on Mon Mar 11 04:45:53 2024.
Dependencies resolved.
=======================================================================================
 Package               Architecture  Version                    Repository        Size
=======================================================================================
。。。。。。。。

Complete!
[root@b443fa7b5465 yum.repos.d]# 

退出容器

[root@b443fa7b5465 yum.repos.d]# exit
exit
[root@wangliukun ~]# 

使用docker commit命令在Centos镜像基础上创建新的镜像

#此处的'b443fa7b5465 '为刚才操作的镜像ID,只填写前几位即可,确保于其他镜像区分开
[root@wangliukun ~]# docker commit b443fa7b5465 centos/vim
sha256:1ab761fac3cb950119734b530cae48e249aec76b1eeafee73789c40de4f25c1a
[root@wangliukun ~]# 

查看结果

[root@wangliukun ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
centos/vim    latest    1ab761fac3cb   2 minutes ago   298MB
hello-world   latest    d2c94e258dcb   10 months ago   13.3kB
hello-world   latest    feb5d9fea6a5   2 years ago     13.3kB
centos        latest    5d0da3dc9764   2 years ago     231MB
centos        latest    5d0da3dc9764   2 years ago     231MB
ubuntu        15.04     d1b55fd07600   8 years ago     131MB
[root@wangliukun ~]# 

可以发现,新镜像大小298MB,而此前的镜像只有231MB,这是因为在安装vim时安装了许多依赖包

查看镜像中是否自动安装了vim

[root@wangliukun ~]# docker run -it 1ab761fac3cb /bin/bash
[root@6872c6db69a9 /]# which vim
/usr/bin/vim
[root@6872c6db69a9 /]# 

以上代码可以看到新镜像已安装vim

Dockerfile

使用dockerfile构建镜像

镜像基于Dockerfile构建,Dockerfile是一个描述文件,包含若干指令,每条指令都会为基础文件系统创建新的层次结构,正好弥补了Docker commit构建镜像效率低的缺陷

使用Dockerfile创建centos/vim

#在/root下创建Dockerfile
[root@wangliukun ~]# touch Dockerfile
[root@wangliukun ~]# ls
anaconda-ks.cfg  Dockerfile
[root@wangliukun ~]# pwd
/root

#向Dockerfile中写入内容
[root@wangliukun ~]# vim Dockerfile 
#指定源镜像
FROM centos
#指定要执行的命令
RUN mkdir F1
~                           

#运行文件
[root@wangliukun ~]# docker build -t centos/mkdir-dockerfile .
[+] Building 1.5s (6/6) FINISHED                                        docker:default
 => [internal] load build definition from Dockerfile                              0.0s
 => => transferring dockerfile: 124B                                              0.0s
 => [internal] load metadata for docker.io/library/centos:latest                  0.0s
 => [internal] load .dockerignore                                                 0.0s
 => => transferring context: 2B                                                   0.0s
 => CACHED [1/2] FROM docker.io/library/centos:latest                             0.0s
 => [2/2] RUN mkdir F1                                                            1.3s
 => exporting to image                                                            0.1s
 => => exporting layers                                                           0.1s
 => => writing image sha256:515667f5b2be2c1ea8255251ed1233fa70394754fba062a23a1d  0.0s
 => => naming to docker.io/centos/mkdir-dockerfile                                0.0s
 
 #查看是否有成功创建
[root@wangliukun ~]# docker images
REPOSITORY                TAG       IMAGE ID       CREATED             SIZE
centos/mkdir-dockerfile   latest    515667f5b2be   46 seconds ago      231MB
centos/vim                latest    1ab761fac3cb   About an hour ago   298MB
hello-world               latest    d2c94e258dcb   10 months ago       13.3kB
hello-world               latest    feb5d9fea6a5   2 years ago         13.3kB
centos                    latest    5d0da3dc9764   2 years ago         231MB
centos                    latest    5d0da3dc9764   2 years ago         231MB
ubuntu                    15.04     d1b55fd07600   8 years ago         131MB
[root@wangliukun ~]# 

使用docker history 查看镜像结构

[root@wangliukun ~]# docker history centos
IMAGE          CREATED       CREATED BY                                       SIZE      COMMENT
5d0da3dc9764   2 years ago   /bin/sh -c #(nop)  CMD ["/bin/bash"]             0B        
<missing>      2 years ago   /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B        
<missing>      2 years ago   /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0…   231MB     
[root@wangliukun ~]# docker history centos/mkdir-dockerfile 
IMAGE          CREATED         CREATED BY                                       SIZE      COMMENT
515667f5b2be   4 minutes ago   RUN /bin/sh -c mkdir F1 # buildkit               0B        buildkit.dockerfile.v0
<missing>      2 years ago     /bin/sh -c #(nop)  CMD ["/bin/bash"]             0B        
<missing>      2 years ago     /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B        
<missing>      2 years ago     /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0…   231MB    

5、Docker Hub

docker seach 从镜像仓库中查找所需镜像
[root@wangliukun ~]# docker search centos
NAME                               DESCRIPTION                                      STARS     OFFICIAL
centos                             DEPRECATED; The official build of CentOS.        7713      [OK]
kasmweb/centos-7-desktop           CentOS 7 desktop for Kasm Workspaces             43        
bitnami/centos-base-buildpack      Centos base compilation image                    0         
dokken/centos-7                    CentOS 7 image for kitchen-dokken                6         
dokken/centos-8                    CentOS 8 image for kitchen-dokken                6         
spack/centos7                      CentOS 7 with Spack preinstalled                 2         
dokken/centos-6                    EOL: CentOS 6 image for kitchen-dokken           0         
atlas/centos7-atlasos              ATLAS CentOS 7 Software Development OS           2         
spack/centos6                      CentOS 6 with Spack preinstalled                 1         
ustclug/centos                     Official CentOS Image with USTC Mirror           0         
dokken/centos-stream-8                                                              5         
eclipse/centos_jdk8                CentOS, JDK8, Maven 3, git, curl, nmap, mc, …   5         
dokken/centos-stream-9                                                              9         
adoptopenjdk/centos7_build_image                                                    2         
corpusops/centos-bare              https://github.com/corpusops/docker-images/      0         
corpusops/centos                   centos corpusops baseimage                       0         
adoptopenjdk/centos6_build_image                                                    0         
eclipse/centos_go                  Centos + Go                                      0         
spack/centos-stream                                                                 2         
fnndsc/centos-python3              Source for a slim Centos-based Python3 image…   0         
eclipse/centos_spring_boot         Spring boot ready image based on CentOS          0         
openmicroscopy/centos-systemd-ip   centos/systemd with iproute, for testing mul…   0         
eclipse/centos                     CentOS based minimal stack with only git and…   1         
eclipse/centos_nodejs              CentOS based nodejs4 stack                       0         
eclipse/centos_vertx               CentOS + vertx                                   0         
[root@wangliukun ~]# 

列表说明:
NAME:镜像名(镜像仓库源的名称)
DESCRIPTION:对该镜像的描述
STARS:类似 Github 里面的 star,表示点赞、喜欢的意思
OFFICIAL:是否 docker 官方发布
AUTOMATED:是否自动构建。

docker search参数运用

–no-trunc

[root@wangliukun ~]# docker search --no-trunc centos
NAME                               DESCRIPTION                                                             STARS     OFFICIAL
centos                             DEPRECATED; The official build of CentOS.                               7713      [OK]
kasmweb/centos-7-desktop           CentOS 7 desktop for Kasm Workspaces                                    43        
bitnami/centos-base-buildpack      Centos base compilation image                                           0         
dokken/centos-7                    CentOS 7 image for kitchen-dokken                                       6         
dokken/centos-8                    CentOS 8 image for kitchen-dokken                                       6         
spack/centos7                      CentOS 7 with Spack preinstalled                                        2         
dokken/centos-6                    EOL: CentOS 6 image for kitchen-dokken                                  0         
atlas/centos7-atlasos              ATLAS CentOS 7 Software Development OS                                  2         
spack/centos6                      CentOS 6 with Spack preinstalled                                        1         
ustclug/centos                     Official CentOS Image with USTC Mirror                                  0         
dokken/centos-stream-8                                                                                     5         
eclipse/centos_jdk8                CentOS, JDK8, Maven 3, git, curl, nmap, mc, cbuild                      5         
dokken/centos-stream-9                                                                                     9         
adoptopenjdk/centos7_build_image                                                                           2         
corpusops/centos-bare              https://github.com/corpusops/docker-images/                             0         
corpusops/centos                   centos corpusops baseimage                                              0         
adoptopenjdk/centos6_build_image                                                                           0         
eclipse/centos_go                  Centos + Go                                                             0         
spack/centos-stream                                                                                        2         
fnndsc/centos-python3              Source for a slim Centos-based Python3 image .                          0         
eclipse/centos_spring_boot         Spring boot ready image based on CentOS                                 0         
openmicroscopy/centos-systemd-ip   centos/systemd with iproute, for testing multi-node Ansible playbooks   0         
eclipse/centos                     CentOS based minimal stack with only git and openssh                    1         
eclipse/centos_nodejs              CentOS based nodejs4 stack                                              0         
eclipse/centos_vertx               CentOS + vertx                                                          0         
[root@wangliukun ~]# 
镜像推送

登录docker hub

[root@localhost ~]# docker login
Log in with your Docker ID or email address to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com/ to create one.
You can log in with your password or a Personal Access Token (PAT). Using a limited-scope PAT grants better security and is required for organizations using SSO. Learn more at https://docs.docker.com/go/access-tokens/

Username: wangliukun 
Password: #wang8218.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@localhost ~]# 

使用docker push命令向Docker Hub

[root@localhost ~]# docker push centos/mkdir-dockerfile
Using default tag: latest
The push refers to repository [docker.io/centos/mkdir-dockerfile]
e4ed70109471: Preparing 
74ddd0ec08fa: Preparing 
denied: requested access to the resource is denied
[root@localhost ~]# 

出现错误:

镜像推送错误,请求资源被拒绝

问题原因:

Docker Hub上的镜像都有一个tag标签

解决办法:

用户上传镜像时给镜像添加tag标签

[root@localhost ~]# docker tag centos/mkdir-dockerfile wangliukun/centos-mkdir:latest
[root@localhost ~]# docker images
REPOSITORY                TAG       IMAGE ID       CREATED         SIZE
centos/mkdir-dockerfile   latest    515667f5b2be   2 hours ago     231MB
wangliukun/centos-mkdir   latest    515667f5b2be   2 hours ago     231MB
centos/vim                latest    1ab761fac3cb   3 hours ago     298MB
hello-world               latest    d2c94e258dcb   10 months ago   13.3kB
hello-world               latest    feb5d9fea6a5   2 years ago     13.3kB
centos                    latest    5d0da3dc9764   2 years ago     231MB
centos                    latest    5d0da3dc9764   2 years ago     231MB
ubuntu                    15.04     d1b55fd07600   8 years ago     131MB

重新上传镜像:

[root@localhost ~]# docker push wangliukun/centos-mkdir
Using default tag: latest
The push refers to repository [docker.io/wangliukun/centos-mkdir]
e4ed70109471: Pushed 
74ddd0ec08fa: Pushed 
latest: digest: sha256:4f67818d55165a36f3377ce5e7a9015c57b50d8c5f5156485a23d0a67f4cd7c4 size: 736
[root@localhost ~]# 

尝试拉取先前推送的镜像

[root@localhost ~]# docker pull wangliukun/centos-mkdir
Using default tag: latest
latest: Pulling from wangliukun/centos-mkdir
Digest: sha256:4f67818d55165a36f3377ce5e7a9015c57b50d8c5f5156485a23d0a67f4cd7c4
Status: Image is up to date for wangliukun/centos-mkdir:latest
docker.io/wangliukun/centos-mkdir:latest
[root@localhost ~]# 

6、Docker镜像优化

base镜像优化

base 镜像优化就是在满足环境要求的前提下使用最小的base镜像。常用的 Linux base镜像有CentOS、 Ubuntu、 Alpine等,其中一些比较小的 base镜像适合作为精简镜像的基础镜像,如 Alpine、BusyBox等。

拉取 Alpine与BusyBox的镜像进行对比

[root@wangliukun ~]# docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
5cc84ad355aa: Pull complete 
Digest: sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
Status: Downloaded newer image for busybox:latest
docker.io/library/busybox:latest
[root@wangliukun ~]# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
59bf1c3509f3: Pull complete 
Digest: sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest
[root@wangliukun ~]# docker images
REPOSITORY                TAG       IMAGE ID       CREATED         SIZE
centos/mkdir-dockerfile   latest    515667f5b2be   2 hours ago     231MB
wangliukun/centos-mkdir   latest    515667f5b2be   2 hours ago     231MB
wangliukun/centos-nkdir   latest    515667f5b2be   2 hours ago     231MB
centos/vim                latest    1ab761fac3cb   3 hours ago     298MB
hello-world               latest    d2c94e258dcb   10 months ago   13.3kB
busybox                   latest    beae173ccac6   2 years ago     1.24MB
alpine                    latest    c059bfaa849c   2 years ago     5.59MB
hello-world               latest    feb5d9fea6a5   2 years ago     13.3kB
centos                    latest    5d0da3dc9764   2 years ago     231MB
centos                    latest    5d0da3dc9764   2 years ago     231MB
ubuntu                    15.04     d1b55fd07600   8 years ago     131MB
[root@wangliukun ~]# 

Scratch是一个空镜像,只能用于构建其他镜像,常用于执行一些包含了所有依赖的二进制文
件。如果以Scratch为base镜像,意味着不以任何镜像为基础,下面的指令将作为镜像的第一层存在。

BusyBox相对于 Scratch多了一些常用的Linux命令,BusyBox的官方镜像大小只有1MB多一点,
非常适合构建小镜像。

Alpine是一款高度精简又包含了基本工具的轻量级 Linux发行版,官方 base镜像只有5MB多一
点,很适合当作base镜像使用。

Dockerfile优化

Dockerfile文件包含太多的RUN命令会显得特别臃肿

优化原则:集成多条命令为一条

例:

[root@wangliukun ~]# vim Dockerfile 
                   
FROM centos
RUN mkdir /etc/yum.repos.d/CentOS
RUN mv /etc/yum.repos.d/CentOS-* /etc/yum.repos.d/CentOS/
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
RUN yum makecache
RUN yum -y install vim
RUN yum -y install wget
RUN yum -y install net-tools
RUN yum -y install nano
RUN yum -y install httpd
EXPOSE 80
CMD systemctl start httpd
~   

#构建
[root@wangliukun ~]# docker build -t centos_yum1 .
[+] Building 80.7s (14/14) FINISHED                                                                            docker:default
 => [internal] load build definition from Dockerfile                                                                     0.0s
 => => transferring dockerfile: 487B                                                                                     0.0s
 => [internal] load metadata for docker.io/library/centos:latest                                                         0.0s
 => [internal] load .dockerignore                                                                                        0.0s
 => => transferring context: 2B                                                                                          0.0s
 => [ 1/10] FROM docker.io/library/centos:latest                                                                         0.0s
 => CACHED [ 2/10] RUN mkdir /etc/yum.repos.d/CentOS                                                                     0.0s
 => [ 3/10] RUN mv /etc/yum.repos.d/CentOS-* /etc/yum.repos.d/CentOS/                                                    0.5s
 => [ 4/10] RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo     1.3s
 => [ 5/10] RUN yum makecache                                                                                           21.7s
 => [ 6/10] RUN yum -y install vim                                                                                      21.3s 
 => [ 7/10] RUN yum -y install wget                                                                                      7.1s 
 => [ 8/10] RUN yum -y install net-tools                                                                                 6.2s 
 => [ 9/10] RUN yum -y install nano                                                                                      6.9s 
 => [10/10] RUN yum -y install httpd                                                                                    15.2s 
 => exporting to image                                                                                                   0.6s 
 => => exporting layers                                                                                                  0.6s 
 => => writing image sha256:c85c8f1cbd15e637b7765dc4d615d6887450545b60203e3f4a3d5852fb84940f                             0.0s 
 => => naming to docker.io/library/centos_yum1                                                                           0.0s 
[root@wangliukun ~]#          
 

以上可以看出整个过程十分复杂,查看新镜像大小与UnionFS层数:

[root@wangliukun ~]# docker images                                                                                            
REPOSITORY                TAG       IMAGE ID       CREATED              SIZE
centos_yum1               latest    c85c8f1cbd15   About a minute ago   366MB
centos_yum                latest    49b177114ea8   52 minutes ago       231MB
centos/mkdir-dockerfile   latest    515667f5b2be   3 hours ago          231MB
wangliukun/centos-mkdir   latest    515667f5b2be   3 hours ago          231MB
wangliukun/centos-nkdir   latest    515667f5b2be   3 hours ago          231MB
centos/vim                latest    1ab761fac3cb   4 hours ago          298MB
hello-world               latest    d2c94e258dcb   10 months ago        13.3kB
busybox                   latest    beae173ccac6   2 years ago          1.24MB
alpine                    latest    c059bfaa849c   2 years ago          5.59MB
hello-world               latest    feb5d9fea6a5   2 years ago          13.3kB
centos                    latest    5d0da3dc9764   2 years ago          231MB
centos                    latest    5d0da3dc9764   2 years ago          231MB
ubuntu                    15.04     d1b55fd07600   8 years ago          131MB

#查看构建过程
[root@wangliukun ~]# docker history c85c
IMAGE          CREATED          CREATED BY                                       SIZE      COMMENT
c85c8f1cbd15   2 minutes ago    CMD ["/bin/sh" "-c" "systemctl start httpd"]     0B        buildkit.dockerfile.v0
<missing>      2 minutes ago    EXPOSE map[80/tcp:{}]                            0B        buildkit.dockerfile.v0
<missing>      2 minutes ago    RUN /bin/sh -c yum -y install httpd # buildk…   22.7MB    buildkit.dockerfile.v0
<missing>      2 minutes ago    RUN /bin/sh -c yum -y install nano # buildkit    15.1MB    buildkit.dockerfile.v0
<missing>      2 minutes ago    RUN /bin/sh -c yum -y install net-tools # bu…   14.9MB    buildkit.dockerfile.v0
<missing>      2 minutes ago    RUN /bin/sh -c yum -y install wget # buildkit    15MB      buildkit.dockerfile.v0
<missing>      2 minutes ago    RUN /bin/sh -c yum -y install vim # buildkit     39.5MB    buildkit.dockerfile.v0
<missing>      3 minutes ago    RUN /bin/sh -c yum makecache # buildkit          27.2MB    buildkit.dockerfile.v0
<missing>      3 minutes ago    RUN /bin/sh -c curl -o /etc/yum.repos.d/Cent…   2.5kB     buildkit.dockerfile.v0
<missing>      3 minutes ago    RUN /bin/sh -c mv /etc/yum.repos.d/CentOS-* …   8.79kB    buildkit.dockerfile.v0
<missing>      53 minutes ago   RUN /bin/sh -c mkdir /etc/yum.repos.d/CentOS…   0B        buildkit.dockerfile.v0
<missing>      2 years ago      /bin/sh -c #(nop)  CMD ["/bin/bash"]             0B        
<missing>      2 years ago      /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B        
<missing>      2 years ago      /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0…   231MB     
[root@wangliukun ~]# 


以上可看到新镜像centos_yum1大小 366MB而镜像UnionFS层数13层

对Dockerfile进行优化

#更改Dockerfile 在每个命令后面加上‘&&’(未结束)和‘\’换行
[root@wangliukun ~]# vim Dockerfile 
FROM centos
RUN mkdir /etc/yum.repos.d/CentOS && \
    mv /etc/yum.repos.d/CentOS-* /etc/yum.repos.d/CentOS/ && \
    curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo && \
    yum makecache && \
    yum -y install vim && \
    yum -y install wget && \
    yum -y install net-tools && \
    yum -y install nano && \
    yum -y install httpd
EXPOSE 80
CMD systemctl start httpd
~            

#构建镜像
[root@wangliukun ~]# docker build -t centos_yum2 .
[+] Building 51.5s (6/6) FINISHED                                                                              docker:default
 => [internal] load build definition from Dockerfile                                                                     0.0s
 => => transferring dockerfile: 527B                                                                                     0.0s
 => [internal] load metadata for docker.io/library/centos:latest                                                         0.0s
 => [internal] load .dockerignore                                                                                        0.0s
 => => transferring context: 2B                                                                                          0.0s
 => CACHED [1/2] FROM docker.io/library/centos:latest                                                                    0.0s
 => [2/2] RUN mkdir /etc/yum.repos.d/CentOS &&     mv /etc/yum.repos.d/CentOS-* /etc/yum.repos.d/CentOS/ &&     curl -  50.6s
 => exporting to image                                                                                                   0.8s 
 => => exporting layers                                                                                                  0.8s 
 => => writing image sha256:d75aaf1f2a077e983f8eaab6c165dc2e37e1bf6a44ae506b87edb0ebeb89a635                             0.0s 
 => => naming to docker.io/library/centos_yum2  
 

查看新镜像大小与UnionFS层数:

[root@wangliukun ~]# docker images                                                                                            
REPOSITORY                TAG       IMAGE ID       CREATED             SIZE                                                   
centos_yum2               latest    d75aaf1f2a07   54 seconds ago      309MB
centos_yum1               latest    c85c8f1cbd15   14 minutes ago      366MB
centos_yum                latest    49b177114ea8   About an hour ago   231MB
wangliukun/centos-nkdir   latest    515667f5b2be   4 hours ago         231MB
centos/mkdir-dockerfile   latest    515667f5b2be   4 hours ago         231MB
wangliukun/centos-mkdir   latest    515667f5b2be   4 hours ago         231MB
centos/vim                latest    1ab761fac3cb   5 hours ago         298MB
hello-world               latest    d2c94e258dcb   10 months ago       13.3kB
busybox                   latest    beae173ccac6   2 years ago         1.24MB
alpine                    latest    c059bfaa849c   2 years ago         5.59MB
hello-world               latest    feb5d9fea6a5   2 years ago         13.3kB
centos                    latest    5d0da3dc9764   2 years ago         231MB
centos                    latest    5d0da3dc9764   2 years ago         231MB
ubuntu                    15.04     d1b55fd07600   8 years ago         131MB
[root@wangliukun ~]# docker history  d75
IMAGE          CREATED              CREATED BY                                       SIZE      COMMENT
d75aaf1f2a07   About a minute ago   CMD ["/bin/sh" "-c" "systemctl start httpd"]     0B        buildkit.dockerfile.v0
<missing>      About a minute ago   EXPOSE map[80/tcp:{}]                            0B        buildkit.dockerfile.v0
<missing>      About a minute ago   RUN /bin/sh -c mkdir /etc/yum.repos.d/CentOS…   77.4MB    buildkit.dockerfile.v0
<missing>      2 years ago          /bin/sh -c #(nop)  CMD ["/bin/bash"]             0B        
<missing>      2 years ago          /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B        
<missing>      2 years ago          /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0…   231MB     
[root@wangliukun ~]# 

以上可看到新镜像centos_yum2大小 309MB而镜像UnionFS层数5层

7、Docker操作

docker pull 镜像名 下载容器镜像

docker images 查看本地镜像

 -a 显示所有镜像(默认不显示中间层镜像)

 -q 只显示本地所有镜像id号
 
 --no-trunc 使用不截断模式显示完整镜像ID号
 
 docker -ps -a:查找所有正在运行或停止运行的docker容器 
 
 docker history + 镜像ID号  查看镜像构建过程
 
 docker run -it +镜像名  启动镜像
 
 docker commit + 镜像ID + 需要自动安装的服务  构建镜像
 
 例如:docker commit + c57(镜像ID不必写全,只需于其他镜像有区分即可) + centos/vim(使得新镜像自动安装vim)
 
 dockerfile   用于构建镜像
 
 docker build -t 源镜像/mkdir-dockerfile 运行dockerfile文件
 
 docker search  从镜像仓库中查找所需镜像
 
 docker push  镜像推送
 
 docker tagin 镜像标签名

dockerfile常用指令

- from     指定源镜像
- maintainer      指定作者信息
- run     在当前容器中运行指定命令
- expose     指定运行容器时要使用的端口
- cmd     指定容器启动时要运行的命令
- entrypoint     cmd或容器启动时添加的命令会被当做参数传给entrypoint 
- copy      将文件或目录复制到当前容器中
- add      将文件或目录复制到当前容器中,源文件如果是压缩文件,自动解压
- vplume     为容器添加容器卷
- workdir      在容器中设置工作目录
- env      设置环境变量
- user    指定容器以什么用户身份运行,默认root

docker search常用指令

--automated :只列出 automated build类型的镜像;

--no-trunc :显示完整的镜像DESCRIPTION(描述),不省略;

-f <过滤条件>:列出收藏数(点赞)不小于指定值的镜像。

--limit 5:列出前5个镜像

-s <指定值> :表示列出星标数不小于指定值的镜像

上一章:Docker安装
下一章:Docker容器

  • 23
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Docker 中,镜像(Image)是一个轻量级、可执行的软件包,其中包含了运行特定应用程序所需的所有代码、运行时、库、环境变量和配置等信息。Docker 镜像可以看作是一个只读的模板,用于创建容器(Container)。 Docker 镜像可以通过以下几种方式获取: 1. 从 Docker Hub 下载:Docker Hub 是 Docker 官方提供的公共镜像仓库,包含了大量的镜像,可以通过 `docker pull` 命令从 Docker Hub 上下载镜像,例如: ``` docker pull ubuntu:latest ``` 上面的命令将会下载最新版本的 Ubuntu 镜像。 2. 通过 Dockerfile 构建:Dockerfile 是一个文本文件,其中包含了一系列构建镜像的指令和配置信息,可以通过 `docker build` 命令根据 Dockerfile 构建镜像,例如: ``` docker build -t my-image . ``` 上面的命令将会在当前目录下的 Dockerfile 中定义的基础上构建一个名为 `my-image` 的镜像。 3. 从已有容器创建:可以通过 `docker commit` 命令从一个已有的容器中创建新的镜像,例如: ``` docker commit my-container my-image ``` 上面的命令将会创建一个名为 `my-image` 的新镜像,其中包含了 `my-container` 容器的所有修改。 Docker 镜像可以使用 `docker images` 命令查看当前系统中已有的镜像列表,也可以使用 `docker rmi` 命令删除不需要的镜像。在使用 Docker 时,镜像是一个非常重要的概念,通过合理地管理镜像,可以提高应用程序的部署效率和可维护性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值