Intro to Git — Snapshots, Branches & Sharing
WorkshopyOfficial10 stepsQuiz9 interactiveDiagram
gitversion controlbeginners
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.

WorkshopyOfficial
78 lessons · used by 2 teachers
Sign in to copy it to your workspace. It’s free.
11 steps · 2 quizzes · open any step, or read it here
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:
docker run, ps, stop, start, rm-p 8080:80, and look inside it with docker logs and docker execDockerfile and docker buildIt 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.
Two words carry the whole of Docker:
docker pull nginx) or build your own.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.
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.
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.
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.
-p 8080:80 and -p 8081:80: the same port 80 inside, different ports outside.-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.
Four questions on the first half.
Question 1. You run docker run -d --name web nginx three times in a row. What happens?
Question 2. Which command shows containers that have stopped?
Question 3. In docker run -p 3000:80 nginx, what is 3000?
Question 4. What is the relationship between an image and a container?
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.
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:
docker stop and docker start: the container is the same, so are its files.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.
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.
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).-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.
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.
The support team keeps a one-page status site in a folder, team-site, with two files:
FROM nginx:alpine
WORKDIR /usr/share/nginx/html
COPY index.html .
EXPOSE 80
and index.html. Build it into an image and run it:
docker build -t team-site .
docker run -d --name status -p 8081:80 team-site
-t team-site names (tags) the image; without a tag after a colon it is team-site:latest.. at the end is the build context: the folder whose files COPY can use. Here, the folder you are in.CMD of its own: it inherits the one from nginx:alpine, which starts nginx.When you change index.html and build again, WORKDIR comes from the cache and only COPY index.html . runs. The new image takes the name team-site; the old one keeps its id and shows as <none> in docker images. A running container keeps the image it started from, so to serve the new page you replace the container.
When an app needs several containers, such as a web app, its database and a cache, a compose.yaml file lists them with their ports and volumes, and docker compose up -d runs them all with one command. Everything in it is what you practised here.
Try it: build, run, change and rebuild the status page. You are in the team-site folder.
Interactive · docker build in a practice terminal — try it in the lesson
After the second build, run docker images. Which line is the old version of your image?
Takeaway: docker build -t name . turns a Dockerfile into an image; rebuild after a change, and replace the container to run the new version.
Type each command or answer, then check. A hint costs nothing, and a wrong answer costs nothing either.
FROM node:22-alpine
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
Six questions on the whole lesson.
Question 1. You changed a file inside a container, then ran docker stop and docker start. Is the change still there?
Question 2. Which of these are still there after docker rm -f web? web was started with -v site:/usr/share/nginx/html nginx. (pick all)
Question 3. Why does a Dockerfile copy package.json and run npm ci before COPY . .?
Question 4. What is the difference between docker run and docker exec?
Question 5. You run docker build -t team-site . What is the dot?
Question 6. nginx's Dockerfile says EXPOSE 80. You run docker run -d nginx, without -p. Can curl localhost:80 on your computer reach nginx?
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.
Version 1Current
· First published
Copies are made from the current version. A copy you already have doesn’t change.
WorkshopyOfficial10 stepsQuiz9 interactiveDiagram
gitversion controlbeginners
WorkshopyOfficial16 steps2 quizzes15 interactiveDiagram
workshopyproduct touronboarding
WorkshopyOfficial10 steps2 quizzes10 interactive
data-literacystatisticsmean and median
WorkshopyOfficial12 steps2 quizzes15 interactive
pythonfunctionsreturn values
WorkshopyOfficial12 steps2 quizzes11 interactive
devopsregexregular expressions
WorkshopyOfficial12 steps2 quizzes11 interactive
gitpull requestscode review
Comments
No comments yet. Be the first to say how it went.