I'm trying to base a Dockerfile
on another local one.
$ ls -lR total 0 -rw-r--r-- 1 me me 42 14 avr 10:42 Dockerfile drwxr-xr-x 3 me me 42 14 avr 10:42 prod ./prod: total 0 -rw-r--r-- 1 me me 42 14 avr 10:42 Dockerfile $ cat prod/Dockerfile FROM ../Dockerfile ... $ docker build - < prod/Dockerfile
unable to process Dockerfile: unable to parse repository info: repository name component must match "a-z0-9(?:[._]a-z0-9)*"
I know that FROM
expects an image and not a path.
How can I extend Dockerfile
from prod/Dockerfile
?
A Dockerfile must be created with no extension. To do this in Windows, create the file with your editor of choice, then save it with the notation "Dockerfile" (including the quotes).
A Dockerfile has no extension . if your using docker on docker on windows use notepad ++ to create a dockerfile while saving select “All type “ and save the file name as “Dockerfile”.
Using multi-stage dockerfiles, you can use several base images as well as previous intermediate image layers to build a new image layer.
Dockerfiles don't extend Dockerfiles but images, the FROM
line is not an "include" statement.
So, if you want to "extend" another Dockerfile, you need to build the original Dockerfile as an image, and extend that image.
For example;
Dockerfile1:
FROM alpine RUN echo "foo" > /bar
Dockerfile2:
FROM myimage RUN echo "bar" > /baz
Build the first Dockerfile (since it's called Dockerfile1
, use the -f
option as docker defaults to look for a file called Dockerfile
), and "tag" it as myimage
docker build -f Dockerfile1 -t myimage . # Sending build context to Docker daemon 3.072 kB # Step 1 : FROM alpine # ---> d7a513a663c1 # Step 2 : RUN echo "foo" > /bar # ---> Running in d3a3e5a18594 # ---> a42129418da3 # Removing intermediate container d3a3e5a18594 # Successfully built a42129418da3
Then build the second Dockerfile, which extends the image you just built. We tag the resulting image as "myextendedimage";
docker build -f Dockerfile2 -t myextendedimage . # Sending build context to Docker daemon 3.072 kB # Step 1 : FROM myimage # ---> a42129418da3 # Step 2 : RUN echo "bar" > /baz # ---> Running in 609ae35fe135 # ---> 4ea44560d4b7 # Removing intermediate container 609ae35fe135 # Successfully built 4ea44560d4b7
To check the results, run a container from the image and verify that both files (/bar
and /baz
) are in the image;
docker run -it --rm myextendedimage sh -c "ls -la ba*" # -rw-r--r-- 1 root root 4 Apr 14 10:18 bar # -rw-r--r-- 1 root root 4 Apr 14 10:19 baz
I suggest to read the User guide, which explains how to work with images and containers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With