Skip to content
SparkTime, home

Guide · Technology

Docker for Beginners: What It Is, Why Developers Use It and How to Start

'It works on my machine' is one of software's oldest problems. Docker fixes it by packaging an app with everything it needs. Here is how containers work and how to run your first one.

6 min readIntermediate

For

Developers and students who have heard of Docker and want to understand and use it for their projects.

Level

Intermediate

Before you start

Comfort with the terminal and basic programming. Some familiarity with how web apps and servers work helps.

On this page

Key takeaways

  • A container packages an app with its dependencies so it runs the same everywhere. An image is the template and a container is a running instance.
  • A Dockerfile is a recipe for building an image. Docker Compose runs several containers together.
  • Containers are lightweight compared to virtual machines because they share the host's kernel.
  • Use volumes for data you want to keep, and never bake secrets into images.
  • Start with one small app, and learn a handful of commands: build, run, ps, logs, exec, stop.

You build an app on your laptop. It works. You send it to a teammate, and it breaks, because they have a different Python version, a missing library or a different operating system. You deploy it to a server, and it breaks again.

Docker solves this by packaging your app and everything it needs into a single unit, called a container, that runs the same on any machine with Docker. It has become a standard tool in modern software and cloud work. See cloud computing explained for where containers fit.

The key ideas

#

Image

#

An image is a read-only template that contains your application code, runtime (such as Python or Node.js), libraries and settings. Think of it like a recipe or a blueprint.

Container

#

A container is a running instance of an image. You can start, stop and delete containers without affecting the image. Think of it as a dish made from the recipe.

Dockerfile

#

A Dockerfile is a text file with instructions to build an image.

Registry

#

A registry stores and shares images. Docker Hub is the best known. Companies also run private registries.

Docker Engine

#

The software that builds and runs containers.

Containers vs virtual machines

#

A virtual machine (VM) includes a full operating system on top of virtualised hardware. It is heavy and slow to start.

A container shares the host's operating-system kernel and isolates only the application and its dependencies. It's lightweight, starts in seconds and uses fewer resources.

That's why you can run many containers on one machine.

Why use Docker

#
  • Consistency: the same environment in development, testing and production.
  • Easy setup: run a database or tool with one command instead of installing it.
  • Isolation: apps don't interfere with each other.
  • Portability: move between laptop, server and cloud.
  • Scalability: run more copies of a container when demand rises.
  • Team collaboration: new developers can start quickly.

Install Docker

#

Install Docker Desktop (Windows and macOS) or Docker Engine (Linux) from the official site. On Windows, Docker Desktop usually uses WSL 2. Check the current system requirements and licensing terms, since Docker Desktop's terms for larger companies differ from personal use.

Verify:

Shell
docker --versiondocker run hello-world

If you see a welcome message, it works.

Your first containers

#

Run a web server

#
Shell
docker run -d -p 8080:80 --name my-nginx nginx
  • -d: run in the background
  • -p 8080:80: map port 8080 on your computer to port 80 in the container
  • --name: give it a name
  • nginx: the image to use

Open http://localhost:8080 in a browser. You are running a web server without installing it.

Useful commands

#
Shell
docker ps                 # list running containersdocker ps -a              # list all containersdocker logs my-nginx      # see outputdocker exec -it my-nginx sh   # open a shell insidedocker stop my-nginx      # stop itdocker rm my-nginx        # remove itdocker images             # list imagesdocker rmi nginx          # remove an image

Containerise your own app

#

Suppose you have a small Python web app, app.py, and a requirements.txt.

Write a Dockerfile

#
Dockerfile
FROM python:3.12-slimWORKDIR /appCOPY requirements.txt .RUN pip install --no-cache-dir -r requirements.txtCOPY . .EXPOSE 8000CMD ["python", "app.py"]

What each line does:

  • FROM: the base image
  • WORKDIR: the working directory inside the container
  • COPY requirements.txt .: copy the dependency list first. Docker caches each step, so dependencies are reinstalled only when this file changes.
  • RUN: run a command during the build
  • COPY . .: copy the rest of your code
  • EXPOSE: documents the port
  • CMD: the command to run when the container starts

Build and run

#
Shell
docker build -t my-app .docker run -d -p 8000:8000 --name my-app my-app

Ignore unneeded files

#

Add a .dockerignore file:

Code
.gitnode_modules__pycache__.env

This keeps images small and keeps secrets out. See Git for beginners for the equivalent .gitignore.

Volumes: keep your data

#

Data inside a container disappears when it is removed. Use volumes for data you want to keep, such as a database.

Shell
docker run -d --name db \  -e POSTGRES_PASSWORD=secret \  -v pgdata:/var/lib/postgresql/data \  -p 5432:5432 postgres:16

-v pgdata:/var/lib/postgresql/data stores the data in a named volume that survives container removal. For a refresher on databases, see SQL for beginners.

For development, you can also use a bind mount to share your code folder with the container, so changes appear immediately.

Environment variables and secrets

#

Pass configuration with -e or an env file:

Shell
docker run -d --env-file .env my-app

Never put passwords or API keys inside the image or commit them to Git. Use environment variables, secret managers or your platform's secret features.

Docker Compose: several containers together

#

Real apps have more than one part: a web app, a database, a cache. Docker Compose defines them in one file, compose.yaml:

YAML
services:  web:    build: .    ports:      - "8000:8000"    environment:      - DATABASE_URL=postgresql://postgres:secret@db:5432/app    depends_on:      - db  db:    image: postgres:16    environment:      - POSTGRES_PASSWORD=secret    volumes:      - pgdata:/var/lib/postgresql/datavolumes:  pgdata:

Then:

Shell
docker compose up -d      # start everythingdocker compose logs -f    # watch logsdocker compose down       # stop and remove containers

Containers in the same Compose file can reach each other by service name (db).

Best practices

#
  • Use small, official base images (such as -slim or alpine), and pin versions rather than latest.
  • Order your Dockerfile for caching: dependencies first, then code.
  • Run as a non-root user where possible.
  • Keep one main process per container.
  • Don't store state in containers.
  • Scan images for vulnerabilities, and keep them updated.
  • Use multi-stage builds to keep final images small.
  • Add a .dockerignore.
  • Handle secrets safely.
  • Clean up unused images and containers (docker system prune, with care).

Docker and the cloud

#

Once an app runs in a container, you can deploy it on many platforms: cloud container services, managed platforms, or Kubernetes for large-scale orchestration. Start simple. Kubernetes is a later topic, and not needed for most beginner projects.

Understanding how your app is delivered helps with the whole stack. See how a website works and what is an API.

Troubleshooting

#
  • Port already in use: change the left side of -p, for example -p 8081:80.
  • Container exits immediately: check docker logs <name>. The main process may have crashed. See how to debug any bug.
  • Can't connect to a database: inside Compose, use the service name as host, not localhost.
  • Changes not showing: rebuild the image or use a bind mount in development.
  • Disk filling up: remove unused images, containers and volumes carefully.

Projects to practise

#
  1. Run a database container and connect to it from a local script.
  2. Containerise a small Flask, FastAPI or Express app.
  3. Use Compose to run an app plus a database.
  4. Push your image to Docker Hub.
  5. Deploy a containerised app to a cloud platform.

Add these to a portfolio. See build a developer portfolio website.

Common mistakes

#
  • Putting secrets in images
  • Using the latest tag everywhere
  • Huge images because of unneeded files
  • Storing data inside containers
  • Running everything as root
  • Treating containers like VMs
  • Skipping the .dockerignore
  • Ignoring logs when something fails

Frequently asked questions

#

Is Docker free?

#

The Docker Engine and many tools are open source. Docker Desktop has licensing terms: it's free for personal use, education and small businesses, while larger companies may need a paid plan. Check the current terms.

Do I need Docker to be a developer?

#

It's not required, but it's widely used and a valuable skill, especially for backend, DevOps and cloud roles.

What's the difference between Docker and Kubernetes?

#

Docker builds and runs containers. Kubernetes manages many containers across machines. Learn Docker first.

Can Docker run on Windows?

#

Yes, through Docker Desktop with WSL 2 or Hyper-V.

Is a container secure?

#

Containers provide isolation, but are not perfectly secure. Follow best practices, and keep images updated.


Commands and features change between versions. Check the official Docker documentation for current guidance.

Sources & further reading

  1. Docker documentation
  2. Docker Get Started guide
  • Guide · Technology

    Production Practices: Configuration, Logging, Docker and Health Checks for Spring Boot

  • Guide · Technology

    Caching in System Design: Patterns, Invalidation, Eviction and Redis

  • Guide · Career

    A LinkedIn Profile That Gets Recruiter Messages