Problem with Multi-stage builds and mount point

+ cat Dockerfile \
FROM php

COPY <<EOF /test/hello
Hello!!!
EOF

FROM bash
COPY --from=0 /test/hello /test/hello



+ docker build -t test . \
[+] Building 0.1s (10/10) FINISHED

Now a little attention

+ docker run --rm test cat /test/hello \
Hello!!!

OK

+ docker run --rm -v .:/test test cat /test/hello \
cat: can't open '/test/hello': No such file or directory

Why?

It’s… normal.

Using -v .:/test you don’t access anymore the test folder inside the image. You’ve mounted your current host folder as /test.

On your host, create a file called hello with a fake content and try again your last command it’ll become explicit I think.

+ tree -p 
[drwxrwxr-x]  .
├── [-rw-rw-r--]  Dockerfile
└── [-rw-rw-r--]  hello



+ cat Dockerfile 
FROM php AS basic

COPY <<EOF /test/hello
Hello!!!
EOF

FROM bash
COPY --from=basic /test/hello /test/hello



+ docker build -t test . 
[+] Building 0.1s (10/10) FINISHED                                                                      docker:default
 => [internal] load build definition from Dockerfile                                                              0.0s
 => => transferring dockerfile: 145B                                                                              0.0s
 => [internal] load metadata for docker.io/library/bash:latest                                                    0.0s
 => [internal] load metadata for docker.io/library/php:latest                                                     0.0s
 => [internal] load .dockerignore                                                                                 0.0s
 => => transferring context: 2B                                                                                   0.0s
 => [internal] preparing inline document                                                                          0.0s
 => [stage-1 1/2] FROM docker.io/library/bash:latest                                                              0.0s
 => [basic 1/2] FROM docker.io/library/php:latest                                                                 0.0s
 => CACHED [basic 2/2] COPY <<EOF /test/hello                                                                     0.0s
 => CACHED [stage-1 2/2] COPY --from=basic /test/hello /test/hello                                                0.0s
 => exporting to image                                                                                            0.0s
 => => exporting layers                                                                                           0.0s
 => => writing image sha256:f2b750492139cd1bb50c1e662223d0a9a94664d853d484a852ec926288d7f9df                      0.0s
 => => naming to docker.io/library/test                                                                           0.0s



+ docker run --rm -v hello:/test/hello test cat /test/hello 
docker: Error response from daemon: source /var/lib/docker/overlay2/a4abfa60b77d53ac5ef92f87f89a250380df4623272f6250008be0c11fc74f9b/merged/test/hello is not directory.

Please explain what you are trying to achieve.

Try with the exact same command and thus -v .:/test

(hello is indeed not a directory but a file)

+ docker run --rm -v .:/test test cat /test/hello
cat: can't open '/test/hello': No such file or directory

Continue to follow the discussion.
When the time is, I will come to the point.

Bonjour

I’ve done exactly as said i.e. from your first post, I’ve taken the Dockerfile, fired the docker build command, the docker “cat” to see Hello!

Then, on my computer, I’ve created “hello” file with “Hello from the host” and fired “docker run --rm -v .:/test test cat /test/hello” and, yeah, exactly as stated in the second post.

Please, s’il vous plaît, de rien (politeness is a thing of the past, isn’t it?)

Let me go, but does it work to you?
yes or no?

Can I see your hello content ? Yes
Can I see mine after having mounted my volume ? Yes

++ echo 'Bye!!!' > hello


++ tree
├── Dockerfile
└── hello


++ cat hello
Bye!!!


++ cat Dockerfile
FROM php AS basic

COPY <<EOF /test/hello
Hello!!!
EOF

FROM bash
COPY --from=basic /test/hello /test/hello

To notice

COPY <<EOF /test/hello
Hello!!!
EOF

This instruction should overwrite the original content (Bye!!!) of the file [./hello → /test/hello], with the new one (Hello!!!).

./hello: INTO HOST
/test/hello: INTO DOCKER

++ docker build -t test .
[+] Building 0.1s (10/10) FINISHED                                                                      docker:default
 => [internal] load build definition from Dockerfile                                                              0.0s
 => => transferring dockerfile: 145B                                                                              0.0s
 => [internal] load metadata for docker.io/library/bash:latest                                                    0.0s
 => [internal] load metadata for docker.io/library/php:latest                                                     0.0s
 => [internal] load .dockerignore                                                                                 0.0s
 => => transferring context: 2B                                                                                   0.0s
 => [internal] preparing inline document                                                                          0.0s
 => [basic 1/2] FROM docker.io/library/php:latest                                                                 0.0s
 => [stage-1 1/2] FROM docker.io/library/bash:latest                                                              0.0s
 => CACHED [basic 2/2] COPY <<EOF /test/hello                                                                     0.0s
 => CACHED [stage-1 2/2] COPY --from=basic /test/hello /test/hello                                                0.0s
 => exporting to image                                                                                            0.0s
 => => exporting layers                                                                                           0.0s
 => => writing image sha256:f2b750492139cd1bb50c1e662223d0a9a94664d853d484a852ec926288d7f9df                      0.0s
 => => naming to docker.io/library/test                                                                           0.0s




++ docker run --rm -v .:/test test cat /test/hello
Bye!!!

Bye!!!

But I expected

Hello!!!

Why?

You start the container and mount the local hello file into the container, it will “overwrite” the file in the built image at runtime.

Specifically?
A concrete example?
What commands should I give?

You build an image and run a container. But you overwrite the files inside the container with a bind mount from host.

The output you see is expected from my point of view. I am not sure what you expected.

You still have not told us what you want to achieve.

Let me clarify.

Is the container started first, or, from what I understand, is the image created?

What is in the Dockerfile, is it executed at runtime (what do you mean by runtime?), or, from what I understand, at image creation?

What do you mean by

bind mount from host

Nothing specific at the moment.
I’m just experimenting a bit.

The Dockerfile is used to build the Docker image. Then a container is run based on that image.