前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Docker生成新镜像版本的两种方式

Docker生成新镜像版本的两种方式

原创
作者头像
葫芦
发布2021-07-03 20:15:07
1.6K0
发布2021-07-03 20:15:07
举报
文章被收录于专栏:葫芦葫芦

Docker生成新镜像版本的两种方式

There are two ways Docker can generate new mirrored versions

方式一:通过修改镜像生成新版本镜像

Method 1: generate a new version of the image by modifying the image

  • 查看现有镜像版本

View the existing image version

代码语言:txt
复制
$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED             SIZE
link         v12       baa606cdbecb   About an hour ago   78.4MB
link         v11       eadbb1c71db6   5 weeks ago         78.4MB
  • 进入镜像修改文件

Enter the image modification file

代码语言:txt
复制
$ docker run --name link -d -p  80:80 link:v12
30f0e4c4545934e9b62b9c282b482232635f4e23854a55350b6d0071d004cfed

$ docker exec -it 30f0e4c45459 sh
/ # sed -i 's/wang/zi.wang/g' /etc/nginx/conf.d/default.conf
/ # cat /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

    location / {
        root /usr/src/app/;
        index  index.html index.htm;

        gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript;
        gzip on;

        if_modified_since off;
        etag off;
        add_header Last-Modified "zi.wang";

        if (!-e $request_filename) {
            rewrite ^/[^.]+$  /index.html break;
        }
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
/ # exit
  • 提交镜像版本

Submit mirrored version

代码语言:txt
复制
$ docker commit -m='add header' -a='wangzi'  30f0e4c45459  link:v13
sha256:60752f362f4e8f856e94133ce2d56393fa9834eb53c2b94b74f6f654f7863cfb

$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED             SIZE
link         v13       60752f362f4e   5 seconds ago       78.4MB
link         v12       baa606cdbecb   About an hour ago   78.4MB
link         v11       eadbb1c71db6   5 weeks ago         78.4MB
  • 运行镜像并验证

Run the image and verify it

代码语言:txt
复制
$ docker run -d -p 8080:80 link:v13
a6c5e1f7cca9d93cc68b7cfefe24fed0ad981f14ae78aeb36c66c79a17ca2d92

$ docker ps -a
CONTAINER ID   IMAGE      COMMAND                  CREATED              STATUS              PORTS                  NAMES
a6c5e1f7cca9   link:v13   "/docker-entrypoint.…"   3 seconds ago        Up 2 seconds        0.0.0.0:8080->80/tcp   tender_goodall

$curl -i 127.0.0.1:8080
HTTP/1.1 200 OK
Server: nginx/1.21.0
Date: Sat, 03 Jul 2021 11:51:58 GMT
Content-Type: text/html
Content-Length: 514
Connection: keep-alive
Last-Modified: zi.wang
Accept-Ranges: bytes
  • 最终看到新版本中 response haader的 last-modified属性已经被修改了.

Finally, you can see that the Last-Modified property of Response Haader has been modified in the new version.

方式二:通过Dockerfile生成新版本

Method 2: Generate a new version via Dockerfile

  • 创建Dockerfile

Create Dockerfile

Dockerfile

代码语言:txt
复制
FROM nginx:mainline-alpine-perl

COPY ./nginx.conf /etc/nginx/conf.d/default.conf
ADD ./dist/ /usr/src/app/

# Define default command.
CMD ["nginx", "-g", "daemon off;"]

# Expose ports.
EXPOSE 80
  • 创建build&run脚本

Create the build&run script

build_run.sh

代码语言:txt
复制
#!/bin/bash
npm run build
docker build -t link:$1 .
docker run  --name link -d -p 80:80 link:$1
  • 运行build&run脚本

Run the build&run script

build command

代码语言:txt
复制
 bash build_run.sh v11
  • 检查镜像仓库是否生成了新的镜像版本

Check that the image repository has generated a new version of the image

check

代码语言:txt
复制
$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
link         v11       eadbb1c71db6   5 

$ docker ps -a
CONTAINER ID   IMAGE      COMMAND                  CREATED              STATUS                            PORTS                NAMES
5cba6642ce99   link:v11   "/docker-entrypoint.…"   About a minute ago   Exited (127) About a minute ago                        angry_carver

葫芦的运维日志-王梓个人博客

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Docker生成新镜像版本的两种方式
  • There are two ways Docker can generate new mirrored versions
    • 方式一:通过修改镜像生成新版本镜像
      • Method 1: generate a new version of the image by modifying the image
        • 方式二:通过Dockerfile生成新版本
          • Method 2: Generate a new version via Dockerfile
          相关产品与服务
          容器镜像服务
          容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档