Docker Labs · Lab 1 of 6

Docker Basics: Containers, Images, Volumes and Networks

Run your first containers, read their logs, open a shell inside one, tag an image, keep data in a named volume when a container is replaced, and connect two containers by name on a user-defined network.

Environment
Local
Level
Beginner
Time
About 30 min
Cost
Free

You will build

The everyday Docker commands in your fingers, and a clear picture of what lives in a container (throwaway), in an image (read-only template), in a volume (your data) and on a network (how containers find each other).

Before you start

  • Docker Desktop, or Docker Engine on Linux (docker version shows a Server section)

  • A terminal

Members can save this page, take notes and track progress. See membership · Sign in

Every command in this lab runs on your own computer and uses small public images (alpine, nginx). All names start with lab-, so they won't clash with anything you already have.

Container IDs, image IDs, timestamps and sizes will differ on your machine; everything else should match.

Steps

  1. 1Run a one-off container

    docker run creates a container from an image, runs its command and, with --rm, deletes the container when the command ends. Docker pulls the image first if you don't have it.

    Run
    docker run --rm alpine:3.24 echo "Hello from a container"
    

    Expected output

    Hello from a container
  2. 2Run a web server in the background

    -d runs the container detached, --name gives it a name you can use instead of its ID, and -p 8080:80 publishes container port 80 on port 8080 of your computer.

    Run
    docker run -d --name lab-web -p 8080:80 nginx:1.29-alpine
    docker ps --filter name=lab-web
    

    Expected output

    d6bd3ba091e3ade21d0dabc45698f0af0c32f6e1ee57f20885d3bb33a1e94c7e
    CONTAINER ID   IMAGE               COMMAND                  CREATED         STATUS        PORTS                                     NAMES
    d6bd3ba091e3   nginx:1.29-alpine   "/docker-entrypoint.…"   2 seconds ago   Up 1 second   0.0.0.0:8080->80/tcp, [::]:8080->80/tcp   lab-web
    Check:

    Open http://localhost:8080 — you see the “Welcome to nginx!” page.

    Hint

    “port is already allocated” means something else uses 8080. Use -p 8081:80 and open port 8081.

  3. 3Read the logs

    Containers write logs to stdout and stderr; Docker keeps them. Your browser visit shows up as an access-log line.

    Run
    curl -s localhost:8080 | head -4
    docker logs lab-web | tail -3
    

    Expected output

    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    2026/11/26 01:44:40 [notice] 1#1: start worker process 38
    2026/11/26 01:44:40 [notice] 1#1: start worker process 39
    172.17.0.1 - - [26/Nov/2026:01:44:41 +0000] "GET / HTTP/1.1" 200 896 "-" "curl/8.5.0" "-"
    Hint

    docker logs -f lab-web follows the log live; press Ctrl+C to stop following (the container keeps running).

  4. 4Run commands inside the container

    docker exec runs a command in a running container. Add -it and a shell (docker exec -it lab-web sh) to look around interactively; type exit to leave.

    Run
    docker exec lab-web cat /etc/os-release | head -2
    docker exec lab-web nginx -v
    

    Expected output

    NAME="Alpine Linux"
    ID=alpine
    nginx version: nginx/1.29.8
  5. 5Stop and remove it

    A stopped container still exists (and keeps its name) until you remove it.

    Run
    docker stop lab-web
    docker rm lab-web
    docker ps -a --filter name=lab-web
    

    Expected output

    lab-web
    lab-web
    CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
    Check:

    The last command lists no containers.

  6. 6Images and tags

    An image is identified by its ID; tags are just names pointing at an ID. Tagging doesn't copy anything: both names show the same image ID.

    Run
    docker images alpine
    docker image tag alpine:3.24 lab-base:1.0
    docker images lab-base
    

    Expected output

    IMAGE           ID             DISK USAGE   CONTENT SIZE   EXTRA
    alpine:3.24     be2656061b53         13MB         3.94MB
    alpine:latest   28bd5fe8b56d         13MB         3.93MB
    IMAGE          ID             DISK USAGE   CONTENT SIZE   EXTRA
    lab-base:1.0   be2656061b53         13MB         3.94MB
    Hint

    Older Docker versions show the columns REPOSITORY TAG IMAGE ID CREATED SIZE. Same information.

  7. 7Keep data in a named volume

    A container's own filesystem disappears with the container. A volume lives outside it: here two different containers write to the same volume one after the other, and the data survives.

    Run
    docker volume create lab-notes
    docker run --rm -v lab-notes:/data alpine:3.24 sh -c 'echo "first note" >> /data/notes.txt'
    docker run --rm -v lab-notes:/data alpine:3.24 sh -c 'echo "second note" >> /data/notes.txt; cat /data/notes.txt'
    

    Expected output

    lab-notes
    first note
    second note
    Check:

    Both notes are printed by the second container, although the first one was already deleted.

  8. 8Connect containers by name

    On a user-defined network, Docker's built-in DNS resolves container names. On the default network it doesn't, so the second request fails.

    Run
    docker network create lab-net
    docker run -d --name lab-api --network lab-net nginx:1.29-alpine
    docker run --rm --network lab-net alpine:3.24 wget -qO- http://lab-api | grep -o '<title>.*</title>'
    docker run --rm alpine:3.24 wget -qO- -T 3 http://lab-api
    

    Expected output

    04d95d87b1c28bbb0fbad255c7e348e5854452c77fe0dc61161dd0bbb5702dc3
    337042a56af2b7fc149dbd55fc3a4f359a4d0a8e25457f1cd38f2ce08f866319
    <title>Welcome to nginx!</title>
    wget: bad address 'lab-api'
    Check:

    The container on lab-net reaches lab-api by name; the one on the default network can't.

  9. 9Clean up

    Run
    docker rm -f lab-api
    docker network rm lab-net
    docker volume rm lab-notes
    docker image rm lab-base:1.0
    

    Expected output

    lab-api
    lab-net
    lab-notes
    Untagged: lab-base:1.0

In production

In this lab you started containers by hand with docker run. In production nobody types docker run: a platform (Compose on a single host, ECS, Cloud Run, Kubernetes) starts containers from a file you keep in Git. The concepts are the same ones you just used.

What changes

In this lab In production
docker run by hand Containers are declared in a file (Compose, a task definition, a Kubernetes manifest) and started by a platform
nginx:1.29-alpine from Docker Hub Images from your own registry, pinned by version and digest, scanned before use
Logs read with docker logs Logs shipped to a central system (CloudWatch, Cloud Logging, Loki) with retention
A named volume on your laptop Managed storage for state: a managed database, object storage, or volumes with backups
Containers found by name on lab-net Service discovery: Compose networks, Kubernetes Services, a load balancer
docker exec to look inside Shell access is the exception: debug from logs, metrics and traces; slim images often have no shell at all

Treat containers as cattle

A container can be replaced at any moment (new deploy, node failure, scaling). Anything that must survive goes in a volume or, better, a managed service. If deleting a container loses data, the design is wrong.

Production checklist

  • Images come from a registry you control, with immutable version tags
  • No data you care about lives only inside a container's filesystem
  • Every container has a name or label that says what it is and who owns it
  • Logs go to stdout/stderr and are collected centrally
  • Containers that must talk to each other share a network; nothing else is exposed
  • Only the ports that clients need are published

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake