Docker: Installation and Basic Usage
01
Docker Installation
Debian / Ubuntu (official method)
bash
# Remove previous versions if present
apt remove docker docker-engine docker.io containerd runc 2>/dev/null
# Install dependencies
apt update && apt install ca-certificates curl -y
# Add official Docker GPG key
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] \
https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" \
| tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install Docker
apt update && apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
# Enable on boot
systemctl enable docker
systemctl start docker
CentOS / AlmaLinux / Rocky Linux
bash
dnf install -y dnf-plugins-core
dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
dnf install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
systemctl enable --now docker
Verify installation
bash
docker --version
docker run hello-world
02
Basic Docker commands
Containers
bash
# Start a container
docker run -d --name my-nginx -p 80:80 nginx
# List active containers
docker ps
# List all containers (including stopped ones)
docker ps -a
# Stop a container
docker stop my-nginx
# Start a stopped container
docker start my-nginx
# Remove a container
docker rm my-nginx
# Logs of a container
docker logs my-nginx
docker logs -f my-nginx # follow in real time
# Access a running container
docker exec -it my-nginx bash
Images
bash
# Download an image
docker pull nginx
docker pull ubuntu:22.04
# List local images
docker images
# Remove an image
docker rmi nginx
# Search images on Docker Hub
docker search image-name
03
Docker Compose
Docker Compose allows you to manage multiple containers with a configuration file.
Example: Nginx + PHP-FPM + MySQL
Create the docker-compose.yml file:
yaml
version: '3.8'
services:
web:
image: nginx:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./html:/var/www/html
- ./nginx.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
php:
image: php:8.2-fpm
volumes:
- ./html:/var/www/html
db:
image: mariadb:10.11
environment:
MYSQL_ROOT_PASSWORD: secure_password
MYSQL_DATABASE: my_db
MYSQL_USER: user
MYSQL_PASSWORD: password
volumes:
- db_data:/var/lib/mysql
volumes:
db_data:
bash
# Start all services
docker compose up -d
# Stop all services
docker compose down
# See logs
docker compose logs -f
# Restart a specific service
docker compose restart web
# See status
docker compose ps
04
Docker space management
Docker can use a lot of disk space over time with unused images and layers:
bash
# Space used by Docker
docker system df
# Complete cleanup (removes stopped containers, unused images, build cache)
docker system prune -a
# Remove only unused volumes
docker volume prune
05
Allow a non-root user to use Docker
bash
usermod -aG docker username
# Exit and re-login for the group to apply
Adding a user to the docker group is equivalent to giving them root access to the system. Do this only for trusted users.
Verwandte Artikel
Software
Package Installation
How to install, update and remove software on your Linux server
2 Min. Lesezeit
Software
Web Server: Nginx
Installation and basic configuration of Nginx on your server
2 Min. Lesezeit
Software
Web Server: Apache
Installation and basic configuration of Apache on your server
2 Min. Lesezeit
