当前位置: 首页 > news >正文

制作 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.0s1 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.0s1 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.0s3 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     198MBdockerfile]# 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.0s2 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.0s2 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.0s2 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      198MBdockerfile]# 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 httpdListen 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.0s1 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
6783951e1fae546aafbd0c76f2c00c189e78b02c4bf01937177c57465153cc1cdockerfile]# 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

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 有关树形结构数据的功能函数
  • Uniapp 调用aar、jar包
  • 什么是Jmeter ?Jmeter使用的原理步骤是什么?
  • Cobalt Strike 4.8 用户指南-第五节-获取初始访问
  • [数据集][目标检测]玻璃瓶塑料瓶检测数据集VOC+YOLO格式8943张2类别
  • 猫咪浮毛清理措施?希喂、安德迈、有哈宠物空气净化器数据大揭秘
  • html+css+js网页设计 翘珠宝微商城移动端20个页面
  • 正则表达式实现带有条件的爬取
  • .net dataexcel winform控件 更新 日志
  • Linux - 深入探讨 Linux `ls` 命令:一个全面的技术指南
  • 【前端面试】采用react前后,浏览器-解析渲染UI的变化
  • cnocr 安装
  • OpenHarmony使用ArkUI Inspector分析布局
  • 一套高效、稳定的自卸车自动充电系统
  • 3.7 Browser -- useMediaQuery
  • 【面试系列】之二:关于js原型
  • 002-读书笔记-JavaScript高级程序设计 在HTML中使用JavaScript
  • CoolViewPager:即刻刷新,自定义边缘效果颜色,双向自动循环,内置垂直切换效果,想要的都在这里...
  • Docker入门(二) - Dockerfile
  • Javascript基础之Array数组API
  • KMP算法及优化
  • Linux下的乱码问题
  • Making An Indicator With Pure CSS
  • Redis中的lru算法实现
  • Sass 快速入门教程
  • seaborn 安装成功 + ImportError: DLL load failed: 找不到指定的模块 问题解决
  • Shell编程
  • Spring Cloud Alibaba迁移指南(一):一行代码从 Hystrix 迁移到 Sentinel
  • tab.js分享及浏览器兼容性问题汇总
  • 第三十一到第三十三天:我是精明的小卖家(一)
  • 和 || 运算
  • 如何用Ubuntu和Xen来设置Kubernetes?
  • 深度学习入门:10门免费线上课程推荐
  • gunicorn工作原理
  • postgresql行列转换函数
  • 阿里云重庆大学大数据训练营落地分享
  • 回归生活:清理微信公众号
  • ​【经验分享】微机原理、指令判断、判断指令是否正确判断指令是否正确​
  • (二)c52学习之旅-简单了解单片机
  • (附源码)ssm高校运动会管理系统 毕业设计 020419
  • (附源码)ssm高校志愿者服务系统 毕业设计 011648
  • (附源码)计算机毕业设计ssm基于B_S的汽车售后服务管理系统
  • (原創) 人會胖會瘦,都是自我要求的結果 (日記)
  • (转)Sublime Text3配置Lua运行环境
  • (转载)(官方)UE4--图像编程----着色器开发
  • (转载)Google Chrome调试JS
  • ./configure,make,make install的作用
  • .NET Compact Framework 3.5 支持 WCF 的子集
  • .net core Redis 使用有序集合实现延迟队列
  • .net MySql
  • .NET和.COM和.CN域名区别
  • .net开发引用程序集提示没有强名称的解决办法
  • .NET企业级应用架构设计系列之技术选型
  • .py文件应该怎样打开?
  • :O)修改linux硬件时间