Create a new image from multiple source images
In this step, we will learn how to create a new Docker image by combining content from multiple source images. This is a common scenario when you need to build an image that includes components from different base images or pre-built images.
First, let's pull the necessary source images. We will use the ubuntu
and alpine
images as examples.
docker pull ubuntu:latest
docker pull alpine:latest
You should see output indicating that the images are being pulled.
latest: Pulling from library/ubuntu
...
Status: Downloaded newer image for ubuntu:latest
latest: Pulling from library/alpine
...
Status: Downloaded newer image for alpine:latest
Now, we will create a new image using a Dockerfile
. A Dockerfile
is a text document that contains all the commands a user could call on the command line to assemble an image. Create a new file named Dockerfile
in your ~/project
directory.
nano ~/project/Dockerfile
Add the following content to the Dockerfile
:
FROM ubuntu:latest AS base
FROM alpine:latest AS alpine_base
COPY --from=alpine_base /etc/alpine-release /app/alpine-release
COPY --from=base /etc/os-release /app/ubuntu-release
WORKDIR /app
CMD ["ls", "-l"]
Let's break down this Dockerfile
:
FROM ubuntu:latest AS base
: This line sets the base image for the first stage of our build to ubuntu:latest
and names this stage base
.
FROM alpine:latest AS alpine_base
: This line starts a new build stage using alpine:latest
as the base image and names this stage alpine_base
. This is a multi-stage build, allowing us to copy files between stages.
COPY --from=alpine_base /etc/alpine-release /app/alpine-release
: This command copies the file /etc/alpine-release
from the alpine_base
stage to the /app/alpine-release
directory in the current stage.
COPY --from=base /etc/os-release /app/ubuntu-release
: This command copies the file /etc/os-release
from the base
stage to the /app/ubuntu-release
directory in the current stage.
WORKDIR /app
: This sets the working directory for subsequent instructions to /app
.
CMD ["ls", "-l"]
: This specifies the default command to run when a container is started from this image. It will list the contents of the /app
directory.
Save the Dockerfile
by pressing Ctrl + X
, then Y
, and Enter
.
Now, build the image using the docker build
command. The .
at the end of the command specifies the build context, which is the current directory (~/project
).
docker build -t my-multi-stage-image:latest ~/project
You should see output indicating the build process, including the different stages being executed.
[+] Building
...
Successfully built <image_id>
Successfully tagged my-multi-stage-image:latest
After the build is complete, you can verify that the image was created by listing your local images.
docker images my-multi-stage-image
You should see your newly created image in the list.
REPOSITORY TAG IMAGE ID CREATED SIZE
my-multi-stage-image latest <image_id> About a minute ago ...MB
Finally, let's run a container from this image to see the copied files.
docker run --rm my-multi-stage-image:latest
The --rm
flag automatically removes the container when it exits. The output should show the files copied from the ubuntu
and alpine
images.
total 8
-rw-r--r-- 1 root root 25 Aug 24 10:00 alpine-release
-rw-r--r-- 1 root root 281 Aug 24 10:00 ubuntu-release
This confirms that we successfully created an image by combining content from multiple source images using a multi-stage build.