Why choose Docker? A complete tutorial on installing Docker on a VPS.
1. Abstract
Have you ever had a headache while configuring environments on a VPS? Have you ever experienced that frustrating moment where "the code runs perfectly on my computer, but fails on the server"? If you are like me and have spent countless nights struggling with environment configurations, you will find Docker incredibly fascinating. It not only simplifies the deployment process but also significantly improves server resource utilization.
If you haven't tried Docker yet, this article is for you—from basics to hands-on practice, we will take a comprehensive look at Docker and help you smoothly install it on your VPS to make your development and deployment more efficient and stress-free. Whether you are a new webmaster, a freelance developer, or a seasoned IT professional, I believe you will benefit from this. Let’s dive into the world of Docker and see how it is revolutionizing server management and application deployment.
2. Why Use Docker?
Let's talk about why Docker is so popular. In my years of managing VPS instances, I have personally felt the revolutionary changes it brings. From environment configuration to resource management, Docker provides unprecedented convenience and efficiency.
2.1 Eliminate the "It Works on My Machine" Problem
Docker packages an application and all its dependencies into a standardized container. Whether you are running it in a development, testing, or production environment, the application behaves almost identically. You no longer have to worry about the classic excuse: "It worked on my laptop; I don't know why it's failing on yours."
2.2 Improve Resource Utilization and Save Costs
Traditionally, running multiple applications on a VPS required virtual machines to isolate environments. However, this leads to significant resource waste and performance degradation. Docker solves this via container technology. Containers share the host's OS kernel while providing isolated environments, allowing for highly efficient resource use. For example, I once used Docker to run over a dozen different services—including websites, databases, and caching apps—on a VPS with only 4GB of RAM, which would be unthinkable using traditional virtualization.
2.3 Effortless Deployment and Scaling
In the past, deploying an app involved a long list of complex steps: installing runtimes, configuring environments, and resolving dependency conflicts. This took hours. Even worse, horizontal scaling was often a nightmare. Docker changes everything. With just one simple command:
Bash
docker run -d -p 80:80 nginx
You can instantly deploy an Nginx server. When you need to scale, tools like Docker Swarm or Kubernetes help you achieve automated deployment and load balancing—a true lifesaver for tech professionals.
2.4 Security through Version Control and Rollbacks
In traditional setups, if a system upgrade fails, manual intervention is usually required, leading to high recovery costs. Docker packages every app and its environment into an Image. Since images are immutable, you have precise control over every version. If an issue arises, you can simply roll back to a previous version with ease.
2.5 Strong Isolation and Enhanced Security
Running multiple apps directly on a VPS means a vulnerability in one could compromise the entire system. Docker provides an additional layer of isolation through containers. Even if one container is breached, the other containers and the host remain secure, effectively preventing the spread of security incidents.
3. Steps to Install Docker on a VPS
Below are the step-by-step instructions for installing Docker and starting containers. The process varies slightly depending on your OS.
Installing Docker on Ubuntu
1. Update package lists:
Bash
sudo apt update
sudo apt upgrade -y
2. Install dependencies:
Bash
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
3. Add Docker’s official GPG key:
Bash
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
4. Add the Docker repository:
Bash
echo "deb [arch=amd64 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
5. Install Docker:
Bash
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
Installing Docker on CentOS
1. Install dependencies:
Bash
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
2. Add the repository:
Bash
sudo yum-config-manager --add-repo https://download.repo.com/linux/centos/docker-ce.repo
3. Install Docker:
Bash
sudo yum install -y docker-ce docker-ce-cli containerd.io
Post-Installation Configuration
1. Start and enable Docker:
Bash
sudo systemctl start docker
sudo systemctl enable docker
2. Verify the installation:
Bash
docker --version
docker run hello-world
3. (Optional) Allow the current user to run Docker without sudo:
Bash
sudo usermod -aG docker $USER
(Note: You must log out and back in for this to take effect.)
4. How to Overcome Network Restrictions?
When using Docker in certain regions (like Mainland China), you may face slow image download speeds or connectivity issues with Docker Hub. The most common solution is to use a Mirror Accelerator to significantly boost download speeds.
Recommended: Use 1Panel for Docker Management
I personally highly recommend using 1Panel to manage Docker because it offers:
One-click Installation: Set up Docker with just a few clicks.
Built-in Mirror Acceleration: 1Panel provides acceleration services that greatly improve pull speeds for local users.
Visual Management: Intuitively manage containers, view logs, and monitor status in real-time.
Excellent UI: A clean interface that makes operations much easier.
5. Summary
Through this tutorial, you should now understand how to install Docker on your VPS and the numerous benefits it brings. From optimizing resources and simplifying deployment to enhancing security, Docker is a game-changer. For VPS owners and developers like me, it makes our work more efficient and stable.
I believe Docker will play an increasingly vital role in future development and O&M. I hope this guide helps you kickstart your Docker journey. If you encounter any issues, feel free to leave a comment!