Skip to article content
FrontendXLesson 06 / 16

Building and running containers with Docker

Move from a laptop shell to a safe, observable delivery pipeline: version control, Linux processes and permissions, container images, networking, configuration and secrets, deployment, health checks, observability, rollback, reproducibility and security hardening.

intermediate · about 55 minutes · 3 exercises

  • Explain
  • Implement
  • Debug

What you will be able to do

#
  • Build, run and inspect containers with the docker CLI.
  • Publish ports and attach volumes correctly.
  • Compose multiple services and let them reach each other by name.

Overview

#

The docker CLI builds images and manages containers. The two commands you use most are docker build and docker run, but understanding ports, volumes and networks is what makes containers useful.

Build and run

snippet.bash
docker build -t app:1.0 .
docker run --rm -p 8080:3000 --name app app:1.0
docker ps                       # running containers
docker logs app                 # stdout and stderr
docker exec -it app sh          # shell inside a running container

-p host:container publishes a container port on the host. The application inside must listen on 0.0.0.0, not 127.0.0.1, or the published port cannot reach it.

Volumes

A bind mount maps a host path into the container; a named volume is managed by Docker. Use a volume for data that must outlive the container, and a bind mount for local development.

snippet.bash
docker run -v app-data:/var/lib/app app:1.0        # named volume
docker run -v "$PWD":/app app:1.0                  # bind mount

Compose

Compose describes several services in one file. Services reach each other by service name over a shared network.

snippet.yaml
services:
  api:
    build: .
    ports:
      - "8080:3000"
    environment:
      DATABASE_URL: postgres://db:5432/app
    depends_on:
      - db
  db:
    image: postgres:16-alpine
    volumes:
      - db-data:/var/lib/postgresql/data
volumes:
  db-data:

Common pitfalls

  • Binding the app to localhost inside the container so the published port fails.
  • Using a bind mount in production and depending on a host path that does not exist.
  • Assuming depends_on waits for the database to be ready; it only waits for the container to start.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Implement

    Write the command to run app:1.0 in the background, publish container port 3000 on host port 8080, mount the named volume app-data at /data, and remove the container when it stops.

    starter.bash
    # docker run ...
    

    Reveal the reference answer
    reference.bash
    docker run -d --rm -p 8080:3000 -v app-data:/data --name app app:1.0

    Authored expected output: A detached container named app with the port published and the volume mounted.

    -d detaches, --rm cleans up on exit, -p maps the port, -v mounts the volume, and --name gives a stable name for logs and exec.

  2. Exercise 2Debug

    A container starts and logs 'listening on 3000', but curl http://localhost:8080 on the host is refused. The run command used -p 8080:3000. What is the most likely cause?

    starter.text
    docker run -p 8080:3000 app:1.0
    # app logs: listening on http://127.0.0.1:3000
    

    Reveal the reference answer
    reference.text
    The app is bound to 127.0.0.1 inside the container, so only the container loopback can reach it. Bind the server to 0.0.0.0 (for example HOST=0.0.0.0) so the published port reaches it.

    Authored expected output: The server listens on 0.0.0.0:3000 and the host port 8080 responds.

    Port publishing forwards host traffic to the container network interface, not to the container loopback. Binding to loopback inside the container hides the service from the published port.

  3. Exercise 3Explain

    Explain the difference between a bind mount and a named volume, and when you would choose each.

    starter.text
    // Describe bind mounts versus named volumes.
    

    Reveal the reference answer
    reference.text
    A bind mount maps a specific host path into the container, so the host controls the location and the content. It is ideal for local development where you edit host files and see changes immediately. A named volume is created and managed by Docker under its own storage area, so it is portable across hosts and not tied to a directory you must create. Use named volumes for persistent data such as database files, especially in production.

    Authored expected output: Bind mounts tie to a host path for development; named volumes are Docker-managed for persistence.

    The choice is about who owns the storage location and whether you need host-path coupling.

Common misconceptions

#
  • depends_on waits until the dependency is healthy: it only orders container start, not readiness.
  • A container keeps its data by default: without a volume, data is lost when the container is removed.

Follow-up practice

#
  • devops-networking

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.