Skip to main content

Command Palette

Search for a command to run...

Docker - Best practices (Part 1)

Updated
•2 min read•View as Markdown
Docker - Best practices (Part 1)

Use existing image that needed software is already installed

Description

By using images that have the required software already installed, you can save a lot of time.

Real-world case

You have created Spring Boot Java application that uses Gradle build tool.

You don't need to use Debian / Ubuntu (or any other system) as base image then install Java and Maven and only then build and package your code.

Instead of you can simply use Gradle build tool's Docker image as base image.

When building image of your application, Docker will simply pull this image from repository and you can use already installed tool in your Dockerfile.

Use lightweight Linux distros as base image when its possible

Description

When it's possible you can use lightweight distros like Alpine Linux, Fedora CoreOS as your base image (or use application images based on these distros) to make size of the image much smaller. By making image size smaller you will win more time because of download time of base image.

By making image size smaller Docker uses less time to download base image and upload image of the project to the registry.

Real-world case

I created Java Spring Boot application that uses Gradle as a build tool. In this case I tried next combinations:

  • Debian-based Gradle image with my app- ~300 MB

  • Alpine Linux-based Gradle image with my app - ~155MB

As you can see my app's image with Debian-based Gradle took 2 times more disk space than Alpine Linux-based Gradle image

When I push application image I got something like this:

  • Gradle installed Debian image - ~10.6 seconds

  • Gradle installed Alpine Linux image - ~8.1 seconds

As you can see Debian-based image took 2.5 seconds more time to push than Alpine Linux based image