Docker

Docker is a platform that lets developers package applications and their dependencies into lightweight, portable containers. A container runs the same way on a laptop, staging server, or production host as long as Docker is available.

This page covers the most common Docker tasks: installing Docker, running and managing containers, building images, and using Docker Compose for multi-service applications.

Install Docker

The following commands install Docker Engine on a Fedora, CentOS, or RHEL based system.

sudo dnf install -y dnf-plugins-core
sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo dnf install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
sudo systemctl enable --now docker

If you want to run Docker without typing sudo every time, add your user to the docker group and log out and back in:

sudo usermod -aG docker $USER
newgrp docker

Verify the Installation

Check that Docker is working:

docker --version
docker info
docker run hello-world

Common Container Commands

Download an image from Docker Hub:

docker pull nginx:latest

Run a container in detached mode and map a port:

docker run -d --name web -p 8080:80 nginx:latest

List running containers:

docker ps

List all containers, including stopped ones:

docker ps -a

View container logs:

docker logs web

Open a shell inside a running container:

docker exec -it web /bin/sh

Stop and remove a container:

docker stop web
docker rm web

Build Images with a Dockerfile

Docker images are created from a Dockerfile. A Dockerfile describes the base image, files to copy, dependencies to install, and the command to run.

Example Dockerfile for a small Python application:

FROM python:3.12-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

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

Build the image:

docker build -t myapp:latest .

Run the image:

docker run --rm myapp:latest

Docker Compose

Docker Compose is useful when an application needs multiple services, such as a web app and a database.

Example compose.yaml file:

services:
  web:
    build: .
    ports:
      - "8000:8000"
    depends_on:
      - db

  db:
    image: postgres:16
    environment:
      POSTGRES_DB: appdb
      POSTGRES_USER: appuser
      POSTGRES_PASSWORD: secret
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:

Start the stack:

docker compose up -d

Stop the stack:

docker compose down

Volumes and Networks

Volumes store data outside the container lifecycle:

docker volume create app_data
docker run -v app_data:/var/lib/app myimage

Docker also creates networks so containers can communicate with each other:

docker network ls
docker network inspect bridge

Useful Maintenance Commands

Remove unused containers, networks, images, and build cache:

docker system prune

Show image history:

docker history myapp:latest

Inspect a container or image in JSON format:

docker inspect web

Best Practices

Keep images small by using minimal base images and cleaning up build artifacts.

Use .dockerignore to avoid copying unnecessary files into build contexts.

Prefer pinned image tags instead of latest for reproducible deployments.

Run one main process per container when possible.

Store persistent data in volumes instead of inside the container filesystem.