Docker的镜像制作小试验

1) 用docker commit 制作一个nginx镜像,定制主页文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
root@ubuntu2204:~# apt update 
root@ubuntu2204:~# apt install -y docker.io
root@ubuntu2204:~# docker pull nginx:1.24.0

root@ubuntu2204:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.24.0 6c0218f16876 13 months ago 142MBtu2204:~# docker pull nginx:1.24.0

root@ubuntu2204:~# docker run -d --name nginx -p 80:80 nginx:1.24.0
9d8f1f98879be8b561f29dd78aa4291e3e8c3adc44b3313fc909bb6ecb12fee6

root@ubuntu2204:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9d8f1f98879b nginx:1.24.0 "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nginx

root@ubuntu2204:~# docker exec -it nginx bash
root@9d8f1f98879b:/# nginx -T
root@9d8f1f98879b:/# echo "My Nginx images with nginx:1.24.0" > /usr/share/nginx/html/index.html
root@9d8f1f98879b:/# cat /usr/share/nginx/html/index.html
My Nginx images with nginx:1.24.0
root@9d8f1f98879b:/# exit

root@ubuntu2204:~# curl 10.0.0.7
My Nginx images with nginx:1.24.0

root@ubuntu2204:~# docker commit nginx mynginx:1.24.0
sha256:6cdae199f0ed257a7725af4f11a33a962fe7d23eb72db849b5755d62f7cb13b8

root@ubuntu2204:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mynginx 1.24.0 6cdae199f0ed 8 seconds ago 142MB
nginx 1.24.0 6c0218f16876 13 months ago 142MB

2) 用docker build 基于alpine镜像,自定义镜像Label\

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
root@ubuntu2204:~# mkdir -p /data/dockerfile/alpine
root@ubuntu2204:~# cd /data/dockerfile/alpine

root@ubuntu2204:/data/dockerfile/alpine# vim Dockerfile
FROM alpine:20240329
LABEL Author=xcjyc Description="docker build" Data="2024-05-22"

root@ubuntu2204:/data/dockerfile/alpine# docker build -t myalpine:for20240329 .
DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
Install the buildx component to build images with BuildKit:
https://docs.docker.com/go/buildx/

Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM alpine:20240329
20240329: Pulling from library/alpine
4edfc05e3af2: Pull complete
Digest: sha256:e31c3b1cd47718260e1b6163af0a05b3c428dc01fa410baf72ca8b8076e22e72
Status: Downloaded newer image for alpine:20240329
---> 49b3cb3043cd
Step 2/2 : LABEL Author=xcjyc Description="docker build" Data="2024-05-22"
---> Running in 2fc73db792f6
Removing intermediate container 2fc73db792f6
---> d1a87b0d00c6
Successfully built d1a87b0d00c6
Successfully tagged myalpine:for20240329

3)Dockerfile 加入一个前台持续执行的进程

1
2
3
4
root@ubuntu2204:/data/dockerfile/alpine# cat Dockerfile 
FROM alpine:20240329
LABEL Author=xcjyc Description="docker build" Data="2024-05-22"
CMD tail -f /etc/hosts

Docker的镜像制作小试验
https://www.xcjyc.top/2024/06/04/Docker的镜像制作小试验/
作者
XCJYC
发布于
2024年6月4日
许可协议