Dockerfile - 中间容器到底在做什么? [英] Dockerfile - What are the intermediate containers doing exactly?

查看:25
本文介绍了Dockerfile - 中间容器到底在做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的 Dockerfile,它看起来像:

I have my Dockerfile, which looks like:

FROM confluentinc/cp-kafka-connect:4.0.0

ARG VERSION=0.0.2.15

RUN curl -Ls https://github.com/jcustenborder/kafka-connect-rabbitmq/releases/download/$VERSION/kafka-connect-rabbitmq-$VERSION.tar.gz | tar -xzC /tmp && 
mkdir -p /etc/kafka-connect/jars && 
cp -R /tmp/usr/share/kafka-connect/kafka-connect-rabbitmq/. /etc/kafka-connect/jars && 
ls -l /etc/kafka-connect/jars

RUN ls -l /etc/kafka-connect/jars

现在我面临的问题是,当我第一次执行 ls 时,我可以确认 JAR 已按预期复制.

Now the issue I am facing is when I do that first ls I can confirm that the JARs have been copied across as I'd expect.

但是,当我在 RUN 行中执行第二个 ls 时,它们不会显示(随后当我运行映像时它们不会出现在我的容器中).

However when I do that second ls in the RUN line, they are not shown (and subsequently they do not appear in my container when I run the image).

所以我想了解为什么会这样?我是否错误地假设在每一行之后传递图像 - 因为情况似乎并非如此.

So I'd like to understand why is this happening? Am i wrong to assume after each line the image is passed along - as this doesnt seem the case.

仅供参考 - 这是我控制台的完整输出:

FYI - this is the full output from my console:

Sending build context to Docker daemon   2.56kB
Step 1/4 : FROM confluentinc/cp-kafka-connect:4.0.0
 ---> 4db60f092134
Step 2/4 : ARG VERSION=0.0.2.15
 ---> Using cache
 ---> dc641b6beb04
Step 3/4 : RUN curl -Ls https://github.com/jcustenborder/kafka-connect-rabbitmq/releases/download/$VERSION/kafka-connect-rabbitmq-$VERSION.tar.gz | tar -xzC /tmp &&     cp /tmp/usr/share/kafka-connect/kafka-connect-rabbitmq/kafka-connect-rabbitmq-$VERSION.jar /usr/share/java/ &&     mkdir -p /etc/kafka-connect/jars &&     cp /tmp/usr/share/kafka-connect/kafka-connect-rabbitmq/*.jar /etc/kafka-connect/jars &&     ls -l /etc/kafka-connect/jars
 ---> Running in f48dbf0e487e
total 6804
-rw-r--r-- 1 root root  491199 Apr  2 03:48 amqp-client-4.2.0.jar
-rw-r--r-- 1 root root   74557 Apr  2 03:48 annotations-2.0.1.jar
-rw-r--r-- 1 root root  100811 Apr  2 03:48 connect-utils-0.3.101.jar
-rw-r--r-- 1 root root    7046 Apr  2 03:48 connect-utils-testing-data-0.3.101.jar
-rw-r--r-- 1 root root 1493680 Apr  2 03:48 freemarker-2.3.25-incubating.jar
-rw-r--r-- 1 root root 2256213 Apr  2 03:48 guava-18.0.jar
-rw-r--r-- 1 root root   55784 Apr  2 03:48 jackson-annotations-2.8.0.jar
-rw-r--r-- 1 root root  281079 Apr  2 03:48 jackson-core-2.8.5.jar
-rw-r--r-- 1 root root 1236315 Apr  2 03:48 jackson-databind-2.8.5.jar
-rw-r--r-- 1 root root  749499 Apr  2 03:48 javassist-3.19.0-GA.jar
-rw-r--r-- 1 root root   31212 Apr  2 03:48 kafka-connect-rabbitmq-0.0.2.15.jar
-rw-r--r-- 1 root root  129763 Apr  2 03:48 reflections-0.9.10.jar
-rw-r--r-- 1 root root   41071 Apr  2 03:48 slf4j-api-1.7.21.jar
Removing intermediate container f48dbf0e487e
 ---> ad2ca0767def
Step 4/4 : RUN ls -l /etc/kafka-connect/jars
 ---> Running in c0b5fda45249
total 0
Removing intermediate container c0b5fda45249
 ---> 5fef032d5aba
Successfully built 5fef032d5aba
Successfully tagged myfirstimage:latest

我必须使用某种finalise"命令吗?

Do I have to use some sort of 'finalise' command?

任何帮助将不胜感激.

谢谢.

推荐答案

我的猜测是 /etc/kafka-connect/jars 目录被声明为 VOLUME在该图像的 Dockerfile 中.

My guess is that the /etc/kafka-connect/jars directory is declared as a VOLUME in that image's Dockerfile.

docker inspect 命令的输出证实了我的猜测:

And the output of the docker inspect command confirms my guess:

$ docker image inspect confluentinc/cp-kafka-connect:4.0.0 --format '{{.Config.Volumes}}'
map[/etc/kafka-connect/secrets:{} /etc/kafka/secrets:{} /var/lib/kafka/data:{} /etc/kafka-connect/jars:{}]

引用 Dockerfile 规范:

如果任何构建步骤在声明后更改卷中的数据,这些更改将被丢弃.

If any build steps change the data within the volume after it has been declared, those changes will be discarded.

那么,以下是有关您的问题的详细信息:

So, here are the details about your problem:

  1. 基础镜像声明VOLUME/etc/kafka-connect/jars.
  2. 在 Dockerfile 的第 3 步中,您更改了该目录的内容.这就是为什么这一步中的ls命令可以正常工作的原因.
  3. 然后这些更改将被丢弃.
  1. The base image declares VOLUME /etc/kafka-connect/jars.
  2. In step 3 of your Dockerfile, you changed the contents of that directory. That's why the ls command in this step works normally.
  3. Then these changes are discarded.

解决办法是把jar文件放到你的宿主机上,运行容器的时候把宿主机目录绑定挂载到容器上.如下:

The solution is to put the jar files on your host, and bind-mount the host directory to the container when running the container. As below:

docker run -v /path/contains/jar/files:/etc/kafka-connect/jars <IMAGE>

这篇关于Dockerfile - 中间容器到底在做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆