Multiple images, one Dockerfile

前端 未结 2 861
Happy的楠姐
Happy的楠姐 2020-12-16 00:28

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

Shouldn\'t this produce two images img1 & img2, ins

相关标签:
2条回答
  • 2020-12-16 01:07

    You can build multiple images from one Docker file by running docker build for each build stage name want to pick out of the Dockerfile (FROM alpine as name).

    By default docker builds the image from the last command in the Dockerfile, however you can target the build to an intermediate layer. Extending from the example Dockerfile in the question, one can:

    docker build -t image1 --target img1 .
    

    ref: https://docs.docker.com/engine/reference/commandline/build/#specifying-target-build-stage---target

    0 讨论(0)
  • 2020-12-16 01:11

    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 .
    
    0 讨论(0)
提交回复
热议问题