Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending local Dockerfile

Tags:

docker

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 ?

like image 660
Pierre de LESPINAY Avatar asked Apr 14 '16 08:04

Pierre de LESPINAY


People also ask

How do I create a Dockerfile extension?

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).

What extension is used for Dockerfile?

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”.

Can we have 2 base images in Dockerfile?

Using multi-stage dockerfiles, you can use several base images as well as previous intermediate image layers to build a new image layer.


1 Answers

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

like image 156
thaJeztah Avatar answered Sep 20 '22 22:09

thaJeztah