您应该运行最后获得的图像。不要指望 Dockerfile 会为你做这件事。
让我解释一下:
首先让我们回顾一下什么是图像。
图像 = 文件系统快照 + 启动命令。
这里重述了完整的故事:
您的 Dockerfile 中有 3 条指令(FROM、RUN、CMD)。
当您“docker build.”时,会发生以下情况:
$ docker build .
[+] Building 1.8s (6/6) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 37B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/alpine:latest 1.7s
=> [1/2] FROM docker.io/library/alpine@sha256:ec14c7992a97fc11425907e908340c6c3d6ff602f5f13d899e6b7027c9b4133a 0.0s
FROM 指令:这里是生成带有文件系统的 alpine 镜像。图像 id 显示在“sha256:”之后。我们称它为图像 A。
=> CACHED [2/2] RUN apk add --update redis 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:45e4f46a107d5b213a2b325204322e52ac4e4eeeb2282ba66e5c6455310ad282
RUN指令:获取上一步生成的图像(图像A)。它从中创建一个容器,并在此容器中执行命令。现在应该修改文件系统(由于包管理器 apk 添加了 redis)。从此容器的文件系统中获取快照并保存为下一条指令的输出(下一条是 CMD)。此输出是一个图像,其 id 显示在第二个“sha256:”之后。我们称它为图像 B。
现在图像 B 包含:文件系统快照(内部有 redis)+ 启动命令(redis-server)。
此图像 (B) 现在可以运行了。结果将是一个容器:
$ docker build run image_B
1:C 07 Apr 2021 09:40:13.168 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 07 Apr 2021 09:40:13.168 # Redis version=6.0.11, bits=64, commit=1522534f, modified=0, pid=1, just started
1:C 07 Apr 2021 09:40:13.168 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 07 Apr 2021 09:40:13.170 * Running mode=standalone, port=6379.
1:M 07 Apr 2021 09:40:13.170 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 07 Apr 2021 09:40:13.170 # Server initialized
1:M 07 Apr 2021 09:40:13.171 * Ready to accept connections