teluzia.Cloud Engineer · Day 100
CHAPTER 15 · DOCKER AND CONTAINERS

Dockerfiles and image layers

4–5 hours including independent practice3 topics
LET’S MAKE THIS SIMPLE

What you’re learning today

A Dockerfile is a reproducible assembly recipe; each layer captures part of the filesystem produced by its instructions. The build context supplies files accessible to COPY and related build operations. FROM selects a base, WORKDIR sets a working directory, COPY adds files, and RUN performs build-time work. CMD defines default runtime behavior. These instructions operate at different phases and should not be confused. Docker can reuse build results when relevant inputs match. Put infrequently changing dependency installation before frequently changing application code when appropriate. A broad early COPY can invalidate later work repeatedly. Caching improves speed but should not hide stale or unverified dependencies.

Think of it this way

A Dockerfile is a reproducible assembly recipe; each layer captures part of the filesystem produced by its instructions.

TOPIC 01 / 03Step by step

Build context and Dockerfile instructions

The build context supplies files accessible to COPY and related build operations. FROM selects a base, WORKDIR sets a working directory, COPY adds files, and RUN performs build-time work. CMD defines default runtime behavior. These instructions operate at different phases and should not be confused. For example, cOPY app.py /app/app.py occurs during build; CMD ["python","/app/app.py"] is the default process at container start.

WORKED EXAMPLE
COPY app.py /app/app.py occurs during build; CMD ["python","/app/app.py"] is the default process at container start.

A build starts with the chosen context directory. FROM selects the initial image, WORKDIR selects the working location for subsequent instructions, COPY transfers eligible context files, and RUN executes build-time commands. When a container later starts, CMD supplies its default runtime command unless launch settings override it. Build success and startup success are separate stages.

The build context is the set of files available to relevant build operations. An instruction defines one operation. Runtime begins when a container starts from the resulting image. A file outside the context is not available merely because it exists elsewhere on the developer's laptop. Moving a command between RUN and CMD changes when and how often it executes. Keep the main runtime process in the foreground so its exit reflects container lifecycle. A server started only during image building is not a running server in the final container.

Why this matters

COPY app.py /app/app.py occurs during build; CMD ["python","/app/app.py"] is the default process at container start.

A common trap

Do not overlook this operational distinction: These instructions operate at different phases and should not be confused.

Read all topics in this lesson

A complete reference to revisit alongside the guided steps above.

Build context and Dockerfile instructions

The build context supplies files accessible to COPY and related build operations. FROM selects a base, WORKDIR sets a working directory, COPY adds files, and RUN performs build-time work. CMD defines default runtime behavior. These instructions operate at different phases and should not be confused. For example, cOPY app.py /app/app.py occurs during build; CMD ["python","/app/app.py"] is the default process at container start.

WORKED EXAMPLE
COPY app.py /app/app.py occurs during build; CMD ["python","/app/app.py"] is the default process at container start.

A build starts with the chosen context directory. FROM selects the initial image, WORKDIR selects the working location for subsequent instructions, COPY transfers eligible context files, and RUN executes build-time commands. When a container later starts, CMD supplies its default runtime command unless launch settings override it. Build success and startup success are separate stages.

The build context is the set of files available to relevant build operations. An instruction defines one operation. Runtime begins when a container starts from the resulting image. A file outside the context is not available merely because it exists elsewhere on the developer's laptop. Moving a command between RUN and CMD changes when and how often it executes. Keep the main runtime process in the foreground so its exit reflects container lifecycle. A server started only during image building is not a running server in the final container.

Why this matters

COPY app.py /app/app.py occurs during build; CMD ["python","/app/app.py"] is the default process at container start.

A common trap

Do not overlook this operational distinction: These instructions operate at different phases and should not be confused.

Layer caching and ordering

Docker can reuse build results when relevant inputs match. Put infrequently changing dependency installation before frequently changing application code when appropriate. A broad early COPY can invalidate later work repeatedly. Caching improves speed but should not hide stale or unverified dependencies. For example, copy requirements.txt, install dependencies, then copy source so source edits need not repeat dependency installation.

WORKED EXAMPLE
Copy requirements.txt, install dependencies, then copy source so source edits need not repeat dependency installation.

Build an application by copying only its dependency manifest, installing dependencies, then copying source. On a later source-only edit, Docker can reuse the earlier dependency steps if their relevant inputs still match. If the entire source tree was copied before installation, a small source edit can invalidate that earlier step and force dependency work again.

A layer records filesystem changes from applicable build operations. A cache key incorporates relevant instruction and input information. Cache invalidation occurs when those inputs no longer match a reusable result. RUN results may be reused rather than reexecuted, so a cached package-install command can preserve earlier dependency versions unless inputs or cache policy cause a fresh build. Balance speed with deliberate updates. Removing large files in a later layer does not necessarily remove their bytes from earlier layers. Combine related creation and cleanup appropriately, and measure real build behavior rather than assume fewer Dockerfile lines always produce a smaller image.

Why this matters

Copy requirements.txt, install dependencies, then copy source so source edits need not repeat dependency installation.

A common trap

Do not overlook this operational distinction: Caching improves speed but should not hide stale or unverified dependencies.

Context hygiene and secret handling

A .dockerignore excludes unwanted context files and improves safety and efficiency. Secrets copied into a layer can remain recoverable even if removed in a later layer. Use appropriate build secret mechanisms, avoid credentials in image metadata, and inspect artifacts before distribution. For example, exclude .git, .env, and local state; never COPY a cloud credential file then assume a later rm erases it from image history.

WORKED EXAMPLE
Exclude .git, .env, and local state; never COPY a cloud credential file then assume a later rm erases it from image history.

A developer copies the whole project into an image, including .env. A later RUN deletes .env, and the application appears clean when inspected at runtime. However, an earlier layer may still contain the secret bytes and the build context already sent them to the builder. Deletion is not reliable secret erasure across image history.

A .dockerignore file excludes selected context paths. A build secret mechanism provides sensitive input without ordinary permanent COPY behavior when used correctly. Image metadata includes configuration and history that can reveal unsafe values. Exclude credentials, state, private keys, and unnecessary repositories before building. Build arguments are not a general secure secret store. Use fake values in learning examples and inspect what is actually distributed. Context minimization also improves transfer and cache behavior, but the security reason is stronger: a file never sent or copied cannot be recovered from a layer that should not have contained it.

Why this matters

Exclude .git, .env, and local state; never COPY a cloud credential file then assume a later rm erases it from image history.

A common trap

Do not overlook this operational distinction: Use appropriate build secret mechanisms, avoid credentials in image metadata, and inspect artifacts before distribution.

KEEP THESE WITH YOU

Today’s takeaways

Build context and Dockerfile instructions: The build context supplies files accessible to COPY and related build operations. FROM selects a base, WORKDIR sets a working directory, COPY adds files, and RUN performs build-time work. CMD defines default runtime behavior. These instructions operate at different phases and should not be confused.

Layer caching and ordering: Docker can reuse build results when relevant inputs match. Put infrequently changing dependency installation before frequently changing application code when appropriate. A broad early COPY can invalidate later work repeatedly. Caching improves speed but should not hide stale or unverified dependencies.

Context hygiene and secret handling: A .dockerignore excludes unwanted context files and improves safety and efficiency. Secrets copied into a layer can remain recoverable even if removed in a later layer. Use appropriate build secret mechanisms, avoid credentials in image metadata, and inspect artifacts before distribution.

CLOSE THE LOOP

Ready to complete this day?

Verify the practical steps and pass the knowledge check (at least two thirds correct).

Lab steps verifiedKnowledge check passed