Dockerfile— Build-Time vs. Run-Time Instructions

Yair Nevet
2 min readMay 21, 2024
Photo by Ian Taylor on Unsplash

Did you totally catch that difference ❓

In a Dockerfile, there are instructions that are executed at build time and others that are executed at runtime.

Build time instructions are executed when you build a Docker image using the docker build command.

These instructions include:

🎯 FROM: Specifies the base image to start with.

🎯 RUN: Executes any commands in a new layer on top of the current image and commits the results.

🎯 COPY: Copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.

🎯 ADD: Copies new files, directories or remote file URLs from <src> and adds them to the filesystem of the container at the path <dest>.

🎯 WORKDIR: Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow in the Dockerfile.

Runtime instructions are executed when you start a new container from the Docker image using the docker run command.

These instructions include:

🎯 CMD: Provides defaults for an executing container. These can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction.

🎯 ENTRYPOINT: Allows you to configure a container that will run as an executable.

It’s important to note that the CMD and ENTRYPOINT instructions define what command gets executed when running a container. If both are used in the same Dockerfile, CMD will specify parameters that can be passed to an ENTRYPOINT.

In the example below, you’ll notice the distinction between build-time and run-time instructions. The build-time instructions are used to create the final image, while the run-time instructions specify the steps the resulting Docker container will execute.

That was it, I hope you were able to learn something new 🐳

Ensure that you’re following me and keep an eye out for my upcoming posts… See ya 😉

--

--