DEV Community

Cover image for Setting Up a VPS with Docker: A Step-by-Step Guide
Ryan Machado
Ryan Machado

Posted on • Edited on

Setting Up a VPS with Docker: A Step-by-Step Guide

Running applications in the cloud gives you reliability, accessibility, and the power to scale. In this guide, I'll walk through setting up a Virtual Private Server (VPS) with Docker, using environment variables to make this guide reusable for your own projects.

Prerequisites

Before we begin, set these variables according to your project:

# Your server details export SERVER_IP="your-server-ip" export DOMAIN="your-domain.com" export PROJECT_NAME="your-project-name" # User credentials for deployment export DEPLOY_USER="deploy" export DEPLOY_EMAIL="${DEPLOY_USER}@${DOMAIN}" 
Enter fullscreen mode Exit fullscreen mode

🔑 Connecting to Your VPS

First, connect to your newly provisioned server:

ssh root@${SERVER_IP} 
Enter fullscreen mode Exit fullscreen mode

🔧 Installing Docker

After connecting to the server, install Docker:

sudo apt-get update sudo apt-get install ca-certificates curl sudo install -m 0755 -d /etc/apt/keyrings sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc sudo chmod a+r /etc/apt/keyrings/docker.asc echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ $(. /etc/os-release && echo \"${UBUNTU_CODENAME:-$VERSION_CODENAME}\") stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io -y 
Enter fullscreen mode Exit fullscreen mode

🌐 Configuring DNS

Create a DNS A record pointing your domain to the server IP. Once propagated, you can SSH using the domain:

ssh root@${DOMAIN} 
Enter fullscreen mode Exit fullscreen mode

🐳 Creating a Docker Context

To make working with remote Docker commands easier, create a Docker context:

docker context create ${PROJECT_NAME} --docker host=ssh://root@${DOMAIN} docker context use ${PROJECT_NAME} 
Enter fullscreen mode Exit fullscreen mode

Now all Docker commands will run on the VPS via this context.

🚀 Setting Up Docker Swarm

Initialize Docker Swarm on your VPS:

docker swarm init # Optionally add: --advertise-addr ${SERVER_IP} 
Enter fullscreen mode Exit fullscreen mode

The command will output a join token. Save this for adding worker nodes later if needed:

# Example output - your token will be different docker swarm join --token SWMTKN-1-xxxxxxxxxxxxxxxxxxxx ${SERVER_IP}:2377 
Enter fullscreen mode Exit fullscreen mode

📦 Deploying Your Application Stack

Deploy your application stack using docker-compose:

docker stack deploy -c docker-compose.yml ${PROJECT_NAME} 
Enter fullscreen mode Exit fullscreen mode

Your application should now be accessible at:

http://${DOMAIN} 
Enter fullscreen mode Exit fullscreen mode

To check service logs:

docker service logs ${PROJECT_NAME}_app 
Enter fullscreen mode Exit fullscreen mode

Conclusion

You now have a production-ready Docker environment on your VPS with a secure deployment user. This setup provides a solid foundation for running containerized applications in production with proper separation of concerns.

Top comments (1)

Collapse
 
tambytamby profile image
neo lithiq

Is this a use case for installing three docker-firefox and two docker-chromium? or is this more for programmers creating new things?