Running Docker on your VPS

Step-by-step guide to installing Docker on a VPS, running your first container, managing volumes, and using Docker Compose to run multiple services simultaneously.

6 min Updated 26 Aug 2026

Why Docker on VPS is a Smart Choice?

If you have a virtual private server (VPS) and want to run your applications quickly, isolated, and portable, Docker on VPS is the best option. Docker allows you to package each application along with all its dependencies in a lightweight container. This means you no longer have to worry about library version conflicts, environment variable settings, or incompatibilities between services. In this article, we will walk through the complete path of setting up Docker on a VPS, from installation to running multi-service projects with Docker Compose.

Prerequisites and Server Preparation

Before you start, make sure your VPS meets these specifications:

  • Ubuntu 22.04 or 24.04 operating system (the commands in this article have been tested on these versions)
  • At least 1 GB of RAM and 10 GB of disk space (for lightweight projects)
  • SSH access with a user that has sudo privileges

First, log into your server and update the system:

sudo apt update && sudo apt upgrade -y

Installing Docker Prerequisites

A few helper packages are needed so that apt can use HTTPS repositories:

sudo apt install -y ca-certificates curl gnupg lsb-release

Installing Docker on VPS

To install Docker on a VPS, there are two main methods: using the official script or adding the Docker repository. We recommend the official repository method because subsequent updates are easier.

Adding the Official Docker Repository

Add the Docker GPG key:

sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Then add the repository to the sources list:

echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Installing Docker Engine and Plugins

sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

After installation, enable and start the Docker service:

sudo systemctl enable docker
sudo systemctl start docker

Verifying Installation and Adding User to the docker Group

To use Docker commands without sudo, add your user to the docker group:

sudo usermod -aG docker $USER

To apply the change, log out of the server and log back in. Then check the Docker version:

docker --version

The output should be something like Docker version 27.x.x.

Common mistake: If you still see a permission denied error after being added to the docker group, make sure to fully log out and log back in. Sometimes you may also need to restart the SSH service.

Running Your First Container on VPS

Now that Docker is installed, let's run our first container. The simplest test is running the hello-world image:

docker run hello-world

If you see a success message, Docker is working correctly on your VPS. Now for a more realistic example: running an Nginx web server:

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

This command:

  • -d means run in the background
  • -p 8080:80 maps port 80 of the container to port 8080 on the server
  • --name assigns a name to the container

Now open http://VPS_IP:8080 in your browser. You should see the default Nginx page.

Managing Containers

Here are some commonly used commands for managing containers:

docker ps                    # List running containers
docker ps -a                 # List all containers
docker stop my-nginx         # Stop a container
docker start my-nginx        # Start a container again
docker rm my-nginx           # Remove a container
docker logs my-nginx         # View logs

Managing Data with Docker Volumes

Containers are inherently stateless; any change to the container's filesystem is lost when the container is removed. To store persistent data (such as databases or uploaded files), you need to use volumes.

Creating and Using a Volume

Create a named volume:

docker volume create my-data

Now run a container with this volume:

docker run -d -p 3306:3306 -v my-data:/var/lib/mysql --name mysql-db -e MYSQL_ROOT_PASSWORD=secret mysql:8

In this example, MySQL data is stored in the my-data volume, and even if you remove the container, the data will persist.

Bind Mount for Direct File Access

Sometimes you want the container to have direct access to a specific folder on the server. This is done with a bind mount:

docker run -d -p 8080:80 -v /home/user/html:/usr/share/nginx/html --name web nginx

Now any file you place in /home/user/html will be immediately visible on the web server. This method is great for local development.

Common mistake: Don't forget that the bind mount path must already exist. If the destination folder doesn't exist, Docker will create it with root access, and you may encounter a permission error.

Running Multiple Services with Docker Compose

For real-world projects, you typically need multiple services running simultaneously; for example, a web application, a database, and a cache. Docker Compose simplifies this. With a single docker-compose.yml file, you can define all services and run them with one command.

Creating the docker-compose.yml File

Create a folder for your project and create the compose file:

mkdir ~/myapp && cd ~/myapp
nano docker-compose.yml

Enter the following content — a simple Node.js application with Redis:

version: '3.8'

services:
  app:
    image: node:18-alpine
    container_name: myapp
    working_dir: /app
    volumes:
      - ./app:/app
    ports:
      - "3000:3000"
    command: sh -c "npm install && npm start"
    environment:
      - REDIS_HOST=redis
    depends_on:
      - redis

  redis:
    image: redis:7-alpine
    container_name: myapp-redis
    volumes:
      - redis-data:/data

volumes:
  redis-data:

Running the Project with Compose

Now create the app folder and place a simple package.json and index.js file in it. Then run:

docker compose up -d

This command downloads and runs both services. To check the status:

docker compose ps

And to view logs:

docker compose logs -f

Commonly Used Compose Commands

docker compose down          # Stop and remove containers
docker compose down -v       # Remove containers and volumes
docker compose restart       # Restart all services
docker compose build         # Rebuild images

Common mistake: If you modify the compose file, make sure to run docker compose up -d again to apply the changes. The restart command only restarts containers and does not apply configuration changes.

Optimizing and Securing Docker on VPS

For professional use of Docker on a VPS, follow these security tips:

  • Always pull images from official and trusted sources
  • Use docker scan to check images for vulnerabilities
  • Place sensitive environment variables (such as database passwords) in a .env file and exclude it from your Git repository
  • Use the --memory and --cpus flags to limit resources for each container
docker run -d --memory="512m" --cpus="0.5" --name limited nginx

Conclusion

Setting up Docker on a VPS is not difficult, but it requires attention to detail. In this article, you learned how to install Docker, run your first container, manage data with volumes, and run multiple services simultaneously with Docker Compose. These skills are foundational for deploying modern applications on cloud infrastructure. If you're looking for a stable and high-speed VPS to run Docker, ServerNet's virtual server services can be a great choice. Now it's time to get your hands dirty and run your first container!

Was this page helpful?