Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple images, one Dockerfile

Tags:

How to create two images in one Dockerfile, they only copy different files.

Shouldn't this produce two images img1 & img2, instead it produces two unnamed images d00a6fc336b3 & a88fbba7eede

Dockerfile:

FROM alpine as img1
COPY file1.txt .

FROM alpine as img2
COPY file2.txt .

Instead this is the result of docker build .

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              d00a6fc336b3        4 seconds ago       4.15 MB
<none>              <none>              a88fbba7eede        5 seconds ago       4.15 MB
alpine              latest              3fd9065eaf02        3 months ago        4.15 MB
like image 252
John Avatar asked Apr 10 '18 12:04

John


People also ask

Can one Dockerfile build multiple images?

Use of two commands – FROM and AS, in particular, allows you to create a multi-stage dockerfile. It allows you to create multiple image layers on top of the previous layers and the AS command provides a virtual name to the intermediate image layer.

Can you combine 2 docker images?

Docker doesn't do merges of the images, but there isn't anything stopping you combining the dockerfiles if available, and rolling into them into a fat image which you'd need to build.

Can I have multiple from in Dockerfile?

With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don't want in the final image.

Can a single container have multiple images?

Can we have a single container with multiple image like nginx + Redis + alpine ? You can have multiple processes inside a container but it wouldn't really be a best practice. Having NGINX and Redis in separate containers but inside the same pod would be better.


1 Answers

You can use a docker-compose file using the target option:

version: '3.4'
services:
  img1:
    build:
      context: .
      target: img1
  img2:
    build:
      context: .
      target: img2

using your Dockerfile with the following content:

FROM alpine as img1
COPY file1.txt .

FROM alpine as img2
COPY file2.txt .
like image 186
Sebastian Brosch Avatar answered Sep 28 '22 04:09

Sebastian Brosch