docker base image

Docker Base Image | How to decide | Debian or Alpine [2022]

If you have ever worked on docker, you might have created a dockerfile as well. While creating the dockerfile, you might have come across the question of which docker base image to use. This blog will explore:

  • what is an image in the docker
  • what is dockerfile
  • we will understand how to choose a base image
  • How to check the image size and layer size.
  • Extra tips for writing efficient dockerfile

To follow along, I assume you have prior knowledge about docker.

What is an image in docker?

Image is nothing but a read-only template that contains a set of instructions to create a container. Think of a docker image as a recipe to make food. We need a recipe to produce food; similarly, we need an image to create a container. A Docker image includes the elements necessary to run an application as a container — such as code, environment variables, config files, libraries and run time, etc.

What is dockerfile

A dockerfile is a text document that contains a set of instructions to create a docker image. The Docker builds images by following the instructions written in the Dockerfile.

docker base image

Here is the format of the Dockerfile:

# Comment

Lines that begin with a hash, or pound, symbol (#) are comments.

INSTRUCTION represents the keyword in the dockerfile. Instructions are not case-sensitive, but the convention is to make instructions all uppercase to improve visibility.

The first instruction must be a FROM instruction that specifies the base image to be used. Dockerfile instructions are executed into a new container using this base image. The order in which the instructions are executed depends on the order in which it is written in the Dockerfile.

The instruction is not case-sensitive. However, the convention is to be UPPERCASE to distinguish them from arguments more easily.

A simple Dockerfile example

FROM busybox
ENV FOO=/bar
ADD . $FOO       # ADD . /bar
COPY \$FOO /quux # COPY $FOO /quux

A Dockerfile must begin with a FROM instruction, which signifies the base image for the dockerfile.

How to choose a Docker base image

When choosing a docker base image, the first thumb rule is that the size should be smaller. So it could reduce overall image size and other overheads also. Most people choose either alpine or Debian as a base image. It is not the thumb rule, but alpine and Debian images are very lightweight, so it takes significantly less time to boot up a new container built from these base images.

docker base image

Docker base image alpine

The alpine image is very small, and the alpine community pays a lot of attention to minimizing this image. This image only contains the necessary packages. Alpine Linux is built around musl libc and busybox, making it smaller and more resource-efficient. Select an alpine image as a base image if you are not going to install other applications in that container, and you will have a separate container for each application.

Docker base image Debian

The size of the Debian image is large compared to the alpine image as it contains more packages. Debian image has more packages, so more vulnerabilities are discovered in Debian image, and sometimes scanners may report more false positives if you use Debian image.

Debian vs. alpine image size

Let’s pull both Debian and alpine images and check how much space both images consume.

Pull alpine image

➜  ~ docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
540db60ca938: Pull complete
Digest: sha256:69e70a79f2d41ab5d637de98c1e0b055206ba40a8145e7bddb55ccc04e13cf8f
Status: Downloaded newer image for alpine:latest

Pull Debian image

➜  ~ docker pull debian
Using default tag: latest
latest: Pulling from library/debian
bd8f6a7501cc: Pull complete
Digest: sha256:ba4a437377a0c450ac9bb634c3754a17b1f814ce6fa3157c0dc9eef431b29d1f
Status: Downloaded newer image for debian:latest

Check both image sizes

➜  ~ docker images
REPOSITORY                    TAG       IMAGE ID       CREATED        SIZE
alpine                        latest    6dbb9cc54074   3 weeks ago    5.61MB
debian                        latest    0d587dfbc4f4   4 weeks ago    114MB

As you can see Debian image is much larger as compared to the alpine image

Now check the image layer size as well

check the alpine image layer size

➜  ~ docker history alpine
IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT
6dbb9cc54074   3 weeks ago   /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B
<missing>      3 weeks ago   /bin/sh -c #(nop) ADD file:8ec69d882e7f29f06…   5.61MB
➜  ~

check the Debian image layer size

➜  ~ docker history debian
IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT
0d587dfbc4f4   4 weeks ago   /bin/sh -c #(nop)  CMD ["bash"]                 0B
<missing>      4 weeks ago   /bin/sh -c #(nop) ADD file:e18bc3e10e7c743f1…   114MB

history command enables us to check the size of the image layer, which will help us troubleshoot if containers are getting more time to spin up.

What happens if I choose a large base image

If you choose a large image, your container will still work. But your overall image size will increase, and the container creation will take more time as it needs to download more binaries for larger images.

Docker base image based on the operating system

In this session, we will understand how to choose the docker base image based on the operating system.

Docker Linux base image

If you want to choose a Linux-based environment for your image, then you can selectalpine as a base image that is based on the Linux distribution. The alpine-based docker base image has a more complete package repository than the other Busybox-based image.

Docker Ubuntu base image

For Debian-based Linux operation, you can choose Ubuntu as a base image. This image is built using official rootfs tarballs. The ubuntu: latest tag pulls the latest ubuntu image, the dockerhub.The recommendation is always to get the latest version of an image.

Final verdict onDebian vs. alpine

People choose the alpine as a base image in most official images because of its size. I will recommend using alpine whenever possible as a base image. But if you require an image that contains all the necessary libraries, you can always go with a Debian image.

Docker base image example

Almost all the dockerfile starts with a parent image, but if you want to have more control over the content/libraries of the base image, then you can build your custom image.

You can use the docker reserved scratch image as a starting point to build an image. Next, you can add/install the libraries you want to have in your base image. Below is a short example of how to create a base image using scratch

FROM scratch
ADD hello /
CMD ["/hello"]

More details about the custom docker base image can be found here


I hope you like this tutorial on the docker base image. In this blog, we have learned about the docker base image and the different scenarios/criteria to choose a docker base image, and finally, we have understood how to create a custom base image.

Please do let me know in the comment box if you face any issues while following along.

More to read

Docker layer

Docker vs Podman

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top