Docker, start to ship · Chapter 1 of 26

Your first container

By the end of this chapter you can run a container, keep a web server running in one, visit it, read its logs, and clear away the ones that finished. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Containers
  • 5 tasks
  • Docker
  • Free chapter

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.

Why you need this

Tapri's menu site works on Bunty's laptop. On Ravi's laptop it does not, and nobody is sure about the server. "It works on my machine," says Bunty. Kabir's answer: then ship the machine.

Without containers, Ravi installs nginx by hand on every machine, gets a different version on each one, and finds out which settings differ only when the site breaks. A container is an app together with everything it needs, started from an image that is the same everywhere. Bunty has already tried, and left two finished containers lying around.

The lesson

Run a command in a container

The lab has Docker ready, with the images the chapters use already loaded (it has no internet). docker run takes an image and, after it, the command to run inside:

$ docker run --rm alpine echo "Chai is ready"
Chai is ready

alpine is a tiny Linux. Now the same kind of run without --rm, and with a name:

$ docker run --name chai-01-hello alpine cat /etc/alpine-release
3.20.10
$ docker ps
CONTAINER ID  IMAGE       COMMAND     CREATED     STATUS      PORTS       NAMES
$ docker ps -a
CONTAINER ID  IMAGE                            COMMAND               CREATED                 STATUS                   PORTS       NAMES
790f2845f277  docker.io/library/alpine:latest  echo it works on ...  Less than a second ago  Exited (0) 1 second ago              chai-01-bunty-test
25876f1fb2a9  docker.io/library/alpine:latest  sh -c echo trying...  Less than a second ago  Exited (1) 1 second ago              chai-01-bunty-oops
293e62f029a4  docker.io/library/alpine:latest  cat /etc/alpine-r...  Less than a second ago  Exited (0) 1 second ago              chai-01-hello

docker ps lists running containers: none. docker ps -a lists all of them: Bunty's two, and chai-01-hello, whose cat printed one line and finished. Look at its STATUS. Your IDs will differ. (This lab's docker is Podman, which takes the same commands; image names show as docker.io/library/alpine.)

With nothing after the image, docker run alpine runs alpine's own command, a shell, which ends at once and prints nothing. Give it a command.

Run a server

A web server should keep running, so start it detached with -d, name it with --name, and publish its port with -p:

$ docker run -d --name chai-01-web -p 8000:80 nginx
1b72a84f299a5e6a88044309436cceca30e0d5cae0780e35c9d7d9fe73d99ede
$ docker ps
CONTAINER ID  IMAGE                           COMMAND               CREATED        STATUS            PORTS                 NAMES
1b72a84f299a  docker.io/library/nginx:latest  nginx -g daemon o...  2 seconds ago  Up 2 seconds ago  0.0.0.0:8000->80/tcp  chai-01-web
$ curl -s localhost:8000 | head -4
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>

-p 8000:80 means the lab's port 8000 goes to port 80 in the container, where nginx listens. The lab's port comes first. A port nobody published has nobody behind it:

$ curl localhost:8001
curl: (7) Failed to connect to localhost port 8001 after 0 ms: Couldn't connect to server

A name belongs to one container, so running the same line twice fails:

$ docker run -d --name chai-01-web -p 8000:80 nginx
Error: creating container storage: the container name "chai-01-web" is already in use by 1b72a84f299a5e6a88044309436cceca30e0d5cae0780e35c9d7d9fe73d99ede. You have to remove that container to be able to reuse that name: that name is already in use

Read the logs

docker logs prints what the container wrote. nginx writes a line for every visit, so after your curl, the end of the logs shows it:

$ docker logs --tail 3 chai-01-web
2026/10/06 22:47:05 [notice] 1#1: start worker process 37
2026/10/06 22:47:05 [notice] 1#1: start worker process 38
10.0.2.100 - - [06/Oct/2026:22:47:07 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.88.1" "-"

That last line is the visit: a GET of /, answered with 200, by curl. Without --tail you get every line nginx printed since it started.

Clean up

Finished containers stay, with their logs and files, until you remove them. docker rm takes one or more names. It refuses a running one, which here is a good thing:

$ docker rm chai-01-web
Error: cannot remove container 1b72a84f299a5e6a88044309436cceca30e0d5cae0780e35c9d7d9fe73d99ede as it is running - running or paused containers cannot be removed without force: container state improper
$ docker rm chai-01-hello
chai-01-hello

Your turn

Your five tasks are at the top, and the terminal prints them too (lab tasks shows them again). Visit the server before you read its logs, and keep chai-01-web while you clean up. The interview question asks you to say in your own words what you saw in docker ps -a. lab reset starts the chapter over.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Run a one-off command in alpine

    Run one small command inside a container, and let it finish.

    1. Open the terminal below. It starts in the chapter folder.

    2. Run docker run with the alpine image, then a command after the image name. echo with a short message is a good one.

    3. Add --rm so Docker deletes the container when it finishes.

    To pass: you ran docker run with alpine and a command after the image name, in the chapter folder. A run with nothing after the image does not count. A command with a typing mistake or an error does not count, so type it again.

  2. Task 2

    Start chai-01-web on port 8000

    Start Tapri's web server in a container that keeps running.

    1. Run a container from the nginx image and name it chai-01-web.

    2. Start it detached (-d), so it keeps running in the background.

    3. Publish the container's port 80 on the lab's port 8000. The lab's port comes first.

    4. Run docker ps to see it running.

    To pass: a container named chai-01-web runs from nginx now, and localhost:8000 shows the Welcome to nginx page.

  3. Task 3

    Visit it, then read its logs

    Visit the web server, then read what it printed about your visit.

    1. Visit localhost:8000 with curl (or wget).

    2. After that, read chai-01-web's logs with docker logs.

    3. Find the line your visit left. It has GET and curl in it.

    To pass: you ran curl (or wget) on localhost:8000, then docker logs chai-01-web, both in the chapter folder. The order matters: visit first, logs second.

  4. Task 4

    Delete the finished containers

    Bunty left finished containers behind. Delete them.

    1. Run docker ps -a. It lists every container, the finished (Exited) ones too.

    2. Delete Bunty's two containers with docker rm. Their names start with chai-01-bunty.

    3. Delete any finished chai-01 container of your own too.

    To pass: no finished (Exited) container with a name starting with chai-01 is left, and chai-01-web is still there.

  5. Task 5 · interview question

    Interview: when the main process exits

    Interview question: what happens to a container when its main process exits?

    1. The main process is the command the container runs.

    2. Write one sentence in your own words. Say what happens to the container.

    3. Save the sentence in answer.txt, in the chapter folder.

    To pass: answer.txt says in plain words what the container itself does then.

What you will learn

  • docker run: makes a container from an image and runs a command in it.
  • Detached, named and published: a server that keeps running in the background, with a name, reachable on one of the lab's ports.
  • docker logs: everything the container printed, a line for every visit too.
  • docker ps -a and docker rm: every container, finished ones too, and how to remove them.

More in Containers

Build "Your first container" yourself

Tapri’s menu site works on one laptop and nowhere else. You ship the machine. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.