Skip to content
Intermediate ContainersLesson 1 of 5 2 min read

Docker Deep Dive

Images versus containers, layers, and what actually happens when you run docker run.

On this page4 sections

An image is a read-only template - application code, a runtime, and every dependency, frozen in place. A container is a running instance of that image. The relationship is the same as a class and an object: one image, as many running containers from it as you like.

Layers

A Docker image is built from stacked, cacheable layers - each instruction in a Dockerfile produces one:

dockerfile
FROM node:20-alpine        # base layer
COPY package.json .        # a layer
RUN npm install            # a layer
COPY . .                   # a layer
CMD ["node", "server.js"]  # metadata, not a layer

If only your application code changes, Docker reuses the cached npm install layer from before instead of redoing it - which is exactly why COPY package.json and npm install are deliberately placed before copying the rest of the source: dependencies change far less often than application code, so that expensive layer gets reused far more often too.

Reading a Dockerfile

  • FROM - the base image to build on top of.
  • RUN - executes a command while building the image (installing packages, compiling).
  • COPY - copies files from your machine into the image.
  • CMD - the command that runs when a container starts from this image.

What docker run actually does

bash
docker run -d --name web nginx

Pulls the nginx image if it isn't already local, creates a new container from it, names it web, and (-d) runs it detached - in the background, handing you your prompt straight back instead of attaching to its output.

Try it

Run these in the practice terminal (opens in a new tab). It opens in a new tab, so you keep your place here.
bash
docker run -d --name web nginx

When you're ready, hit Mark complete and move to the next lesson.

Challenge

Start an nginx container in the background, named web.

Want to experiment first? Try things in the practice terminal (opens in a new tab), then enter the command that solves the task here.

Quiz

Lock in the concepts. Score at least 80% to mark the lesson complete.

0 of 2 answered

Done with this one?

Skipped the challenge? You can mark the lesson complete by hand.

Your notes

Kept on this browser only.