tech687
(Tech687)
January 30, 2025, 12:19pm
1
+ 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?
cavo789
(Christophe Avonture)
January 30, 2025, 5:26pm
2
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.
tech687
(Tech687)
January 30, 2025, 7:48pm
3
+ 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.
bluepuma77
(Bluepuma77)
January 30, 2025, 8:03pm
4
Please explain what you are trying to achieve.
cavo789
(Christophe Avonture)
January 30, 2025, 8:30pm
5
Try with the exact same command and thus -v .:/test
(hello is indeed not a directory but a file)
tech687
(Tech687)
January 31, 2025, 8:47am
6
+ docker run --rm -v .:/test test cat /test/hello
cat: can't open '/test/hello': No such file or directory
tech687
(Tech687)
January 31, 2025, 9:00am
7
Continue to follow the discussion.
When the time is, I will come to the point.
cavo789
(Christophe Avonture)
January 31, 2025, 12:37pm
8
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?)
tech687
(Tech687)
January 31, 2025, 3:43pm
9
Let me go, but does it work to you?
yes or no?
cavo789
(Christophe Avonture)
January 31, 2025, 4:32pm
10
Can I see your hello content ? Yes
Can I see mine after having mounted my volume ? Yes
tech687
(Tech687)
January 31, 2025, 5:41pm
11
++ 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?
bluepuma77
(Bluepuma77)
February 6, 2025, 10:29pm
12
You start the container and mount the local hello file into the container, it will “overwrite” the file in the built image at runtime.
tech687
(Tech687)
February 7, 2025, 8:39am
13
Specifically?
A concrete example?
What commands should I give?
bluepuma77
(Bluepuma77)
February 9, 2025, 6:54pm
14
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.
tech687
(Tech687)
February 10, 2025, 9:02am
15
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.
bluepuma77
(Bluepuma77)
February 10, 2025, 1:11pm
16
The Dockerfile is used to build the Docker image. Then a container is run based on that image.