Skip to content
IT glossary
DevOps and cloudUpdated December 3, 2026

Dockerfile

A text file that tells Docker how to build an image: what to start from, which files and dependencies to include, and what to run by default when a container starts.

A build recipe, not a running container

A Dockerfile records how to package an application and its runtime into a Docker image. The image is the result of the build; a container is an instance created from that image. Teams usually keep the Dockerfile beside the source code so they can review changes to the runtime and dependencies in the same pull request as changes to the app.

What happens during a build

In docker build -t demo ., -t demo names the image and . selects the current directory as the build context. By default, Docker looks for a file named Dockerfile there. The context determines which local files an ordinary COPY can access. A .dockerignore file can exclude files the build doesn't need, such as .git or a local virtual environment.

FROM starts a build stage from a base image. WORKDIR sets the working directory inside that stage. COPY adds files, and RUN executes a command at build time, such as installing dependencies. CMD is different: it supplies a default command for container startup, or default arguments to ENTRYPOINT if one is set.

Example: packaging a Python script

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app.py .
CMD ["python", "app.py"]

From the directory containing these three files, run docker build -t demo ., then docker run --rm demo. The build installs Python packages into the image. Starting the container runs app.py; it doesn't install the packages again. Copying requirements.txt before app.py lets Docker reuse the dependency installation step when only the script changes. In this example, --no-cache-dir disables pip's own cache, not Docker's build cache.

Where it fits in a delivery pipeline

A CI pipeline can build an image, test it, and deploy that same image rather than rebuilding separately for each environment. When an application needs a compiler, a multi-stage Dockerfile can build the executable in one stage and bring only the runtime files into the final stage with COPY --from. The deployed image then doesn't have to carry the compiler or the source tree.

A Dockerfile doesn't describe how several services run together. That's what a Docker Compose file can do: for example, start an app and a database and connect them to the same network. Also, don't bake credentials into an image with COPY or ENV. Deleting a copied file in a later layer doesn't remove it from the earlier layer. If a build step needs a credential, use a BuildKit secret mount.

Interview questions

You may be asked to review a Dockerfile that uses COPY . . before installing dependencies. Explain which file changes can invalidate the install step's cache and how copying the dependency manifest first helps. Another common question is what happens to CMD when you pass arguments after the image name in docker run: those arguments replace CMD. If the image has an exec-form ENTRYPOINT, they're passed to that executable instead of its default arguments.