Skip to main content

Docker Commit or Dockerfile?

· 2 min read
Lucas Sovre
Software architect, Docker certified expert, cloud and devsecops .

Have you heard about the second command that allows you to create Docker images? 🐳

docker commit [container name] [image name]:[tag]

This command creates an image by taking a kind of "snapshot" of the targeted container. However, I wondered what the differences were compared to docker build (aside from the Dockerfile).

I took the time to study a very simple case and examine the differences it produces:

Differences between Docker Commit and Docker Build:

Both docker commit and docker build commands are used to create Docker images. However, docker commit creates an image from an existing container, while docker build creates an image from a Dockerfile.

Comparison Test Protocol for Docker Commit and Docker Build:

  1. Build an nginx:1.24.0 image with the addition of the iputils-ping package.

  2. Make this addition in a single command.

The Dockerfile :

FROM nginx:1.24.0

RUN apt update && apt install iputils-ping -y

In this case, there is a difference of 1254 Bytes, approximately 0.0001 MB.

The exact sizes of the images are:

  • 155 182 582 bytes for docker build
  • 155 183 836 bytes for docker commit

Attribute Differences

While the image sizes have changed very little, the attributes have been radically modified:

The image created with docker build has lost the following attributes:

  • Cmd
  • EntryPoint
  • Env
  • Labels
  • Exposed ports
  • Stop signal
  • Docker version
  • Parent

Conclusion on the Differences between Docker Commit and Docker Build:

There is no better solution; they simply have different purposes:

Docker commit should mainly be used to customize a Docker image to simplify its later use.

Example:

Adding a package, quickly modifying an image for testing purposes...

Docker build should be used when creating a new use case, especially when wanting to keep a scripted version of the image in the form of a Dockerfile. This is much more sustainable than an image stored in a registry.

The sources of my experience and the entire process are available on this repository: https://github.com/LucasSovre/sourcesCommitVsBuild/tree/main

This shouldn't affect the studied mechanics, but the tests were performed with an ARM (M1) processor.

Sources