浏览量 2825
2021/07/03 19:59
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
$ 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
$ 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
$ 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
$ 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
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
#!/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
bash build_run.sh v11
- 检查镜像仓库是否生成了新的镜像版本
Check that the image repository has generated a new version of the image
check
$ 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
上一篇
搜索
下一篇