Part 1, Install Docker and Docker Compose
Here’s a step-by-step guide to install Docker and Docker Compose on Debian 12 Bookworm
Step 1: Update System Packages
Ensure your system packages are up to date before starting the installation.
sudo apt update && sudo apt upgrade -yStep 2: Install Required Dependencies
Install packages that allow apt to use HTTPS and required tools for adding Docker’s official repository.
sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg lsb-release -yStep 3: Add Docker’s Official GPG Key
You need to add Docker’s GPG key to verify the packages.
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpgStep 4: Set Up Docker Repository
Add Docker’s official repository to your system.
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/nullStep 5: Install Docker Engine
Update the package database and install Docker.
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io -yStep 6: Verify Docker Installation
Check if Docker is installed and running.
sudo systemctl status docker(Optional) Add your user to the docker group to avoid needing sudo for Docker commands:
sudo usermod -aG docker $USER
You’ll need to log out and log back in for this change to take effect, or apply
newgrp docker
Step 7: Install Docker Compose
Now install Docker Compose, which is a tool to define and run multi-container Docker applications.
sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-composeMake the binary executable:
sudo chmod +x /usr/local/bin/docker-composeVerify the installation:
docker-compose --version
Step 8: Enable Docker to Start on Boot
Enable the Docker service to automatically start on system boot.
sudo systemctl enable docker
Step 9: Test Docker and Docker Compose
You can run a simple Docker container to verify everything is set up properly:
docker run hello-world
For Docker Compose, create a simple YAML file (docker-compose.yml) and run it:
version: '3'
services:
hello-world:
image: hello-world
Run the following command:
docker-compose up
Part 1, Summary
Now, Docker and Docker Compose are installed on your Debian 12 system, ready to manage containerized applications!
This guide will fit well into your blog post as a detailed tutorial for preparing Debian 12 to run Docker containers.
Part 2, Running Containers with Docker
Running Docker containers is straightforward once Docker and Docker Compose are installed. Here’s a basic guide on how to run containers.
Step 1: Pull a Docker Image
Docker images are pre-configured applications that you can run in containers. You can search for and pull images from Docker Hub.
For example, to pull an official Nginx image, run:
docker pull nginx
Step 2: Run a Docker Container
Once the image is pulled, you can start a container. For example, to run an Nginx container:
docker run --name my-nginx -p 80:80 -d nginx
--name my-nginx: Assigns a name to the container.-p 80:80: Maps port 80 on the host to port 80 on the container.-d: Runs the container in detached mode (in the background).nginx: The name of the image.
You can now visit http://localhost in your browser, and you should see the Nginx welcome page.
Step 3: Check Running Containers
To see all running containers:
docker ps
To see all containers, including stopped ones:
docker ps -a
Step 4: Stop and Remove Containers
To stop a running container:
docker stop my-nginx
To remove a stopped container:
docker rm my-nginx
You can also stop and remove containers in one command:
docker stop my-nginx && docker rm my-nginx
Running Containers with Docker Compose
Docker Compose is useful for managing multi-container environments with a single configuration file.
Step 1: Create a docker-compose.yml File
Here’s an example for running WordPress and MySQL:
version: '3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
wordpress:
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: example
depends_on:
- db
volumes:
db_data:
Step 2: Start the Containers
Navigate to the directory containing your docker-compose.yml file and run:
docker-compose up -d
This command will pull the required images, create containers, and start them in detached mode.
Step 3: Check Running Containers with Docker Compose
To see running containers managed by Docker Compose:
docker-compose ps
Step 4: Stop and Remove Containers
To stop the running containers:
docker-compose down
This command stops and removes all containers defined in the docker-compose.yml file.
Step 5: View Logs
To view logs of your services:
docker-compose logs
You can specify a service to view only its logs, for example:
docker-compose logs wordpress
Part 2, Summary
With Docker, you can easily run single containers, while Docker Compose allows for managing multi-container applications. This should give you the foundation to start running containers on Debian 12 and add more advanced setups to your blog post later.