Docker is a containerization platform that lets you package applications with their dependencies into lightweight, portable containers.
Prerequisites
- Operating system: Supported Linux distribution (Ubuntu 18.04+, Debian 9+, CentOS 7+)
- Privileges: Root access or sudo privileges
- Resources: Minimum 2GB RAM, 20GB of disk space
- Network: Active Internet connection to download images
Installing Docker
Step 1: Updating the system
Start by updating your system to make sure you have the latest package versions:
sudo apt update && sudo apt upgrade -y
Step 2: Installing dependencies
Install the packages needed to allow APT to use repositories over HTTPS:
sudo apt install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
Step 3: Adding the official GPG key
Add Docker's official GPG key to verify the authenticity of the packages:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Step 4: Configuring the Docker repository
Add the official Docker repository to your APT sources:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Step 5: Installing Docker Engine
Update the package index and install Docker Engine:
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
Step 6: Verifying the installation
Check that Docker is correctly installed and started:
sudo systemctl status docker
docker --version
If you see the Docker version displayed, the installation completed correctly!
Post-installation configuration
Running without sudo
By default, Docker requires root privileges. To allow your user to run Docker without sudo:
# Add your user to the docker group
sudo usermod -aG docker $USER
# Restart the session or run
newgrp docker
Adding a user to the docker group is equivalent to giving them root privileges. Use this feature with caution.
Configuring the Docker daemon
Create a configuration file to customize Docker's behavior:
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json > /dev/null <
Restart Docker to apply the configuration:
sudo systemctl restart docker
First test with Docker
Basic test
Test your installation with the hello-world image:
docker run hello-world
Running an interactive container
Launch an interactive Ubuntu container:
docker run -it ubuntu:latest bash
Managing Docker images
Searching for images
# Search for an image on Docker Hub
docker search nginx
# Download an image
docker pull nginx:latest
# List local images
docker images
Removing images
# Remove a specific image
docker rmi nginx:latest
# Remove unused images
docker image prune
# Remove all unused images
docker image prune -a
Managing containers
Basic commands
# Run a container in the background
docker run -d --name my-nginx nginx
# List active containers
docker ps
# List all containers
docker ps -a
# Stop a container
docker stop my-nginx
# Restart a container
docker start my-nginx
# Remove a container
docker rm my-nginx
Practical example: Nginx web server
# Run Nginx with port mapping
docker run -d \
--name web-server \
-p 8080:80 \
-v $(pwd)/html:/usr/share/nginx/html:ro \
nginx:latest
# Check that the server is working
curl http://localhost:8080
Building custom images
Basic Dockerfile
Create a Dockerfile to build your own image:
# Use a base image
FROM ubuntu:20.04
# Set the maintainer
LABEL maintainer="[email protected]"
# Update and install packages
RUN apt-get update && apt-get install -y \
nginx \
curl \
&& rm -rf /var/lib/apt/lists/*
# Copy configuration files
COPY nginx.conf /etc/nginx/nginx.conf
COPY index.html /var/www/html/
# Expose the port
EXPOSE 80
# Set the default command
CMD ["nginx", "-g", "daemon off;"]
Building the image
# Build the image
docker build -t my-server:1.0 .
# Run a container from your image
docker run -d -p 8080:80 --name my-app my-server:1.0
Docker Compose
Installing Docker Compose
Docker Compose is now included as a plugin. Verify the installation:
docker compose version
Example docker-compose.yml
version: "3.8"
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html:ro
restart: unless-stopped
database:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: secure_password
MYSQL_DATABASE: app_db
volumes:
- db_data:/var/lib/mysql
restart: unless-stopped
volumes:
db_data:
Using Docker Compose
# Start the services
docker compose up -d
# View the logs
docker compose logs
# Stop the services
docker compose down
# Stop and remove the volumes
docker compose down -v
Security best practices
Securing the Docker daemon
- Non-root users: Avoid running processes as root inside containers
- Official images: Favor official and verified images
- Updates: Keep Docker and your images up to date
- Limited resources: Limit container memory and CPU
Example of a secured container
docker run -d \
--name secured-app \
--user 1000:1000 \
--memory="512m" \
--cpus="0.5" \
--read-only \
--tmpfs /tmp \
nginx:latest
Security scan
# Scan an image for vulnerabilities
docker scout quickview nginx:latest
Monitoring and maintenance
Resource monitoring
# Real-time statistics
docker stats
# Docker system information
docker system df
# Automatic cleanup
docker system prune
# Full cleanup (be careful!)
docker system prune -a --volumes
Logs and debugging
# View a container's logs
docker logs my-container
# Follow the logs in real time
docker logs -f my-container
# Run a command inside a container
docker exec -it my-container bash
Use
docker system prune regularly to clean up unused resources and save disk space.
Conclusion
Docker is now installed and configured on your Linux system. You have the basic knowledge to:
- Manage Docker images and containers
- Create your own images with a Dockerfile
- Orchestrate multi-container applications with Docker Compose
- Apply security best practices
- Monitor and maintain your Docker environment
Docker revolutionizes application deployment by offering a portable, lightweight and consistent solution across all your development and production environments.
Comments