Module 3 · Project 03 · Dockerize a Real Application · Lesson 11 of 48

Images, containers and your first Dockerfile

What a container is, how it differs from a virtual machine, and how a Dockerfile turns an app into an image.

In this lesson · about 25 minutes

  • Explain the difference between an image and a container
  • Compare containers with virtual machines
  • Write a first Dockerfile and run it with port mapping

"It works on my machine"

An app needs more than its code: a particular language version, libraries, settings. Install it on a new server and something is always slightly different. A container packages the app together with everything it needs to run, so it behaves the same on your laptop, in CI and in production.

Containers vs virtual machines

A virtual machine pretends to be a whole computer, with its own full operating system. That's heavy: gigabytes, and a minute to boot. A container shares the host's Linux kernel and only isolates the app's processes, files and network. It's small, and starts in about a second. Linux features called namespaces (what a process can see) and cgroups (how much CPU and memory it may use) do the isolating.

Image vs container

  • An image is a read-only template: the app, its dependencies and a start command, frozen. Think of it as a recipe, or a class in programming.
  • A container is a running instance of an image, with a thin writable layer on top. You can start many containers from one image, like objects from a class.

A first Dockerfile

A Dockerfile is the recipe that builds an image. For a small Node.js notes API:

FROM node:22-alpine          # start from an image with Node installed
WORKDIR /app                 # cd into /app (created if missing)
COPY package*.json ./        # copy the dependency list first…
RUN npm ci --omit=dev        # …and install exact versions
COPY . .                     # then copy the rest of the code
ENV PORT=3000
EXPOSE 3000                  # documents the port the app listens on
CMD ["node", "server.js"]    # what runs when a container starts

Build it, giving the image a name and tag, then run a container from it:

docker build -t notes-app:1.0 .
docker run -d --name notes-1 -p 8080:3000 notes-app:1.0
curl localhost:8080/health

-p 8080:3000 is port mapping: traffic to port 8080 on your machine goes to port 3000 inside the container. -d runs it in the background. The same app in any language follows this exact workflow; only the FROM image and commands change.

Add a .dockerignore file (like .gitignore) listing node_modules, .git and .env, so they never get copied into the image. That keeps it small and keeps secrets out.

Try it · watch it run

Images and containers playground

Start a few containers from one image. Save a note in one, then remove it. Now switch on the volume and try again.

Image (read-only)

notes-app:1.0

node:22-alpine
npm ci
app code

One recipe, any number of containers

No containers yet. Press docker run.

terminal
$ docker image ls
REPOSITORY   TAG    IMAGE ID       SIZE
notes-app    1.0    5f2c9e1a7b3d   182MB
An image is a read-only template; a container is one running copy of it with its own thin writable layer. Remove the container and that layer goes with it, so keep data you care about in a volume.

Whiteboard · watch and try

VM vs container

Press Play to compare the two stacks.

5 steps
Press Play and the tutor will talk you through it while writing on the board. Pause any time to write on the board yourself.

Tip: press Pause, then use the pen to work along on the board. Space plays and pauses; Ctrl+Z undoes your last mark.

Hands-on lab (simulated terminal)

Lab: build and run your first image

A folder with a small Node.js notes API and the Dockerfile above.

Step 1 of 5. Type each command and press Enter. Nothing runs on your computer.

Lab: build and run your first image

1. Build an image tagged notes-app:1.0 from the current folder.

you@laptop:~/notes-app$

Check your understanding0 of 3 correct

Question 1

1.What is the relationship between an image and a container?

Question 2

2.What does -p 8080:3000 do?

Question 3

3.Why are containers lighter than virtual machines?

Key terms

Say the meaning to yourself, then tap a card to check.

Quiz score: 0 of 3

Tip: answer every quiz question correctly first.

Written by Noviq Academy in its own words. The labs are simulations: no real servers are touched. When you build the projects yourself, use a practice virtual machine or a free-tier cloud account you control, watch your cloud spending, and never run cleanup or delete commands on a machine you care about. Tool names belong to their owners; versions and output are shown for learning and may differ slightly from yours. Read-aloud and the whiteboard voice use your device's built-in voices.