Docker containers illustration

Docker Tutorial for Beginners: Images, Containers and Your First Dockerfile

Introduction

Docker is the skill that makes the rest of DevOps make sense. Before Docker, running an application meant installing the right language version, the right libraries and the right configuration on every server — and hoping nothing differed between your laptop and production. Docker packages the application and everything it needs into a single portable unit, an image, and runs it as a container. The same image runs on your laptop, in CI, and on a server, with the same behaviour.

This tutorial teaches Docker from zero: what an image and a container actually are, the commands you will use every day, how to write a good Dockerfile, and how to run a multi-container application with Docker Compose. You need a computer with Docker installed and basic comfort with the terminal. No prior container experience is assumed.

Version note (26 Nov 2026): the latest Docker Engine is 29.10.1 (released 24 Nov 2026). Version 29 is the only supported major line; Docker Engine 28 lost security support in May 2026, and version 25.0 reaches end of life on 4 Dec 2026 — if you are still on either, upgrade before following this tutorial. The commands in this article work on any recent Docker version.

What Docker Is, in Plain Language

A container is a lightweight, isolated process that runs from an image. An image is a read-only template: a layered snapshot of a filesystem containing the application code, its runtime, libraries and configuration. A container is a running instance of an image — you can start, stop, delete and restart it, and each container is isolated from the others and from the host by Linux namespaces and control groups.

Two ideas matter most. First, images are immutable: you never change a running container’s image; you build a new image version and start a new container from it. Second, containers share the host kernel, which is why they start in under a second and use far less memory than virtual machines. A virtual machine carries a whole guest operating system; a container carries only the application and its dependencies on top of the host kernel.

Docker is not a replacement for a virtual machine in every case — Windows and macOS users run Docker inside a small Linux virtual machine (Docker Desktop handles this for you) — but for running applications, containers are the standard unit in 2026. Kubernetes, the subject of our Kubernetes Tutorial for Beginners, orchestrates containers; Docker is how you build and run them in the first place.

Installing Docker and Checking It Works

On Linux, install Docker Engine from Docker’s official repository for your distribution — do not rely on the version in your operating system’s default package list, which is often years old. On Windows and macOS, install Docker Desktop, which includes the Engine, the command-line tools and Docker Compose. After installing, check the version and run the test image:

docker version
docker run hello-world

If hello-world prints its greeting, Docker is working. Two commands you will use constantly are docker ps (list running containers) and docker images (list images on your machine):

docker ps
docker images

If Docker reports a permissions error on Linux, your user is not in the docker group. Adding yourself to it (sudo usermod -aG docker $USER, then log out and back in) is the standard fix; running every Docker command with sudo is a habit worth avoiding, because it creates root-owned files in your project directories.

Images and Containers: The Core Commands

Pull an image from Docker Hub, Docker’s public registry, and run it:

docker pull nginx:1.27
docker run -d --name my-web -p 8080:80 nginx:1.27

That one line does a lot: -d runs the container in the background (detached), --name gives it a memorable name, and -p 8080:80 maps port 8080 on your machine to port 80 inside the container. Open http://localhost:8080 and you should see the nginx welcome page. Then:

docker ps                 # see my-web running
docker logs my-web        # read its output
docker exec -it my-web sh # open a shell inside it
docker stop my-web        # stop it
docker start my-web       # start it again
docker rm my-web          # delete the container (image stays)
docker rmi nginx:1.27     # delete the image

A container’s filesystem is ephemeral by design. Delete the container and anything written inside it is gone. That is a feature, not a bug — it forces you to keep data somewhere durable, which is the next section’s job.

A note on tags. nginx:1.27 pins a specific version. nginx:latest means “whatever the newest image was when it was pulled” — it changes without warning and makes rollbacks unreliable. Pin versions in anything you intend to run twice, exactly as you would pin a dependency in a project.

Writing Your First Dockerfile

A Dockerfile is a recipe for building an image. Here is a small Node.js application — a single file that serves “Hello from Docker” — packaged properly:

# Dockerfile
FROM node:22-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .

FROM node:22-alpine
WORKDIR /app
COPY --from=build /app /app
USER node
EXPOSE 3000
CMD ["node", "server.js"]

Build it and run it:

docker build -t hello-docker:1.0 .
docker run -d --name hello -p 3000:3000 hello-docker:1.0
curl http://localhost:3000

Three practices in that Dockerfile are worth copying into every project:

  1. Multi-stage builds. The first stage installs dependencies; the second stage copies only the finished application. The final image contains no build tools, no package cache and no source history, so it is smaller and has a smaller attack surface.
  2. Copy dependencies first, code second. Docker caches each layer. If you copy package*.json and run npm ci before copying your code, a code change rebuilds only the last layers — dependency installation is cached. Copying everything at once invalidates the cache on every change.
  3. Do not run as root. The USER node line runs the application as an unprivileged user. Containers that run as root and are later exposed to a network are a common, avoidable security hole.

Add a .dockerignore file next to the Dockerfile — the same idea as .gitignore — listing node_modules, .git, .env and anything else that should never enter the build context. Without it, COPY . . sends your whole project directory, including secrets and local dependencies, into the image build.

Volumes, Networks and Environment Variables

Three concepts turn a demo container into something usable:

Volumes — durable data. A named volume survives container deletion. This is how a database keeps its data:

docker volume create app-data
docker run -d --name db -v app-data:/var/lib/postgresql/data \
  -e POSTGRES_PASSWORD=dev-password postgres:16

Bind mounts (-v ./data:/data) map a host directory into a container instead; they are convenient for development, but named volumes are the safer default for data you care about, because Docker manages their lifecycle.

Networks — containers talking to each other. Containers on the same user-defined network can reach each other by name. Create one and attach both containers:

docker network create app-net
docker run -d --name db --network app-net postgres:16
docker run -d --name api --network app-net -p 3000:3000 hello-docker:1.0

Inside the api container, the database is reachable at db:5432 — the container name is its hostname. The default bridge network does not provide this name resolution, which is why creating your own network is a good habit.

Environment variables — configuration outside the image. The same image should run in development, staging and production with different settings, so pass configuration with -e:

docker run -d --name api -e NODE_ENV=production -e DATABASE_URL=postgres://db:5432/app hello-docker:1.0

Never bake a real password or API key into an image. For local development, an .env file passed with --env-file is fine; in production, use your platform’s secrets manager.

Docker Compose: Multi-Container Applications

Writing out long docker run commands for every service is unmaintainable. Docker Compose describes the whole application in one YAML file:

# compose.yaml
services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      DATABASE_URL: postgres://postgres:dev-password@db:5432/app
    depends_on:
      - db
  db:
    image: postgres:16
    volumes:
      - app-data:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: dev-password

volumes:
  app-data:
docker compose up -d     # start everything
docker compose ps        # status
docker compose logs -f   # follow logs
docker compose down      # stop and remove containers (volumes stay)
docker compose down -v   # also remove volumes — data gone

Compose creates a network for the project automatically, so web reaches the database at db:5432 with no extra configuration. depends_on starts the database first, but note it only waits for the container to start, not for the database to be ready — for a real application, add a health check or a retry loop in your application code rather than assuming the database is up.

Common Beginner Mistakes

  1. Storing data in a container’s filesystem. Use a volume. Data in a container is lost when the container is deleted — and containers are meant to be deleted.
  2. Using latest tags. Pin versions, so a rebuild next month produces the same image.
  3. Running everything as root. Add a non-root USER to your Dockerfiles.
  4. Committing secrets into an image. A secret baked into an image layer stays in the layer history even if you delete it in a later layer. Use environment variables or a secrets manager.
  5. One giant image. Multi-stage builds and a .dockerignore keep images small, fast to pull and easier to scan for vulnerabilities.
  6. Treating containers as virtual machines. Do not SSH into a container to “fix” it by hand and leave the fix there. Fix the Dockerfile or the image, rebuild, and redeploy — that is the whole point of containers.

What to Learn Next

  • Kubernetes — once you are comfortable with Docker, our Kubernetes Tutorial for Beginners shows how to run containers across many machines, with self-healing, scaling and rolling updates.
  • CI/CD — build and push images automatically on every commit; our GitHub Actions tutorial (Batch 2) builds a pipeline that tests, builds and pushes a Docker image.
  • Image scanning — scan images for known vulnerabilities before deploying them (docker scout or your registry’s scanner), and keep base images updated.
  • Registries — Docker Hub for public images, your cloud provider’s registry (ECR, ACR, Artifact Registry) for private ones.

Frequently Asked Questions

What is the difference between a Docker image and a container? An image is a read-only template; a container is a running instance of an image. You can run many containers from one image, and deleting a container does not delete its image.

Is Docker the same as Kubernetes? No. Docker builds and runs containers on one machine; Kubernetes orchestrates containers across many machines. You need containers to use Kubernetes, and Docker is the usual way to create them.

Do I need Docker Desktop? On Windows and macOS, yes — Docker Desktop provides the Linux virtual machine Docker needs. On Linux, Docker Engine installed directly is enough, and Docker Desktop is optional.

How much does Docker cost? Docker Engine is free and open source. Docker Desktop is free for personal use, small businesses and open-source projects; larger commercial use requires a paid subscription — check Docker’s pricing page for the current terms before installing it at work.

Why is my image so large? Usually because it includes build tools, package caches or a full operating system image as its base. Use a slim base image (such as -alpine variants), multi-stage builds and a .dockerignore file, and check the result with docker images.

Conclusion

Docker’s core idea fits in one sentence: package the application and its dependencies into an image, run it as a container, and keep data and configuration outside the image. Learn the commands in this tutorial, write a clean multi-stage Dockerfile, and run a two-service application with Compose — and you have the container skill that underpins Kubernetes, CI/CD and most cloud jobs in 2026. For the career context, see our DevOps Engineer Roadmap 2026.

Try it today: containerise a small application of your own, run it with Compose alongside a database, stop it, delete the containers, and bring it back with docker compose up — your data should still be there, because it lives in a volume.

Scroll to Top