Estimate project
← All posts

7 fast ways to make your Dockerfile better

Here are 7 fast steps that make Docker images more efficient and effective. See how it's done.

5 min read
DevOpsDockerDockerfilescontainersCloud

These are strange times. Docker has existed for nearly 10 years, and asking someone whether they use it might almost come across as an insult. At the same time, many organizations still don’t use it, for a variety of reasons – technical constraints, security concerns or missing know-how, for instance. The most likely situation, though, is that Docker images are in use, just in a very inefficient way. What do I mean by that? Below are a few examples, along with tips on how to fix them.

1. Reinventing the wheel

Quite often, particularly when moving away from non-Docker environments, people take a bare operating system image as the base without checking whether an official, ready-made Docker image already exists – one that would shorten build time and shift the maintenance burden outside our organization. For instance, rather than starting from the debian:bullseye image and installing NodeJS during the build, think about using node:bullseye:

# don’t do this
FROM debian:bullseye
RUN apt update && apt install -y node
COPY . .
RUN node install && echo “success!”

# do this
FROM node:bullseye
COPY . .
RUN node install && echo “great success!”

2. Not using “slim”/Alpine base images

In most cases, a complete OS image with every utility and program isn’t needed. We might only want a handful of them, which is why slim images can be the better choice. Even more can be gained by switching to the Alpine base image, which is ideal for apps that run as a single binary. Alpine’s APK repositories are smaller than those of Ubuntu or even Debian, so before installing something, it’s worth checking the repos. If it’s not there, you’ll have to bring the dependency in by hand.

3. Creating unnecessary layers

I’ve come across Dockerfiles with dozens of RUN statements and several COPY statements. This is a sneaky problem if you don’t understand how Docker images work. Each step that adds, deletes or changes something in the image file system produces a new, incremental layer. That increases the overall image size, but more importantly – builds take longer than they need to. During development in particular, nobody wants to wait for a build any longer than necessary. The fix is easy – combine these instructions. Rather than several RUN statements, write a single one, joining the commands with &&:

# don’t do this:
FROM node:slim
RUN apt update
RUN apt install -y postgresql
RUN apt install -y redis
(...)

# do this instead:
FROM node:slim
RUN apt update && apt install -y postgresql redis
(...)

The same approach works for COPY instructions:

# instead of this:
(...)
COPY src/* .
COPY assets/* .
COPY other/* .
(...)

# do this:
(...)
COPY src/* assets/* other/* .
(...)

TL;DR - keep in mind that each COPY, RUN and ADD instruction produces a new image layer, making the image bigger overall.

4. Copying all files from the local machine to the image

In the earlier example, we used:

COPY . .

It’s a convenient way to put all the source code from the current directory into the image, but it can also drag along unneeded files that do nothing except make the image bigger. A neat solution is to add a .dockerignore file. Anyone who knows the .gitignore file format will feel at home here. It’s really straightforward:

# Comments starting with ‘#’ are ignored

# Add a path of the file you don’t want to use
.gitignore

# Exclude a whole directory
temp/*

# You can also use wildcards
*.md

# Exclude files if you need any that get caught by the wildcard
!IMPORTANT.md

5. Keeping package manager cache inside the image

This is easy to overlook, yet I consider it among the most important points. Whenever we install something with a package manager such as apt, apk and so on, we have to remember to clear its cache – otherwise we’re left with files that will never be used. There are a few ways to handle this, depending on the base image:

# On Alpine-based images it’s very simple - just add –no-cache parameter:
FROM alpine
RUN apk add –no-cache curl
(...)

# On Debian-based images remove files from /var/lib/apt/lists directory:
FROM debian:stable-slim
RUN apt update && apt install -y curl && rm -rf /var/lib/apt/lists/*
(...)

6. Using a single image for building and deployment

At the start of development, it’s fine to have a single image that we build and deploy straight to a dev environment (frequently the developer’s own machine). As time goes on, though, we pile on libraries, tools and other dependencies that can bloat the image. The way to handle this is a technique known as multi-stage building. It lets us build artifacts in one container and then transfer them to a separate, clean one. The source code stays behind, and only what the app needs to run gets added:

# Firstly, let’s create an image used for compiling and give it an alias
FROM golang:1.19-alpine AS builder
WORKDIR /build
COPY go.mod go.sum .
RUN go mod download
COPY *.go .
RUN go build -o go-app

# Now we will use a new container and add only one new layer
FROM alpine:latest
COPY --from=builder /build/go-app /bin/go-app
ENTRYPOINT [“/bin/go-app“]

The outcome is a small, lightweight container holding nothing but what we wanted - our Go binary.

test ➤ docker image ls gotest:gotest
REPOSITORY   TAG       IMAGE ID       CREATED              SIZE
gotest       gotest    b93962d2f028   About a minute ago   8.87MB

7. Ignoring caching

This point gets neglected very frequently. Thanks to its layered structure, a Docker image can cache layers that haven’t changed since the previous build. If you keep modifying the application you’re working on, you don’t want to sit through downloading all your packages each time. That’s why the instructions that change least often should always come first in your Dockerfile, while those tied to your app belong at the bottom.

# avoid doing this
FROM node:slim
COPY . .
RUN yarn install
RUN apt update && apt install -y curl && rm -rf /var/lib/apt/lists/*

# do this instead to allow caching of curl installation
FROM node:slim
RUN apt update && apt install -y curl && rm -rf /var/lib/apt/lists/*
COPY . .
RUN yarn install

Ending thoughts

Following these tips will give you a solid Dockerfile that performs at least reasonably well. There are further ways to improve it, frequently depending on particular languages and/or tools. I’m certain you have more ideas worth adding to this list – if so, please tell me.