Docker: Is it Build-time or Runtime ?

February 18, 2025

3 mins read

TLDR:

Build-time: FROM, LABEL, ARG, COPY, ADD, RUN, ONBUILD

Runtime: EXPOSE, CMD, ENTRYPOINT, VOLUME, HEALTHCHECK

Both: USER, WORKDIR, ENV

This has always been popping up when I'm working with Dockerfile or Docker compose. Some of the commands can be run in build-time, but some can't. Maybe you want to run a specific command only in runtime. Totally depends on your situation 🥂. On my end, this issue showed up in my prisma migration, quite similar to this github issue where I can't run prisma migrate on RUN instruction.

I'll be listing the things that I know currently is under Build-time, and Runtime.

🏗️ Build-time

These will run while the image is building (some came from this reference).

InstructionDescription
FROMSpecifies the base image.
LABELAdds metadata
ARGDefines a build-time variable
COPYCopies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.
ADDCopies new files, directories or remote file URLs from <src> and adds them to the filesystem of the container at the path <dest>.
RUNExecutes any commands in a new layer on top of the current image and commits the results.
ONBUILDExecutes instructions when the image is used as a base image

🏃 Runtime

These will run when the image has been built, and the container will run.

InstructionDescription
EXPOSEDocuments the port the container listens on
CMDProvides 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.
ENTRYPOINTAllows you to configure a container that will run as an executable.
VOLUMECreates a mount point for persistent storage (ensures data is not lost when the container stops).
HEALTHCHECKDefines a command to monitor the container’s health while it is running.

🫶 Both

These are being used for both Build-time, and Runtime.

InstructionDescription
USERSets the user for subsequent commands in buildtime, and sets the user for runtime CMD, and ENTRYPOINT
WORKDIRSets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow in the Dockerfile.
ENVDefines environment variables that are available both at build-time and runtime.

📖 Note

When using Docker compose, env is actually runtime. If you want to use it for build-time, use build arg instead.

That's it! 🔥

© 2025 Alec Blance

Bacolod, PH