Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build multiple images from multiple dockerfile

Is there any way to build the multiple images by managing two different dockerfiles? In my case I want to keep two dockerfile suppose Dockerfile_app1 Dockerfile_app2 within the build context.

docker build -t <image_name> .

The above will pick the dockerfile named as Dockerfile

docker build -t <image_name> Dockerfile_app1

This is also not working for my case as It's expecting the file name as Dockerfile.

I have tried by docker-compose build also. However it din't work.

app1:
  build: Dockerfile_app1
  ports:
   - "80:80"
app2:
  build: Dockerfile_app2
  ports:
   - "80:80"
like image 468
Mahattam Avatar asked Jul 08 '15 12:07

Mahattam


People also ask

Can a Dockerfile build multiple images?

A multistage build allows you to use multiple images to build a final product. In a multistage build, you have a single Dockerfile, but can define multiple images inside it to help build the final image.

How do I pull multiple pictures at once on docker?

Pull a repository with multiple images By default, docker pull pulls a single image from the registry. A repository can contain multiple images. To pull all images from a repository, provide the -a (or --all-tags ) option when using docker pull .

Can I have multiple Dockerfile?

As Kingsley Uchnor said, you can have multiple Dockerfile , one per directory, which represent something you want to build.

Can a docker container have multiple images?

Multiple containers can run simultaneously, each based on the same or different images. Docker is similar to virtual machines in the way it creates multiple instances of an operating system.


1 Answers

Just use the -f argument to docker build to specify the name of the Dockerfile to use:

$ docker build -t <image_name> -f Dockerfile_app1 .
...

Or in Compose you can use the dockerfile key from version 1.3 onwards:

app1:
  build: .
  dockerfile: Dockerfile_app1
  ports:
   - "80:80"
app2:
  build: .
  dockerfile: Dockerfile_app2
  ports:
   - "80:80"

Note that the build key is for the build context, not the name of the Dockerfile (so it looked for a directory called Dockerfile_app1 in your case).

like image 96
Adrian Mouat Avatar answered Oct 11 '22 23:10

Adrian Mouat