Using Docker and Containerization on a VPS
Historically, installing applications on a VPS meant configuring the operating system, installing dependencies (like specific versions of Python, Node.js, or PHP), and hoping these dependencies didn’t conflict with other applications on the same server.
Docker revolutionized this process. It allows you to package an application and all its dependencies into a single, isolated, standardized unit called a container.
Why Use Docker on a VPS?
- Isolation without the Overhead: Unlike virtual machines, containers share the host’s OS kernel. This means you can run hundreds of containers on a single VPS with minimal performance overhead.
- Consistency (The “It Works on My Machine” Fix): A Docker container will run exactly the same way on your laptop, on a staging server, and on your production VPS.
- Easy Upgrades and Rollbacks: Updating an app is as simple as pulling a new container image and restarting. If something breaks, you can instantly revert to the previous image.
- Simplified Deployments: You don’t need to manually configure Nginx, database servers, and application runtimes. A
docker-compose.ymlfile can define and spin up your entire stack in seconds.
Installing Docker on Ubuntu/Debian
Avoid installing Docker from the default OS repositories, as they are often outdated. Use the official Docker repository:
# 1. Update packages and install prerequisites
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
# 2. Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# 3. Add the Docker repository
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
# 4. Install Docker Engine and Docker Compose
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y
(Optional) If you don’t want to type sudo every time you run a Docker command, add your user to the docker group:
sudo usermod -aG docker ${USER}
You will need to log out and log back in for this to take effect.
Introduction to Docker Compose
While docker run is great for single containers, modern applications are usually multi-container (e.g., a web frontend, an API backend, and a database).
Docker Compose is a tool that allows you to define these multi-container environments in a single YAML file.
Here is an example docker-compose.yml for a WordPress site with a MySQL database:
version: '3.8'
services:
db:
image: mysql:8.0
restart: always
environment:
MYSQL_ROOT_PASSWORD: root_password_here
MYSQL_DATABASE: wordpress
MYSQL_USER: wp_user
MYSQL_PASSWORD: wp_password
volumes:
- db_data:/var/lib/mysql
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wp_user
WORDPRESS_DB_PASSWORD: wp_password
WORDPRESS_DB_NAME: wordpress
volumes:
db_data:
To launch this entire environment, simply navigate to the directory containing the file and run:
docker compose up -d
The -d flag runs the containers in the background (detached mode).
By embracing Docker, you transform your VPS from a fragile environment requiring constant manual configuration into a robust, repeatable hosting platform.