Skip to content
Docker Beginner's Complete Guide: Install, Run, Build & Compose
Operations 13 min read

Docker Beginner's Complete Guide: Install, Run, Build & Compose

This hands-on tutorial walks through Docker fundamentals on Ubuntu 24.04, covering installation, running Nginx containers, persisting data with named volumes, building custom images via Dockerfile, and orchestrating multi-service apps with Docker Compose, including troubleshooting tips and cleanup commands.

Linux Tech Enthusiast
Linux Tech Enthusiast
Linux Tech Enthusiast

Core Concepts: Images, Containers, and Registries

An image packages a program, its required files, and default startup configuration. For example, nginx:alpine has image name nginx and tag alpine. To guarantee exact reuse, pin the image by its digest.

A container is a running instance created from an image. Multiple containers can run from the same image, each with its own process and writable layer. Linux containers share the host kernel while isolating processes and filesystem views.

An image registry distributes images. Use docker pull to fetch existing images or docker build to create images from a Dockerfile.

Install Docker on Ubuntu 24.04 LTS

Add Docker's APT repository and install the engine, build plugin, and Compose plugin:

sudo apt update
sudo apt install -y ca-certificates curl
sudo install -d -m 0755 /etc/apt/keyrings
sudo curl -fsSL -o /etc/apt/keyrings/docker.asc https://download.docker.com/linux/ubuntu/gpg
sudo chmod 0644 /etc/apt/keyrings/docker.asc

docker_arch="$(dpkg --print-architecture)"
sudo tee /etc/apt/sources.list.d/docker.sources >/dev/null <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: noble
Components: stable
Architectures: $docker_arch
Signed-By: /etc/apt/keyrings/docker.asc
EOF

sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo systemctl enable --now docker
sudo docker run --rm hello-world
docker compose version

Seeing Hello from Docker! confirms image pull and container run. docker compose version verifies the Compose plugin. The docker group grants root-equivalent access; only trusted accounts should be added.

Run the First Nginx Container

Pull the image and start a detached container with a friendly name and port mapping:

docker pull nginx:alpine
docker run -d --name docker-guide-web \
  -p 127.0.0.1:8080:80 nginx:alpine

docker ps
curl -fsS --retry 5 --retry-connrefused http://127.0.0.1:8080/

The colon-separated -p syntax binds host address, host port, and container port. Binding to 127.0.0.1 keeps the exercise local. If port 8080 is busy, use 8081 instead.

Troubleshooting commands:

docker logs --tail 20 docker-guide-web
docker exec docker-guide-web nginx -t
docker stop docker-guide-web
docker rm docker-guide-web
docker ps

shows running containers; add -a to include stopped ones.

Persist Data with Named Volumes

Container writable layers disappear when the container is removed. For data that must survive container recreation (databases, uploads), use Docker-managed named volumes .

docker volume create docker-guide-data

docker run --rm \
  --mount type=volume,src=docker-guide-data,dst=/data \
  alpine:3.22 sh -c 'echo hello-volume > /data/note.txt'

docker run --rm \
  --mount type=volume,src=docker-guide-data,dst=/data,readonly \
  alpine:3.22 cat /data/note.txt

The first container writes hello-volume to the volume; the second reads it back read-only, proving persistence across container lifecycles.

For development where you edit host files directly, use a bind mount instead:

--mount "type=bind,src=$PWD/site,dst=/usr/share/nginx/html,readonly"

.

Build a Custom Image with Dockerfile

Create a project directory, add an HTML page, and write a Dockerfile:

mkdir -p docker-guide/site
cd docker-guide

cat > site/index.html <<'EOF'
<!doctype html>
<html lang="zh-CN">
<meta charset="utf-8">
<title>Docker Guide</title>
<h1>你好,Docker!</h1>
</html>
EOF

cat > Dockerfile <<'EOF'
FROM nginx:alpine
COPY site/ /usr/share/nginx/html/
EOF

printf '%s
' '.git' '.env' '.env.*' > .dockerignore
docker build -t docker-guide-web:1.0 .
docker run -d --name docker-guide-custom \
  -p 127.0.0.1:8080:80 docker-guide-web:1.0

The trailing . sets the build context. .dockerignore excludes unnecessary files. After verifying the page at http://127.0.0.1:8080/, stop and remove the container; the image remains.

Common Dockerfile instructions: RUN executes at build time (e.g., install dependencies). CMD sets the default container startup command.

Copy dependency manifests first, then install dependencies, then copy application code to leverage build cache when only code changes.

Orchestrate Two Services with Docker Compose

Create compose.yaml alongside the Dockerfile:

services:
  web:
    build: .
    image: docker-guide-web:1.0
    ports:
      - "127.0.0.1:8080:80"
  probe:
    image: alpine:3.22
    profiles: [tools]
    depends_on:
      - web
    command: ["wget", "-qO-", "http://web/"]

Compose places services on a shared default network. The probe service uses the service name web to reach Nginx on container port 80. Using 127.0.0.1 inside a container would refer to that container itself. Internal-only services (e.g., databases) typically omit ports. profiles: [tools] makes probe run only on demand. The short depends_on form only controls startup order; for readiness (e.g., database accepting connections), add a healthcheck and use service_healthy.

Validate and start:

docker compose config -q
docker compose up -d --build
docker compose ps
curl -fsS --retry 5 --retry-connrefused http://127.0.0.1:8080/
docker compose run --rm probe

Both the host curl and the probe container should return "你好,Docker!", confirming host port mapping and internal service-name resolution.

Troubleshooting and Cleanup

Cannot reach Docker: Run sudo systemctl status docker. If sudo works, check socket permissions for the current user.

Page not loading: Use docker compose ps -a to see if services exited, then docker compose logs --tail 50 web for errors. For port conflicts, run ss -ltnp on the host. Remember 127.0.0.1 points to the machine you're on; access the page from the Docker host.

Changes not reflected: The page was baked into the image via COPY. Edit site/index.html, then run docker compose up -d --build to rebuild and restart.

When finished, clean up project resources:

docker compose down
docker volume rm docker-guide-data
docker system df
docker compose down

removes containers and the default network. The named volume docker-guide-data persists until explicitly removed. docker system df shows disk usage for images, containers, and volumes.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

dockernginxdockerfilecontainersimagesdocker-composeubuntuvolumes
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.