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