How to Manage Docker Containers on Debian 12 Bookworm System

How to Manage Docker Containers on Debian 12 Bookworm System

Docker is a powerful and popular containerization platform that allows you to create, deploy, and manage lightweight, portable, and self-sufficient containers. These containers can run virtually anywhere, making Docker an essential tool for modern DevOps workflows, application deployment, and microservices architecture.

Debian 12 Bookworm, known for its stability and security, is a great choice for running Docker containers. In this guide, we’ll walk through the steps to install Docker on Debian 12, create and manage containers, handle images, manage volumes and networks, and perform container lifecycle operations effectively.


1. Why Use Docker on Debian 12

Debian 12 Bookworm is a stable and robust operating system used widely in server environments. Running Docker on Debian offers:

  • Stability and reliability – Debian’s package ecosystem is mature and trusted.
  • Security – Debian maintains a solid security track record with timely updates.
  • Wide software support – Debian supports all necessary dependencies to run Docker effectively.
  • Lightweight – Ideal for containerized environments due to its low overhead.

2. Prerequisites

Before starting, ensure that:

  • You’re using a Debian 12 system with a non-root user having sudo privileges.
  • The system has access to the internet for downloading Docker packages.
  • The latest system updates are applied:
sudo apt update && sudo apt upgrade -y

3. Installing Docker on Debian 12

Step 1: Uninstall Old Versions

Remove older versions of Docker if they exist:

sudo apt remove docker docker-engine docker.io containerd runc

Step 2: Set Up Docker Repository

Install the required packages:

sudo apt install ca-certificates curl gnupg lsb-release -y

Add Docker’s official GPG key:

sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Set up the stable Docker repository:

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
  $(lsb_release -cs) stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Step 3: Install Docker Engine

sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y

Step 4: Enable and Start Docker

sudo systemctl enable docker
sudo systemctl start docker

Step 5: Verify Installation

sudo docker version
sudo docker run hello-world

Add your user to the docker group to run Docker without sudo:

sudo usermod -aG docker $USER
newgrp docker

4. Basic Docker Commands

Here are a few essential Docker commands:

CommandDescription
docker psList running containers
docker ps -aList all containers
docker imagesList local images
docker runRun a container
docker stopStop a container
docker startStart a stopped container
docker rmRemove a container
docker rmiRemove an image

5. Managing Docker Containers

Running a Container

docker run -d --name nginx-container -p 80:80 nginx
  • -d: Run in detached mode
  • --name: Assign a name
  • -p: Map host port to container port

Stopping and Starting Containers

docker stop nginx-container
docker start nginx-container

Restarting Containers

docker restart nginx-container

Removing Containers

docker rm nginx-container

To forcefully remove a running container:

docker rm -f container_id

6. Working with Docker Images

Pulling Images

docker pull ubuntu

Listing Images

docker images

Removing Images

docker rmi image_name

Building Your Own Image

Create a Dockerfile:

FROM ubuntu:latest
RUN apt update && apt install -y nginx
CMD ["nginx", "-g", "daemon off;"]

Build it:

docker build -t custom-nginx .

7. Volumes and Persistent Storage

Containers are ephemeral, so persistent storage is critical.

Creating and Using Volumes

docker volume create mydata
docker run -d -v mydata:/usr/share/nginx/html nginx

To inspect a volume:

docker volume inspect mydata

Bind Mounts

docker run -v /home/user/html:/usr/share/nginx/html nginx

8. Networking with Docker

Docker supports several network drivers:

  • bridge (default)
  • host
  • none
  • overlay (for Docker Swarm)

Listing Networks

docker network ls

Creating a Custom Bridge Network

docker network create mynetwork
docker run -d --name web --network mynetwork nginx

This allows containers to communicate using container names as hostnames.


9. Monitoring and Logging Containers

Checking Container Logs

docker logs container_name

Inspecting a Container

docker inspect container_name

Real-Time Resource Usage

docker stats

10. Security Best Practices

While Docker is powerful, it must be used securely.

  • Use Official Images: Stick to trusted sources to avoid vulnerabilities.
  • Limit Privileges: Avoid running containers as root unless absolutely necessary.
  • Keep Docker Updated: Use the latest stable version for security patches.
  • Use Docker Bench for Security: Run the benchmark tool to analyze configuration:
docker run -it --net host --pid host --userns host --cap-add audit_control \
  -e DOCKER_CONTENT_TRUST=1 \
  -v /etc:/etc:ro \
  -v /var/lib:/var/lib:ro \
  -v /var/run/docker.sock:/var/run/docker.sock:ro \
  --label docker_bench_security \
  docker/docker-bench-security

11. Conclusion

Managing Docker containers on a Debian 12 Bookworm system is a practical and efficient way to deploy modern applications. With its robust base and Docker’s powerful tooling, you can confidently build, deploy, and scale your applications using containers. From installing Docker to managing volumes, networks, and security, this guide covers the essential steps to start and maintain a reliable container infrastructure.

As you become more familiar with Docker, consider exploring advanced orchestration tools like Docker Compose or Kubernetes, especially for managing complex multi-container applications.