制作 Docker 镜像

目录

1 docker镜像介绍

1.1 docker的镜像结构  

1.2 镜像运行的基本原理

1.3 镜像获得方式

2 构建 docker 镜像 Dockerfile

2.1 Dockerfile 基础参数介绍

2.2 实现参数功能示例

2.2.1 FROM LABEL COPY

2.2.2 ADD

2.2.3 ENV 和 CMD与ENTRYPOINT

2.2.3.1 CMD的替代性

2.2.3.2 ENTRYPOINT的不可替代性

2.2.4 EXPOSE:端口暴露

2.2.5 VOLUME:指定容器的目录将他挂载到宿主机上

2.3 使用Dockerfile构建nginx镜像

2.3.1 系统更换网络源

2.3.2 编写Dockerfile镜像文件

2.3.3 镜像优化示例


1 docker镜像介绍

1.1 docker的镜像结构  

  • 共享宿主机的kernel
  • base镜像提供的是最小的Linux发行版
  • 同一docker主机支持运行多种Linux发行版
  • 采用分层结构的最大好处是:共享资源

1.2 镜像运行的基本原理

  • Copy-on-Write 可写容器层
  • 容器层以下所有镜像层都是只读的
  • docker从上往下依次查找文件
  • 容器层保存镜像变化的部分,并不会对镜像本身进行任何修改
  • 一个镜像最多127

1.3 镜像获得方式

  • 基本镜像通常由软件官方提供
  • 企业镜像可以用官方镜像+Dockerfile来生成
  • 系统关于镜像的获取动作有两种:
docker pull 镜像地址
docker load -i 本地镜像包

2 构建 docker 镜像 Dockerfile

2.1 Dockerfile 基础参数介绍

指令描述
FROM指定基础镜像,例如:FROM busybox:version
COPY复制文件,例如:COPY file /file 或者 COPY ["file","/"]
MAINTAINER指定作者信息,例如邮箱:MAINTAINER user@example.com在最新版的Docker中用LABEL KEY="VALUE"代替
ADD

功能和COPY相似,指定压缩文件或url,

例如:ADD test.tar /mnt 或者 ADD http://ip/test.tar /mnt

ENV指定环境变量,例如:ENV FILENAME test
EXPOSE暴露容器端口,例如:EXPOSE 80
VOLUME声明数据卷,通常指数据挂载点,例如:VOLUME ["/var/www/html"]
WORKDIR切换路径,例如:WORKDIR /mnt
RUN在容器中运行的指令,例如:touch file
CMD

在启动容器时自动运行的动作,可以被覆盖,

例如:CMD echo FILENAME会调用shell解析

例如:CMD["/bin/sh","−c","echoFILENAME"] 不调用shell解析

ENTRYPOINT和CMD功能和用法类似,但动作不可被覆盖

2.2 实现参数功能示例

2.2.1 FROM LABEL COPY

[root@rockynode-1 dockerfile]# vim Dockerfile 
FROM busybox:latest
LABEL mail = shuyan@123
COPY shuyan /


[root@rockynode-1 dockerfile]# docker build -t shuyan_copy:v1 .
[+] Building 0.1s (7/7) FINISHED         docker:default
 => [internal] load build definition from Dockerf  0.0s
 => => transferring dockerfile: 155B               0.0s
 => WARN: LegacyKeyValueFormat: "LABEL key=value"  0.0s
 => [internal] load metadata for docker.io/librar  0.0s
 => [internal] load .dockerignore                  0.0s
 => => transferring context: 2B                    0.0s
 => [internal] load build context                  0.0s
 => => transferring context: 87B                   0.0s
 => [1/2] FROM docker.io/library/busybox:latest    0.0s
 => [2/2] COPY shuyan /                            0.0s
 => exporting to image                             0.0s
 => => exporting layers                            0.0s
 => => writing image sha256:9185f618183ab1a307bfb  0.0s
 => => naming to docker.io/library/shuyan_copy:v1  0.0s

 1 warning found (use docker --debug to expand):
 - LegacyKeyValueFormat: "LABEL key=value" should be used instead of legacy "LABEL key value" format (line 2)

使用 ctrl+pq 不删除 镜像退出

[root@rockynode-1 dockerfile]# docker history shuyan_copy:v1 
IMAGE          CREATED         CREATED BY                          SIZE      COMMENT
9185f618183a   5 minutes ago   COPY shuyan / # buildkit            0B        buildkit.dockerfile.v0
<missing>      5 minutes ago   LABEL mail== shuyan@123             0B        buildkit.dockerfile.v0
<missing>      15 months ago   BusyBox 1.36.1 (glibc), Debian 12   4.26MB  


[root@rockynode-1 dockerfile]# docker ps -a
CONTAINER ID   IMAGE            COMMAND   CREATED         STATUS         PORTS     NAMES
c7d1405cc637   shuyan_copy:v1   "sh"      2 minutes ago   Up 2 minutes             test
[root@rockynode-1 dockerfile]# docker rm -f test 
test

2.2.2 ADD

[root@rockynode-1 dockerfile]# touch shuyanfile{1..3}
[root@rockynode-1 dockerfile]# tar zcf shuyanflie.tar.gz shuyanfile*
[root@rockynode-1 dockerfile]# 
FROM busybox:latest
LABEL mail = shuyan@123
COPY shuyan /
ADD shuyanflie.tar.gz /


[root@rockynode-1 dockerfile]# docker build -t shuyan_add:v1 .
[+] Building 0.1s (8/8) FINISHED                                           docker:default
 => [internal] load build definition from Dockerfile                                 0.0s
 => => transferring dockerfile: 179B                                                 0.0s
 => WARN: LegacyKeyValueFormat: "LABEL key=value" should be used instead of legacy   0.0s
 => [internal] load metadata for docker.io/library/busybox:latest                    0.0s
 => [internal] load .dockerignore                                                    0.0s
 => => transferring context: 2B                                                      0.0s
 => [1/3] FROM docker.io/library/busybox:latest                                      0.0s
 => [internal] load build context                                                    0.0s
 => => transferring context: 332B                                                    0.0s
 => CACHED [2/3] COPY shuyan /                                                       0.0s
 => [3/3] ADD shuyanflie.tar.gz /                                                    0.0s
 => exporting to image                                                               0.0s
 => => exporting layers                                                              0.0s
 => => writing image sha256:3c20eac277664d5e3d2c02efbfda238748c1cd52dfb89ab26141431  0.0s
 => => naming to docker.io/library/shuyan_add:v1                                     0.0s

 1 warning found (use docker --debug to expand):
 - LegacyKeyValueFormat: "LABEL key=value" should be used instead of legacy "LABEL key value" format (line 2)
[root@rockynode-1 dockerfile]# docker images 
REPOSITORY           TAG           IMAGE ID       CREATED          SIZE
shuyan_add           v1            3c20eac27766   23 seconds ago   4.26MB
nginx                1.26-alpine   9703b2608a98   12 days ago      43.3MB
nginx                latest        5ef79149e0ec   12 days ago      188MB
busybox              latest        65ad0d468eb1   15 months ago    4.26MB
timinglee/game2048   latest        19299002fdbe   7 years ago      55.5MB
timinglee/mario      latest        9a35a9e43e8c   8 years ago      198MB
[root@rockynode-1 dockerfile]# docker run -it --name test shuyan_add:v1 
/ # ls
bin          home         proc         shuyanfile1  sys          var
dev          lib          root         shuyanfile2  tmp
etc          lib64        shuyan       shuyanfile3  usr


[root@rockynode-1 dockerfile]# docker rm -f test 
test

2.2.3 ENV 和 CMD与ENTRYPOINT

FROM busybox:latest
LABEL mail = shuyan@123
COPY shuyan /
ADD shuyanflie.tar.gz /
ENV NAME shuyan   # 设置变量
CMD echo $NAME    # 打印变量

[root@rockynode-1 dockerfile]# docker build -t shuyan_env_cmd .
[+] Building 0.1s (8/8) FINISHED                                           docker:default
 => [internal] load build definition from Dockerfile                                 0.0s
 => => transferring dockerfile: 210B                                                 0.0s
 => WARN: LegacyKeyValueFormat: "LABEL key=value" should be used instead of legacy   0.0s
 => WARN: LegacyKeyValueFormat: "ENV key=value" should be used instead of legacy "E  0.0s
 => WARN: JSONArgsRecommended: JSON arguments recommended for CMD to prevent uninte  0.0s
 => [internal] load metadata for docker.io/library/busybox:latest                    0.0s
 => [internal] load .dockerignore                                                    0.0s
 => => transferring context: 2B                                                      0.0s
 => [1/3] FROM docker.io/library/busybox:latest                                      0.0s
 => [internal] load build context                                                    0.0s
 => => transferring context: 182B                                                    0.0s
 => CACHED [2/3] COPY shuyan /                                                       0.0s
 => CACHED [3/3] ADD shuyanflie.tar.gz /                                             0.0s
 => exporting to image                                                               0.0s
 => => exporting layers                                                              0.0s
 => => writing image sha256:74952b428aa082102f25e0d25f84016907e7cb6845187dc671b7088  0.0s
 => => naming to docker.io/library/shuyan_env_cmd                                    0.0s

 3 warnings found (use docker --debug to expand):
 - LegacyKeyValueFormat: "LABEL key=value" should be used instead of legacy "LABEL key value" format (line 2)
 - LegacyKeyValueFormat: "ENV key=value" should be used instead of legacy "ENV key value" format (line 5)
 - JSONArgsRecommended: JSON arguments recommended for CMD to prevent unintended behavior related to OS signals (line 6)
[root@rockynode-1 dockerfile]# docker images 
REPOSITORY           TAG           IMAGE ID       CREATED         SIZE
shuyan_env_cmd       latest        74952b428aa0   6 minutes ago   4.26MB
nginx                1.26-alpine   9703b2608a98   12 days ago     43.3MB
nginx                latest        5ef79149e0ec   12 days ago     188MB
busybox              latest        65ad0d468eb1   15 months ago   4.26MB
timinglee/game2048   latest        19299002fdbe   7 years ago     55.5MB
timinglee/mario      latest        9a35a9e43e8c   8 years ago     198MB

dockerfile]# docker run -it --rm --name test shuyan_env_cmd:latest 
shuyan   # 直接输出


[root@rockynode-1 dockerfile]# docker rmi shuyan_env_cmd:latest 
2.2.3.1 CMD的替代性
[root@rockynode-1 dockerfile]# docker build -t shuyan_cmd .
[+] Building 0.1s (8/8) FINISHED                                           docker:default
 => [internal] load build definition from Dockerfile                                 0.0s
 => => transferring dockerfile: 252B                                                 0.0s
 => WARN: LegacyKeyValueFormat: "LABEL key=value" should be used instead of legacy   0.0s
 => WARN: LegacyKeyValueFormat: "ENV key=value" should be used instead of legacy "E  0.0s
 => [internal] load metadata for docker.io/library/busybox:latest                    0.0s
 => [internal] load .dockerignore                                                    0.0s
 => => transferring context: 2B                                                      0.0s
 => [1/3] FROM docker.io/library/busybox:latest                                      0.0s
 => [internal] load build context                                                    0.0s
 => => transferring context: 182B                                                    0.0s
 => CACHED [2/3] COPY shuyan /                                                       0.0s
 => CACHED [3/3] ADD shuyanflie.tar.gz /                                             0.0s
 => exporting to image                                                               0.0s
 => => exporting layers                                                              0.0s
 => => writing image sha256:6c75da7d6a03a1e2b0d6caab10eb5e0d97186a17c2091ab3ed28618  0.0s
 => => naming to docker.io/library/shuyan_cmd                                        0.0s

 2 warnings found (use docker --debug to expand):
 - LegacyKeyValueFormat: "LABEL key=value" should be used instead of legacy "LABEL key value" format (line 2)
 - LegacyKeyValueFormat: "ENV key=value" should be used instead of legacy "ENV key value" format (line 5)
dockerfile]# docker run -it --name test --rm shuyan_cmd:latest 
shuyan
[root@rockynode-1 dockerfile]# docker images 
REPOSITORY           TAG           IMAGE ID       CREATED          SIZE
shuyan_cmd           latest        6c75da7d6a03   24 minutes ago   4.26MB
nginx                1.26-alpine   9703b2608a98   12 days ago      43.3MB
nginx                latest        5ef79149e0ec   12 days ago      188MB
busybox              latest        65ad0d468eb1   15 months ago    4.26MB
timinglee/game2048   latest        19299002fdbe   7 years ago      55.5MB
timinglee/mario      latest        9a35a9e43e8c   8 years ago      198MB

[root@rockynode-1 dockerfile]# docker rmi shuyan_cmd:latest 
Untagged: shuyan_cmd:latest
Deleted: sha256:6c75da7d6a03a1e2b0d6caab10eb5e0d97186a17c2091ab3ed2861888d1a5979
[root@rockynode-1 dockerfile]# docker images 
REPOSITORY           TAG           IMAGE ID       CREATED         SIZE
nginx                1.26-alpine   9703b2608a98   12 days ago     43.3MB
nginx                latest        5ef79149e0ec   12 days ago     188MB
busybox              latest        65ad0d468eb1   15 months ago   4.26MB
timinglee/game2048   latest        19299002fdbe   7 years ago     55.5MB
timinglee/mario      latest        9a35a9e43e8c   8 years ago     198MB
[root@rockynode-1 dockerfile]# docker build -t shuyan_cmd:v1 .
[+] Building 0.1s (8/8) FINISHED                                           docker:default
 => [internal] load build definition from Dockerfile                                 0.0s
 => => transferring dockerfile: 252B                                                 0.0s
 => WARN: LegacyKeyValueFormat: "LABEL key=value" should be used instead of legacy   0.0s
 => WARN: LegacyKeyValueFormat: "ENV key=value" should be used instead of legacy "E  0.0s
 => [internal] load metadata for docker.io/library/busybox:latest                    0.0s
 => [internal] load .dockerignore                                                    0.0s
 => => transferring context: 2B                                                      0.0s
 => [1/3] FROM docker.io/library/busybox:latest                                      0.0s
 => [internal] load build context                                                    0.0s
 => => transferring context: 182B                                                    0.0s
 => CACHED [2/3] COPY shuyan /                                                       0.0s
 => CACHED [3/3] ADD shuyanflie.tar.gz /                                             0.0s
 => exporting to image                                                               0.0s
 => => exporting layers                                                              0.0s
 => => writing image sha256:6c75da7d6a03a1e2b0d6caab10eb5e0d97186a17c2091ab3ed28618  0.0s
 => => naming to docker.io/library/shuyan_cmd:v1                                     0.0s

 2 warnings found (use docker --debug to expand):
 - LegacyKeyValueFormat: "LABEL key=value" should be used instead of legacy "LABEL key value" format (line 2)
 - LegacyKeyValueFormat: "ENV key=value" should be used instead of legacy "ENV key value" format (line 5)

发现使用cmd 的会被 shell 替代 

dockerfile]# docker run -it --name test --rm shuyan_cmd:v1 sh
/ # exit

[root@rockynode-1 dockerfile]# docker rmi shuyan_cmd:v1 
Untagged: shuyan_cmd:v1
Deleted: sha256:6c75da7d6a03a1e2b0d6caab10eb5e0d97186a17c2091ab3ed2861888d1a5979
2.2.3.2 ENTRYPOINT的不可替代性
[root@rockynode-1 dockerfile]# vim Dockerfile
FROM busybox:latest
LABEL mail = shuyan@123
COPY shuyan /
ADD shuyanflie.tar.gz /
ENV NAME shuyan
#CMD echo $NAME
#CMD ["/bin/sh","-c","/bin/echo $NAME"]
ENTRYPOINT ["/bin/sh","-c","/bin/echo $NAME"]  # 不可被替代


[root@rockynode-1 dockerfile]# docker build -t shuyan_ent:v1 .
[+] Building 0.1s (8/8) FINISHED                                           docker:default
 => [internal] load build definition from Dockerfile                                 0.0s
 => => transferring dockerfile: 299B                                                 0.0s
 => WARN: LegacyKeyValueFormat: "LABEL key=value" should be used instead of legacy   0.0s
 => WARN: LegacyKeyValueFormat: "ENV key=value" should be used instead of legacy "E  0.0s
 => [internal] load metadata for docker.io/library/busybox:latest                    0.0s
 => [internal] load .dockerignore                                                    0.0s
 => => transferring context: 2B                                                      0.0s
 => [1/3] FROM docker.io/library/busybox:latest                                      0.0s
 => [internal] load build context                                                    0.0s
 => => transferring context: 182B                                                    0.0s
 => CACHED [2/3] COPY shuyan /                                                       0.0s
 => CACHED [3/3] ADD shuyanflie.tar.gz /                                             0.0s
 => exporting to image                                                               0.0s
 => => exporting layers                                                              0.0s
 => => writing image sha256:c93e8d613c2ec68595594ce0455e67bdbd12b83190ca31c598bcdb2  0.0s
 => => naming to docker.io/library/shuyan_ent:v1                                     0.0s

 2 warnings found (use docker --debug to expand):
 - LegacyKeyValueFormat: "LABEL key=value" should be used instead of legacy "LABEL key value" format (line 2)
 - LegacyKeyValueFormat: "ENV key=value" should be used instead of legacy "ENV key value" format (line 5)

[root@rockynode-1 dockerfile]# docker images 
REPOSITORY           TAG           IMAGE ID       CREATED          SIZE
shuyan_ent           v1            c93e8d613c2e   29 minutes ago   4.26MB
nginx                1.26-alpine   9703b2608a98   12 days ago      43.3MB
nginx                latest        5ef79149e0ec   12 days ago      188MB
busybox              latest        65ad0d468eb1   15 months ago    4.26MB
timinglee/game2048   latest        19299002fdbe   7 years ago      55.5MB
timinglee/mario      latest        9a35a9e43e8c   8 years ago      198MB

dockerfile]# docker run --name test --rm -it shuyan_ent:v1 
shuyan
dockerfile]# docker run --name test --rm -it shuyan_ent:v1 sh
shuyan

2.2.4 EXPOSE:端口暴露

[root@rockynode-1 _data]# vim /root/dockerfile/Dockerfile 
FROM busybox
MAINTAINER shuyan@com
EXPOSE 80 443  # 暴露80与443端口
[root@rockynode-1 dockerfile]# docker ps 
CONTAINER ID   IMAGE        COMMAND   CREATED         STATUS         PORTS             NAMES
2c53843b7bb4   busybox:v3   "sh"      4 minutes ago   Up 4 minutes   80/tcp, 443/tcp   test

2.2.5 VOLUME:指定容器的目录将他挂载到宿主机上

[root@rockynode-1 _data]# vim /root/dockerfile/Dockerfile 

FROM busybox
MAINTAINER shuyan@com
EXPOSE 80 443
VOLUME /var/www/html   # 将容器这个目录内的东西挂载到宿主机
WORKDIR /var/www/html # 进入容器内马上切换到的目录
[root@rockynode-1 dockerfile]# docker run -it --name=test busybox:v2
/var/www/html # ls
/var/www/html # touch 1 2 3 4 5  6 7
/var/www/html # ls
1  2  3  4  5  6  7
/var/www/html # [root@rockynode-1 dockerfile]# 
[root@rockynode-1 dockerfile]# docker inspect test | grep Moun -A 6

--
        "Mounts": [
            {
                "Type": "volume",
                "Name": "d52249eddca8aaf9027c73b8b9051ca68f49c782b44b16b71598ba27f73a3706",
                "Source": "/var/lib/docker/volumes/d52249eddca8aaf9027c73b8b9051ca68f49c782b44b16b71598ba27f73a3706/_data",
                "Destination": "/var/www/html",
                "Driver": "local",
[root@rockynode-1 dockerfile]# ls /var/lib/docker/volumes/d52249eddca8aaf9027c73b8b9051ca68f49c782b44b16b71598ba27f73a3706/_data
1  2  3  4  5  6  7

2.3 使用Dockerfile构建nginx镜像

使用centos镜像制作NGINX镜像

2.3.1 系统更换网络源

由于在centos7的国内镜像已经过期,所以需要自己搭一个服务器,将另一台有光盘镜像  的centos将镜像挂载到上边

[root@rockynode-1 dockerfile]# yum install httpd

Listen 8080

[root@rockynode-1 dockerfile]# mkdir /var/www/html/centos7
[root@rockynode-1 dockerfile]# mount /dev/sr1 /var/www/html/centos7/
[root@rockynode-1 dockerfile]# systemctl restart httpd

2.3.2 编写Dockerfile镜像文件

需要准备NGINX源码包

[root@rockynode-1 dockerfile]# docker load -i centos-7.tar.gz 
[root@rockynode-1 dockerfile]# docker images 
REPOSITORY           TAG           IMAGE ID       CREATED          SIZE
nginx                1.26-alpine   9703b2608a98   12 days ago      43.3MB
nginx                latest        5ef79149e0ec   12 days ago      188MB
busybox              latest        65ad0d468eb1   15 months ago    4.26MB
centos               7             eeb6ee3f44bd   2 years ago      204MB
timinglee/game2048   latest        19299002fdbe   7 years ago      55.5MB
timinglee/mario      latest        9a35a9e43e8c   8 years ago      198MB

首先需要运行一个容器将他的yum源改成刚才配置的http服务器网址

[root@rockynode-1 dockerfile]# docker run -it --name=centos7 centos:7
[root@65ed46926df1 /]# [root@rockynode-1 dockerfile]# 
[root@rockynode-1 dockerfile]# docker exec -it centos7 sh
sh-4.2# cd /etc/yum.repos.d/
sh-4.2# ls
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Sources.repo  CentOS-fasttrack.repo
CentOS-CR.repo    CentOS-Media.repo      CentOS-Vault.repo    CentOS-x86_64-kernel.repo
sh-4.2# rm -fr *
sh-4.2# vi centos.repo
[rhel]
name=rhel
baseurl=http://172.17.0.1:8080/centos7
gpgcheck=0

# ctrl + pq 退出
sh-4.2# read escape sequence  

# 将修改好的容器提交为镜像
dockerfile]# docker commit -m "cent2_repo" centos7 centos7:repo

可查看到容器的IP,他是与宿主机进行桥接的,也就是说链接这个就等于链接宿主机 

编写Dockerfile 文件

[root@rockynode-1 dockerfile]# vim Dockerfile 
FROM centos7:repo
LABEL mail = shuyan@123
ADD nginx-1.26.1.tar.gz /mnt
WORKDIR /mnt/nginx-1.26.1
RUN yum install gcc make pcre-devel openssl-devel -y && \
./configure --prefix=/usr/local/nginx --with-http_ssl_module \
--with-http_stub_status_module && make && make install && \
yum clean all && rm -rf /mnt/nginx-1.26.1
EXPOSE 80 443
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off"]

[root@rockynode-1 dockerfile]# docker build -t centos:v3 .
[+] Building 36.7s (9/9) FINISHED                                                                                               docker:default
 => [internal] load build definition from Dockerfile                                                                                      0.0s
 => => transferring dockerfile: 520B                                                                                                      0.0s
 => WARN: LegacyKeyValueFormat: "LABEL key=value" should be used instead of legacy "LABEL key value" format (line 2)                      0.0s
 => [internal] load metadata for docker.io/library/centos7:repo                                                                           0.0s
 => [internal] load .dockerignore                                                                                                         0.0s
 => => transferring context: 2B                                                                                                           0.0s
 => [internal] load build context                                                                                                         0.0s
 => => transferring context: 102B                                                                                                         0.0s
 => [1/4] FROM docker.io/library/centos7:repo                                                                                             0.0s
 => [2/4] ADD nginx-1.26.1.tar.gz /mnt                                                                                                    0.3s
 => [3/4] WORKDIR /mnt/nginx-1.26.1                                                                                                       0.0s
 => [4/4] RUN yum install gcc make pcre-devel openssl-devel -y && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-h  36.0s
 => exporting to image                                                                                                                    0.3s 
 => => exporting layers                                                                                                                   0.3s 
 => => writing image sha256:68cf4567b2da65477bf5bfcb64ec9e8878a8d13fa8a3a593eb51a2ff7e1ec886                                              0.0s 
 => => naming to docker.io/library/centos:v3                                                                                              0.0s 
                                                                                                                                               
 1 warning found (use docker --debug to expand):                                                                                               
 - LegacyKeyValueFormat: "LABEL key=value" should be used instead of legacy "LABEL key value" format (line 2)
[root@rockynode-1 dockerfile]# docker images 
REPOSITORY           TAG           IMAGE ID       CREATED              SIZE
centos               v3            68cf4567b2da   8 seconds ago        292MB
centos7              repo          9d9f5c6e1915   About a minute ago   204MB
centos               repo          5726ef394719   2 hours ago          228MB
nginx                1.26-alpine   9703b2608a98   12 days ago          43.3MB
nginx                latest        5ef79149e0ec   12 days ago          188MB
busybox              latest        65ad0d468eb1   15 months ago        4.26MB
centos               7             eeb6ee3f44bd   2 years ago          204MB
timinglee/game2048   latest        19299002fdbe   7 years ago          55.5MB
timinglee/mario      latest        9a35a9e43e8c   8 years ago          198MB

2.3.3 镜像优化示例

# 编译安装安装
FROM centos:repo AS build
LABEL mail = shuyan@123
ADD nginx-1.26.1.tar.gz /mnt
WORKDIR /mnt/nginx-1.26.1
RUN yum install gcc make pcre-devel openssl-devel -y && \
./configure --prefix=/usr/local/nginx --with-http_ssl_module \
--with-http_stub_status_module && make && make install && \
yum clean all && rm -rf /mnt/nginx-1.26.1

# 软件迁移
FROM centos:repo
COPY --from=build /usr/local/nginx /usr/local/nginx
EXPOSE 80 443
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]

为了验证这一说法可以制作提交两个镜像

dockerfile]# docker commit -m "ngin_2" nginx_centos centos2:repo
dockerfile]# docker commit -m "ngin_2" nginx_centos centos3:repo
dockerfile]# docker images

[root@rockynode-1 dockerfile]# docker build -t centos:v4 .
[+] Building 0.1s (12/12) FINISHED                                                                                              docker:default
 => [internal] load build definition from Dockerfile                                                                                      0.0s
 => => transferring dockerfile: 601B                                                                                                      0.0s
 => WARN: LegacyKeyValueFormat: "LABEL key=value" should be used instead of legacy "LABEL key value" format (line 2)                      0.0s
 => [internal] load metadata for docker.io/library/centos2:repo                                                                           0.0s
 => [internal] load metadata for docker.io/library/centos3:repo                                                                           0.0s
 => [internal] load .dockerignore                                                                                                         0.0s
 => => transferring context: 2B                                                                                                           0.0s
 => [internal] load build context                                                                                                         0.0s
 => => transferring context: 102B                                                                                                         0.0s
 => [stage-1 1/2] FROM docker.io/library/centos3:repo                                                                                     0.0s
 => [build 1/4] FROM docker.io/library/centos2:repo                                                                                       0.0s
 => CACHED [build 2/4] ADD nginx-1.26.1.tar.gz /mnt                                                                                       0.0s
 => CACHED [build 3/4] WORKDIR /mnt/nginx-1.26.1                                                                                          0.0s
 => CACHED [build 4/4] RUN yum install gcc make pcre-devel openssl-devel -y && ./configure --prefix=/usr/local/nginx --with-http_ssl_mod  0.0s
 => CACHED [stage-1 2/2] COPY --from=build /usr/local/nginx /usr/local/nginx                                                              0.0s
 => exporting to image                                                                                                                    0.0s
 => => exporting layers                                                                                                                   0.0s
 => => writing image sha256:7f2c20fc731e9511b999d5786b03f4d697d9ce6204844dc32f111dbc63cde423                                              0.0s
 => => naming to docker.io/library/centos:v4                                                                                              0.0s

 1 warning found (use docker --debug to expand):
 - LegacyKeyValueFormat: "LABEL key=value" should be used instead of legacy "LABEL key value" format (line 2)

启动容器查看尝试是否能连接

dockerfile]# docker run -it -d -p 80:80 --name=nginx_new centos:v4
6783951e1fae546aafbd0c76f2c00c189e78b02c4bf01937177c57465153cc1c

dockerfile]# docker ps 
CONTAINER ID   IMAGE       COMMAND                   CREATED         STATUS         PORTS                                        NAMES
6783951e1fae   centos:v4   "/usr/local/nginx/sb…"   5 seconds ago   Up 4 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp   nginx_new

  • 14
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

妍妍的宝贝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值