Skip to main content



How to Deploy a Docker Container on a VPS

How to Deploy a Docker Container on a VPS: A Complete Guide

Docker VPS deployment has become one of the most popular methods for hosting modern web applications, APIs, and microservices. By combining the flexibility of a Virtual Private Server with the consistency and portability of Docker containers, developers and system administrators can build reliable, scalable infrastructure without the complexity of managing bare-metal servers. Whether you are running a personal project or a production-grade application, this guide will walk you through everything you need to know to get started.

What Is Docker and Why Use It on a VPS?

Docker is an open-source platform that allows you to package applications and their dependencies into lightweight, portable containers. Unlike traditional virtual machines, Docker containers share the host operating system’s kernel, making them significantly faster to start and far less resource-intensive.

A Virtual Private Server gives you dedicated resources within a shared physical server, providing root access, full control over your environment, and the ability to install any software you need. When you combine a VPS with Docker, you get a powerful deployment environment that is consistent across development, staging, and production. Your application behaves the same way regardless of where it is running, which eliminates the notorious “it works on my machine” problem.

For developers looking to streamline their deployment workflows, Docker VPS deployment offers several key advantages:

  • Rapid deployment and rollback capabilities
  • Isolation between applications running on the same server
  • Simplified dependency management
  • Easy horizontal scaling
  • Consistent environments across all stages of development

Prerequisites Before You Begin

Before diving into the deployment process, make sure you have the following in place:

  • A VPS running Ubuntu 22.04 or a similar Debian-based Linux distribution
  • Root or sudo access to the server
  • A basic understanding of the Linux command line
  • SSH access configured on your local machine
  • A Docker image ready to deploy, either from Docker Hub or a private registry

If you are new to managing VPS environments, you may find it helpful to explore resources on DA-Manager’s blog, which covers a range of server administration topics for beginners and experienced users alike.

Step 1: Connect to Your VPS via SSH

The first step in any Docker VPS deployment is to connect to your server. Open your terminal and use the following command, replacing the placeholder values with your actual server details:

ssh username@your-server-ip-address

Once connected, it is good practice to update your system packages before installing any new software:

sudo apt update && sudo apt upgrade -y

Step 2: Install Docker on Your VPS

Ubuntu’s default package repositories may not always contain the latest version of Docker. It is recommended to install Docker directly from the official Docker repository to ensure you have the most up-to-date release.

Install Required Dependencies

Start by installing the packages needed to allow apt to use a repository over HTTPS:

sudo apt install apt-transport-https ca-certificates curl software-properties-common -y

Add the Docker GPG Key and Repository

Next, add Docker’s official GPG key and set up the stable repository:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

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

Install Docker Engine

Update your package list and install Docker:

sudo apt update && sudo apt install docker-ce docker-ce-cli containerd.io -y

Verify the installation was successful by checking the Docker version:

docker --version

To avoid having to use sudo with every Docker command, add your user to the Docker group:

sudo usermod -aG docker $USER

Log out and back in again for this change to take effect.

Step 3: Pull Your Docker Image

With Docker installed, you can now pull the image you wish to deploy. If you are using a public image from Docker Hub, the command is straightforward:

docker pull nginx:latest

If you are working with a private registry, you will need to log in first:

docker login registry.yourdomain.com

Then pull your image as usual using the full registry path.

Step 4: Run Your Docker Container

Once your image has been pulled, you can run it as a container. The following example runs an Nginx web server and maps port 80 on the host to port 80 inside the container:

docker run -d --name my-nginx -p 80:80 nginx:latest

Here is a breakdown of the flags used:

  • -d — Runs the container in detached mode (in the background)
  • –name — Assigns a human-readable name to the container
  • -p — Maps a host port to a container port

To confirm your container is running, use:

docker ps

Step 5: Manage Container Persistence and Restarts

One of the most important considerations for Docker VPS deployment in a production environment is ensuring your container restarts automatically if the server reboots or the container crashes. You can achieve this by adding a restart policy when running the container:

docker run -d --name my-nginx --restart unless-stopped -p 80:80 nginx:latest

The unless-stopped policy ensures the container restarts automatically unless you explicitly stop it yourself.

Using Volumes for Persistent Data

If your application writes data to the filesystem, you should use Docker volumes to persist that data beyond the container’s lifecycle:

docker run -d --name my-app --restart unless-stopped -v /home/user/app-data:/app/data -p 8080:8080 my-app-image

This maps a directory on your VPS to a directory inside the container, ensuring data is not lost when the container is stopped or replaced.

Step 6: Use Docker Compose for Multi-Container Applications

For more complex applications that require multiple services — such as a web application alongside a database and a caching layer — Docker Compose is the recommended tool. It allows you to define all your services in a single docker-compose.yml file and manage them together.

Install Docker Compose on your VPS:

sudo apt install docker-compose-plugin -y

A basic docker-compose.yml file might look like this:

version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
  database:
    image: postgres:15
    environment:
      POSTGRES_PASSWORD: securepassword

To start all services defined in the file, navigate to the directory containing the file and run:

docker compose up -d

Step 7: Secure Your Deployment

Security is a critical aspect of any Docker VPS deployment. Here are some essential steps to harden your setup:

Configure a Firewall

Use UFW (Uncomplicated Firewall) to restrict access to only the ports your application needs:

sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable

Set Up SSL with a Reverse Proxy

Never expose your application directly without SSL. Use Nginx or Caddy as a reverse proxy in front of your containers, and obtain a free SSL certificate via Let’s Encrypt. Caddy, in particular, handles SSL certificate issuance and renewal automatically, making it an excellent choice for Docker-based deployments.

Keep Images Updated

Regularly pull updated versions of your base images to ensure you have the latest security patches applied. Consider automating this process using tools such as Watchtower.

Final Thoughts

Docker