Marcio Cunha

Docker: complete beginner's guide (2025)

Curious about how software runs smoothly on any computer without breaking? This guide explains Docker containers, essential commands, and configuration files in plain English.

Marcio Cunha9 min
Also available in:EspañolPortuguês
Summary
  • Containers share the operating system kernel, making them significantly lighter and faster than traditional virtual machines.
  • Images act as read-only blueprints, while containers serve as the actual running instances of those applications.
  • The installation process varies by operating system, requiring Docker Desktop for Windows and Mac, or specific repository commands for Linux.
  • Managing applications involves distinct commands for pulling images, running containers in the background, and inspecting active services.
  • Docker Compose simplifies multi-container environments by orchestrating web services and databases using a single YAML configuration file.

Docker lets you package apps and dependencies into containers, which are isolated software boxes that run the same everywhere. This guide covers the essentials to get you started fast.

1. What is Docker?

Docker is a containerization platform, meaning it packages software so it can run reliably on any machine. Containers share the OS kernel, which is the core part of the operating system that manages hardware, so they are lighter and faster than full virtual machines.

  • Portability: run the same in any environment.
  • Consistency: dev, test and prod match.
  • Isolation: apps don't conflict.
  • Efficiency: fewer resources than VMs.

2. Core concepts

  • Image: read-only template with app and dependencies.
  • Container: running instance of an image.
  • Dockerfile: recipe to build images.

3. Install Docker

Windows and macOS: install Docker Desktop from the official site.

Linux (Ubuntu/Debian):

sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install docker-ce
docker --version

4. Essential commands

Images:

docker pull nginx
docker images
docker rmi nginx

Containers:

docker run -d -p 8080:80 nginx
docker ps
docker ps -a
docker stop 
docker rm 
docker exec -it  sh

5. Your first Docker project (Node.js)

Folder:

mkdir my-first-docker
cd my-first-docker

package.json:

{
  "name": "my-first-docker",
  "version": "1.0.0",
  "scripts": { "start": "node server.js" },
  "dependencies": { "express": "^4.18.0" }
}

server.js:

const express = require("express");
const app = express();
const PORT = process.env.PORT || 3000;

app.get("/", (req, res) => {
  res.send("

My first Docker app!

"); }); app.listen(PORT, () => console.log("Running on port " + PORT));

Dockerfile:

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

Build and run:

docker build -t my-app .
docker run -d -p 3000:3000 --name my-container my-app
curl http://localhost:3000

.dockerignore:

node_modules
npm-debug.log
.git
.DS_Store

6. Docker Compose

For multiple services, create a docker-compose.yml:

version: "3.8"
services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
    depends_on:
      - db
  db:
    image: postgres:13
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    volumes:
      - postgres_data:/var/lib/postgresql/data
volumes:
  postgres_data:

Run:

docker compose up -d
docker compose down

7. Best practices

  • Prefer official images.
  • Use .dockerignore to reduce image size.
  • Combine layers where possible.
  • Use multi-stage builds for larger apps.
  • Avoid running as root.
  • Add health checks.

Next steps

Explore image optimization, CI/CD with Docker, and orchestration with Docker Swarm or Kubernetes.