Skip to content
  • Official
  • Beginner
  • English
  • 11 steps · 2 quizzes

Docker Workshop for Beginners: Images, Containers and Volumes

Docker workshop for beginners: run, stop and remove containers, publish ports, keep data in volumes and build images in a practice terminal in your browser.

  • Free
  • No account needed
  • Stop any time, pick up where you left off
Workshopy.io

WorkshopyOfficial

78 lessons · used by 2 teachers

For teachers

Teaching this? Run it live with your class.

  • Students join with a code — no accounts
  • See who’s done and who’s stuck, step by step
  • It’s your copy — change anything before class

Sign in to copy it to your workspace. It’s free.

Your path

11 steps · 2 quizzes · open any step, or read it here

  1. Docker workshop for beginners: what you'll doStart hereIn this Docker workshop for beginners you learn the difference between images and containers, then run, stop and remove containers, publish ports and keep data in volumes.InteractiveHands-on
    Read this step

    In this Docker workshop for beginners you learn the difference between images and containers, then run, stop and remove containers, publish ports and keep data in volumes. You finish by building your own image from a Dockerfile, layer by layer, typing every command in a practice Docker terminal in your browser.

    Watch the whole idea in five scenes:

    Interactive · Images, containers, ports and volumes in five scenes — try it in the lesson

    In this lesson you'll:

    • see why an image is a read-only template and a container is a running copy of it
    • run containers in the background and take them through their life: docker run, ps, stop, start, rm
    • reach a container from your computer with -p 8080:80, and look inside it with docker logs and docker exec
    • keep data in a volume, and build your own image with a Dockerfile and docker build

    It takes about 75 minutes. Each hands-on step has a practice terminal with a short list of tasks that tick themselves when Docker's state looks right. The terminal is a simulation that answers like Docker does: nothing is downloaded and nothing you type touches a real computer. On a computer with Docker installed, the same commands work the same way (outputs differ in ids, sizes and times).

    Takeaway: an image is the template, a container is a running copy, and ports and volumes connect it to the world.

  2. Images and containers: the template and its running copiesTwo words carry the whole of Docker: An image is a read-only template: the files an app needs (a small operating system, the app, its settings) stored as layers, one on top of the other, plus how to start it.InteractiveHands-on
    Read this step

    Two words carry the whole of Docker:

    • An image is a read-only template: the files an app needs (a small operating system, the app, its settings) stored as layers, one on top of the other, plus how to start it. You download images (docker pull nginx) or build your own.
    • A container is a running copy of an image: an isolated process with its own files, network and name. You can start many containers from one image, as a team runs the same web server image for its support portal, its status page and its staging site.

    Containers don't copy the image. Each one uses the image's layers as they are and adds a thin writable layer of its own on top. When a container changes a file, the change goes into its writable layer; the image and every other container stay as they were.

    A container is not a virtual machine: it has no operating system of its own to boot. It is a process on the host's kernel, kept apart from the others, which is why it starts in about a second.

    Try it: start two containers from the image, change the page in one of them, then remove it.

    Interactive · One image, many containers, each with its own writable layer — try it in the lesson

    After you removed a container that you had changed: did the image change? What does a new container serve?

    Takeaway: an image never changes; each container writes to its own thin layer on top of it.

  3. docker pull, docker images and docker run: your first containersThree commands get you startedInteractiveHands-on
    Read this step

    Three commands get you started:

    Command What it does
    docker pull nginx downloads the image nginx from Docker Hub, the public registry
    docker images lists the images on this computer, with their size
    docker run hello-world creates a container from the image and starts it; if the image isn't here yet, run pulls it first

    An image's full name is name:tag. Without a tag Docker means :latest, so nginx is nginx:latest. Pin a tag in real projects (nginx:1.29) so the next pull doesn't change your app.

    A container lives as long as its main process. hello-world prints a message and ends, so its container stops at once. It isn't deleted: docker ps lists only running containers, and docker ps -a (all) lists the stopped ones too.

    A command after the image name replaces the image's own start command: docker run alpine echo Hello starts the tiny Alpine Linux image, runs echo Hello in it, and stops.

    Try it: do the five tasks. Watch the panel beside the terminal: each image and container appears there as you make it.

    Interactive · docker pull, images and run in a practice terminal — try it in the lesson

    In docker ps -a, what is the STATUS of the hello-world container, and what does the number in brackets mean?

    Takeaway: docker run creates and starts a container, pulling the image first if needed; docker ps -a shows the stopped ones too.

  4. A container's life: run -d, ps, stop, start and rmA web server should keep running, and you want your terminal back. Two options of docker run do that: -d (detach) runs the container in the background and prints its id. --name web gives it a name, so you can type web instead of the id.InteractiveHands-on
    Read this step

    A web server should keep running, and you want your terminal back. Two options of docker run do that:

    • -d (detach) runs the container in the background and prints its id.
    • --name web gives it a name, so you can type web instead of the id. Without it Docker makes one up, like eager_turing.

    Then a container goes through a few states:

    Command What happens STATUS in docker ps -a
    docker run -d --name web nginx created and started Up 3 seconds
    docker stop web asks the main process to stop; after 10 seconds Docker forces it Exited (0) 2 seconds ago
    docker start web starts the same container again, with its files as they were Up 1 second
    docker rm web deletes a stopped container gone from the list
    docker rm -f web stops and deletes a running one in one go gone from the list

    docker rm refuses a running container, so you can't delete one by accident. And two containers can't have the same name: remove the old web before you run a new one called web.

    Try it: take web through its whole life. nginx is already downloaded in this terminal.

    Interactive · run -d, ps, stop, start and rm in a practice terminal — try it in the lesson

    Check your work · After `docker stop web`, run `docker ps -a` and paste the line for `web`. — check it in the lesson

    Takeaway: run -d starts a container in the background; stop and start pause and resume it; rm deletes it.

  5. Ports: -p host-port:container-portA container has its own network. nginx listens on port 80 inside its container, and nothing on your computer can reach it until you publish that portInteractiveHands-on
    Read this step

    A container has its own network. nginx listens on port 80 inside its container, and nothing on your computer can reach it until you publish that port:

    docker run -d --name web -p 8080:80 nginx
    

    -p 8080:80 means: connections to port 8080 on your computer (the host) go to port 80 in the container. The order is always host first, container second. Then curl localhost:8080 (or a browser at http://localhost:8080) gets nginx's page.

    • Each host port can be used by only one container at a time. Two web servers are -p 8080:80 and -p 8081:80: the same port 80 inside, different ports outside.
    • The second number must be the port the app listens on. -p 8080:8080 sends the request to port 8080 in the container, where nobody is listening.
    • EXPOSE 80 in an image's Dockerfile only documents the port: docker ps shows 80/tcp without an arrow. Only -p publishes it.

    Try it: pick the two numbers of -p and the address the browser opens, then send a request.

    Interactive · Where does a request to localhost go? — try it in the lesson

    Now publish ports for real. Watch the PORTS column of docker ps and the arrows in the panel.

    Interactive · Publishing ports in a practice terminal — try it in the lesson

    The failed web3 was still created. Run docker ps -a: what is its STATUS?

    Check your work · Run `curl localhost:8080` and paste the line with the page's main heading (the `<h1>` line). — check it in the lesson

    Takeaway: -p host:container opens a door from your computer into the container; each host port fits one container.

  6. Check: images, containers and portsFour questions on the first half.Quiz · 4 questions
    Read the questions

    Four questions on the first half.

    Question 1. You run docker run -d --name web nginx three times in a row. What happens?

    • The first starts web; the next two fail because the name web is already in use
    • Three containers named web start
    • The image nginx is downloaded three times
    • Each command replaces the container from the one before

    Question 2. Which command shows containers that have stopped?

    • docker ps -a
    • docker ps
    • docker images
    • docker logs

    Question 3. In docker run -p 3000:80 nginx, what is 3000?

    • The port on your computer that leads to port 80 in the container
    • The port nginx listens on inside the container
    • The number of connections nginx accepts
    • The container's id

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

    • A container is a running copy of an image, with its own writable layer on top
    • An image is a running copy of a container
    • They are two names for the same thing
    • A container is an image after it has been downloaded
  7. docker logs and docker exec: looking inside a running containerA container in the background prints nothing on your screen. Two commands let you see what it is doing: docker logs web prints everything the container's main process wrote to its output.InteractiveHands-on
    Read this step

    A container in the background prints nothing on your screen. Two commands let you see what it is doing:

    • docker logs web prints everything the container's main process wrote to its output. The nginx image sends its access log there, so every request shows up as one line: who asked, when, for which page, and the answer (200).
    • docker exec web ls /usr/share/nginx/html runs one more command inside the running container and shows its output. The container's main process (nginx) keeps running.
    • docker exec -it web bash opens a shell inside the container: -i keeps it reading your keyboard, -t gives it a terminal. The prompt changes to root@<container id>:/#. Look around with ls, cd and cat, then type exit to come back. Small images have no bash; use sh there.

    docker logs -f web keeps following the log as new lines arrive, until you press Ctrl+C, which is what you do while you test a page in the browser.

    Try it: a container web is already running on port 8080 in this terminal. Investigate it.

    Interactive · docker logs and docker exec in a practice terminal — try it in the lesson

    When you typed exit, did web stop? Check with docker ps. Why not?

    Takeaway: docker logs shows what the container printed; docker exec runs a command inside it, and -it gives you a shell.

  8. The writable layer: what docker rm throws awayEverything a container writes goes into its writable layer: a changed page, a log file, an upload, a database's data. That layer belongs to that container: It survives docker stop and docker start: the container is the same, so are its files.InteractiveHands-on
    Read this step

    Everything a container writes goes into its writable layer: a changed page, a log file, an upload, a database's data. That layer belongs to that container:

    • It survives docker stop and docker start: the container is the same, so are its files.
    • It is deleted with the container by docker rm. A new container from the same image starts from the image's files again.

    This is a feature: you can throw a container away and get a clean one in a second, and every server started from one image is the same. It also means a container is the wrong place to keep anything you need. The next step fixes that.

    Try it: change the support portal's page inside web, then see what survives a stop and what survives a remove.

    Interactive · The writable layer in a practice terminal — try it in the lesson

    The panel counts each container's changes in its writable layer. What did it say for the new web?

    Takeaway: a container's changes survive stop and start, and are gone with docker rm.

  9. Volumes: data that outlives the containerA volume is storage that Docker keeps outside any container. You mount it at a folder inside a container; whatever the container writes there goes into the volume, not into its writable layer.InteractiveHands-on
    Read this step

    A volume is storage that Docker keeps outside any container. You mount it at a folder inside a container; whatever the container writes there goes into the volume, not into its writable layer.

    docker volume create site
    docker run -d --name web -p 8080:80 -v site:/usr/share/nginx/html nginx
    

    -v site:/usr/share/nginx/html means: the volume site appears at /usr/share/nginx/html inside the container. The order is volume first, path in the container second, like -p.

    • Remove the container and the volume stays, with its files. Mount it in a new container (a newer nginx, say) and the data is there.
    • When a new, empty volume is mounted at a folder that has files in the image, Docker copies those files into the volume first, so nginx still finds its pages.
    • docker run -v site:/path creates the volume if it doesn't exist. docker volume ls lists them, docker volume rm site deletes one (Docker refuses while a container uses it).
    • To share a folder of your own computer instead, use a bind mount: -v "$(pwd)/site":/usr/share/nginx/html. Editing the files on your computer changes the site at once. This terminal has named volumes only.

    Databases are the classic case: the official PostgreSQL image keeps its data under /var/lib/postgresql, and a volume there keeps your data through every upgrade of the database container.

    Try it: put the page in a volume, throw the container away, and get the page back in a new one.

    Interactive · Volumes in a practice terminal — try it in the lesson

    Try docker volume rm site while web2 uses it. What does Docker say, and why is that a good thing?

    Takeaway: a volume lives outside containers; -v name:/path mounts it, and it survives docker rm.

  10. A Dockerfile, layer by layer: how the build cache worksTo package your own app, you write a Dockerfile: a list of instructions that docker build runs from top to bottom.InteractiveHands-on
    Read this step

    To package your own app, you write a Dockerfile: a list of instructions that docker build runs from top to bottom.

    FROM node:22-alpine
    WORKDIR /app
    COPY package.json package-lock.json ./
    RUN npm ci
    COPY . .
    CMD ["node", "server.js"]
    
    Instruction What it does
    FROM node:22-alpine starts from an existing image: here Node.js 22 on Alpine Linux
    WORKDIR /app the folder the next instructions work in (created if missing)
    COPY package.json package-lock.json ./ copies files from your project into the image
    RUN npm ci runs a command while building: here, installs the packages
    CMD ["node", "server.js"] the command a container runs when it starts

    Each instruction that changes files (RUN, COPY, ADD, and WORKDIR) adds a layer. CMD, EXPOSE and ENV only record settings.

    Docker keeps every layer it builds in a build cache. On the next build it reuses a step's layer, and prints CACHED, when the instruction is the same, the files it copies are unchanged, and every step before it came from the cache. As soon as one step must run again, every step after it runs again too, because each layer sits on the one below.

    So the order matters. Put what changes rarely (the package list) before what changes all the time (your code), and an edit to your code doesn't reinstall every package.

    Try it: build once, edit server.js and build again, in order A and then in order B. The times are illustrative; the pattern is what a real build does.

    Interactive · Which layers come from the cache? — try it in the lesson

    In order B, which steps ran again after you added a package to package.json? Why does npm ci have to run then?

    Takeaway: a change rebuilds its own layer and every layer after it, so put what changes most at the bottom of the Dockerfile.

Comments

You’ll sign in with Google before it’s posted.Comments are public — don’t name your students.
0 / 1,500

No comments yet. Be the first to say how it went.

Using it

Workshopy shares this lesson under CC BY 4.0. Anyone may use and change it, with credit to you. Anyone can learn it here, free. A copy in your workspace is yours to change and run live; your students never need an account.

Versions

  1. 1

    Version 1Current

    · First published

Copies are made from the current version. A copy you already have doesn’t change.

More from Workshopy

All 78 lessons
All 13 Programming lessons