Docker Tutorial

Welcome to Docker Tutorial. Docker Tutorial provides concepts from basics like installation, docker file, etc. to advanced concepts with detailed examples.

Docker is used to build, package, share, and run applications in containers. This tutorial introduces Docker in a beginner-friendly order: what Docker is, how Docker images and containers work, how to run common Docker commands, how to write a Dockerfile, and how to use Docker Compose for multi-container applications.

Docker Tutorial

Prerequisites for Learning Docker

Basic exposure to Operating Systems like Windows, Linux, Mac OS X would be sufficient. Having a programming experience would be of great help.

You do not need to be a DevOps engineer to start Docker. You should be comfortable opening a terminal, navigating folders, and understanding what a web application, command-line program, port, and environment variable mean. For hands-on examples, install Docker Desktop on Windows or macOS, or Docker Engine on Linux.


Docker Tutorials and Learning Path

Use the following Docker tutorials as a practical learning path. Start with the architecture and commands, then move to installation, Dockerfile examples, and common runtime errors.

Docker Installation Tutorials

After installation, verify Docker from a terminal. The first command checks the Docker client and engine version. The second command checks whether Docker Compose is available through the modern docker compose command.

</>
Copy
docker --version
docker compose version

Docker Image Building Examples

Docker Errors and Their Solutions


What is Docker?

Docker is a platform for software containers. A docker contains all the components inside the container itself, required for running the application, thus assuring the running of application in any machine. Hence when an application is developed and moved through different servers or computers in a docker, no care has to be taken to maintain exact same environment through all the systems.

In simple terms, Docker lets you package an application with its runtime, libraries, configuration, and other dependencies so that it can run consistently across development, testing, and production environments. A container is not the same as a full virtual machine. Containers share the host operating system kernel, while virtual machines usually include a complete guest operating system.

For official reference while learning, see the Docker documentation pages for Docker overview, Docker Compose concepts, and Dockerfile reference.

Docker Components: Client, Daemon, Images, Containers, Registry, and Compose

To understand Docker clearly, learn the main Docker components and how they work together.

Docker componentWhat it doesBeginner example
Docker ClientThe command-line tool that sends your commands to Docker.docker run nginx
Docker DaemonThe background service that builds images, starts containers, and manages Docker objects.The engine that actually runs the container.
Docker ImageA read-only template used to create containers.An nginx, python, or mysql image.
Docker ContainerA running instance of an image with its own process space, filesystem changes, and network settings.A running web server container.
Docker RegistryA place to store and download images.Docker Hub or a private registry.
DockerfileA text file containing instructions to build a Docker image.FROM, COPY, RUN, and CMD instructions.
Docker ComposeA tool for defining and running multiple containers using a Compose YAML file.A web app container with a database container.

Docker Image vs Docker Container

A Docker image is the packaged template. A Docker container is what runs from that image. You can create many containers from the same image, just as you can create many documents from one template.

Comparison pointDocker imageDocker container
StateRead-only templateRunning or stopped instance
PurposePackages the application and dependenciesRuns the application process
Created bydocker build or docker pulldocker run or docker compose up
Examplenginx:latestA specific running Nginx container

First Docker Commands for Beginners

The fastest way to learn Docker is to run a small container, inspect it, stop it, and remove it. The following command starts an Nginx web server container and maps port 8080 on your computer to port 80 inside the container.

</>
Copy
docker run --name tk-nginx -p 8080:80 -d nginx

Open http://localhost:8080 in your browser. You should see the default Nginx page if the container is running correctly.

Use these commands to check, stop, restart, and remove the container.

</>
Copy
docker ps
docker stop tk-nginx
docker start tk-nginx
docker logs tk-nginx
docker rm -f tk-nginx

Common Docker command patterns are easier to remember when you connect them with the object they manage.

TaskDocker commandMeaning
Download an imagedocker pull nginxPulls the Nginx image from a registry.
List running containersdocker psShows containers that are currently running.
List all containersdocker ps -aShows running and stopped containers.
List imagesdocker imagesShows images available locally.
Enter a running containerdocker exec -it container_name shOpens a shell inside the container when available.
Remove a containerdocker rm container_nameDeletes a stopped container.
Remove an imagedocker rmi image_nameDeletes a local image if it is not used by a container.

Dockerfile Example for a Simple Python Application

A Dockerfile describes how to build a Docker image. The example below creates a small Python image that runs app.py. This is a syntax example; adapt the base image, files, and command to your project.

</>
Copy
FROM python:3.12-slim

WORKDIR /app
COPY app.py .

CMD ["python", "app.py"]

Create a simple app.py file in the same folder.

</>
Copy
print("Hello from Docker")

Build and run the image with the following commands.

</>
Copy
docker build -t tk-python-demo .
docker run --rm tk-python-demo

The output should be:

Hello from Docker

Docker Compose Example for a Web App and Database

Docker Compose is useful when your application needs more than one container. For example, a web application may need a database, a cache, or a background worker. A Compose file defines services, ports, volumes, networks, and environment variables in one YAML file.

The following Compose example starts a PostgreSQL database and stores its data in a named volume.

</>
Copy
services:
  db:
    image: postgres:16
    environment:
      POSTGRES_DB: demo
      POSTGRES_USER: demo_user
      POSTGRES_PASSWORD: demo_password
    ports:
      - "5432:5432"
    volumes:
      - db_data:/var/lib/postgresql/data

volumes:
  db_data:

Save the file as compose.yaml or docker-compose.yml, then run:

</>
Copy
docker compose up -d
docker compose ps
docker compose down

docker compose up -d starts the service in detached mode. docker compose down stops and removes the containers created by the Compose project. The named volume can remain unless you remove it separately.

Docker Volumes, Ports, and Networks Explained

Three Docker concepts become important as soon as you run real applications: volumes, ports, and networks.

Docker conceptWhy it mattersExample
VolumePersists data outside the container lifecycle.Database data stored in db_data.
Port mappingMakes a container service reachable from the host computer.-p 8080:80 maps host port 8080 to container port 80.
NetworkAllows containers to communicate with each other.A web container connects to a database container by service name in Compose.

Containers are temporary by design. If a container is removed, changes inside its writable layer may be lost. Use volumes for data that must survive container replacement, such as database files, uploads, or generated reports.

Docker vs Virtual Machine

Docker containers and virtual machines both help isolate workloads, but they do it differently. A virtual machine runs a full guest operating system on a hypervisor. A Docker container shares the host operating system kernel and isolates the application process using container features provided by the operating system.

PointDocker containerVirtual machine
Operating systemShares the host kernelIncludes a guest OS
StartupUsually fasterUsually slower than a container
Image sizeOften smallerUsually larger because of the guest OS
Best useApplication packaging and repeatable environmentsFull OS isolation and running different operating systems

Docker Security Basics for Beginners

Docker is powerful, but a container is not a security boundary you should blindly trust. Follow basic security practices from the start.

  • Use official or trusted base images whenever possible.
  • Prefer smaller base images that include only what the application needs.
  • Do not store passwords, API keys, or private tokens inside a Dockerfile or image.
  • Use environment variables, secret management, or deployment-platform secrets for sensitive values.
  • Run containers as a non-root user when your application supports it.
  • Keep base images updated and rebuild images when security fixes are available.
  • Do not publish container ports unless they must be reachable from outside.

A safer Dockerfile usually creates or uses a non-root user before running the application.

</>
Copy
FROM node:22-slim

WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .

USER node
CMD ["node", "server.js"]

Common Docker Beginner Mistakes

  • Confusing an image with a container: an image is the template; a container is the running or stopped instance.
  • Forgetting port mapping: the app may run inside the container but remain unreachable from the host without -p.
  • Putting secrets in images: anything copied into an image can be exposed later.
  • Not using volumes for persistent data: database data can be lost if stored only inside a disposable container layer.
  • Using latest blindly: versioned image tags are more predictable for production builds.
  • Ignoring logs: docker logs container_name is often the first command to use when a container exits.

How to Learn Docker in the Right Order

The best way to learn Docker is to build small examples and connect each command with a concept. Use this order:

  1. Install Docker and verify docker --version.
  2. Run a ready-made image such as nginx or hello-world.
  3. Learn docker ps, docker images, docker logs, docker stop, and docker rm.
  4. Write a simple Dockerfile for a small app.
  5. Build an image using docker build.
  6. Run the image using docker run and map ports when needed.
  7. Add a volume for persistent data.
  8. Use Docker Compose for a multi-container setup.
  9. Learn image tags, registries, and basic security practices.

Docker Tutorial FAQs

What is the best way to learn Docker?

The best way to learn Docker is to run containers first, then build your own image with a Dockerfile, and finally use Docker Compose for a small multi-container project. Avoid learning only theory; Docker becomes clear when you repeatedly use commands such as docker run, docker ps, docker logs, and docker build.

What are the main Docker components?

The main Docker components are Docker Client, Docker Daemon, Docker Images, Docker Containers, Docker Registry, Dockerfile, and Docker Compose. The client sends commands, the daemon manages Docker objects, images provide templates, containers run applications, registries store images, Dockerfiles build images, and Compose runs multi-container applications.

Is Docker still useful for beginners?

Yes. Docker is still useful for beginners who want consistent development environments, easier dependency setup, and repeatable application packaging. It is commonly used with web applications, APIs, databases, background workers, CI pipelines, and local development environments.

What is the difference between Dockerfile and Docker Compose?

A Dockerfile contains instructions to build one image. Docker Compose defines how one or more containers should run, including services, ports, volumes, networks, and environment variables. In many projects, Compose refers to a Dockerfile to build an application image.

Why do some teams move away from Docker Desktop?

Some teams choose alternatives because of licensing, resource usage, platform requirements, or preference for other container tools. That does not mean containers are no longer useful. The important skill is understanding container images, containers, registries, volumes, networking, and repeatable builds.

Editorial QA Checklist for This Docker Tutorial

  • Does the tutorial explain Docker images, containers, Dockerfile, registry, and Compose without mixing their meanings?
  • Are all Docker command examples safe for beginners and easy to clean up after running?
  • Do new code blocks use PrismJS-compatible classes such as language-bash, language-python, language-yaml, language-plaintext, syntax, and output?
  • Are persistent data examples clearly connected to Docker volumes?
  • Does the tutorial avoid unsupported adoption statistics, exaggerated claims, and outdated Docker Compose syntax?
  • Are official Docker documentation links included for readers who want reference material?

Conclusion: Docker Tutorial for Practical Container Skills

Docker is easiest to learn by building and running small containers. Start with basic commands, understand the difference between images and containers, write a Dockerfile, and then use Docker Compose when an application needs more than one service. Once these basics are clear, Docker architecture, security, registries, CI/CD workflows, and production deployments become easier to understand.