A Step-by-Step Guide to set up a Server on a VPS with Ubuntu and Docker

This article provides a step-by-step guide on how to set up your own server on a VPS using Ubuntu and Docker. It walks through the process of connecting to your server, setting up the server with relevant config and installing Docker with docker compose.

Introduction

Setting up your own server on a VPS can be a daunting task, but with the right guidance, it can be a breeze. In this article, we will go through the steps required to set up your own server on a VPS using Ubuntu and Docker. Before we begin, there are a few prerequisites that you need to have a VPS (Virtual Private Server) available which you can purchase from a hosting provider such as DigitalOcean, Linode, or even OVH. When registering make sure you choose ubuntu as the OS, we will be using Ubuntu 20.04 LTS for this tutorial.

SSH client

Once setup your hosting provider will normally setup your VPS with an SSH client to connect to your VPS if you're using ubuntu it'll probably be using OpenSSH. Following the registration you'll probably receive instructions to connect to your VPS but'll probably be along the lines of you receiving an IP address and a password:

In case of openSSH all you have to do is open your SSH client and enter the IP address of your VPS, along with your username and password.

To connect to your server, log in as the root user by executing the command below (replace the highlighted portion of the command with the public IP address of your server) :

ssh root@your_server_ip
bash

If you encounter a warning regarding the host authenticity, proceed to accept it. For password authentication, provide your root password to gain access. In case you use an SSH key with a passphrase, you might need to enter the passphrase when using the key for the first time in each session. When logging in for the first time with a password, you may be prompted to modify the root password. Otherwise you can do it like that :

sudo passwd ubuntu
bash

Get a fresh start

sudo apt-get update
bash

The apt-get command is a package management tool in Debian-based systems that is used to install, upgrade, and remove packages from the system. The update option for apt-get command updates the local package list cache with the latest package information available from the repositories.

By running the sudo command with apt-get update, the command is executed with elevated privileges or superuser (root) privileges, which is required to perform some system-level tasks, such as updating package information and installing packages.

sudo apt-get -y upgrade
bash

The sudo apt-get -y upgrade command is used to upgrade the installed packages on a Debian-based Linux system. The -y option used with apt-get command automatically answers "Yes" to all prompts that may appear during the upgrade process, so that the upgrade process does not pause to ask for confirmation.

Install docker in a few steps

First off, we need to install some prerequisite packages that are required for Docker to run. To do this, run the following command:

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

These packages are commonly required to enable certain features or repositories on the system, such as:

  • apt-transport-https: This package allows the apt package manager to use HTTPS repositories, which provide secure and encrypted downloads of packages. HTTPS is recommended over HTTP for security reasons, especially when downloading packages from untrusted sources.
  • ca-certificates: This package contains the public SSL certificates that are used to verify the identity of HTTPS websites and repositories. Installing this package ensures that your system can trust the SSL certificates of the websites and repositories you are downloading packages from.
  • curl: This package provides a command-line tool for transferring data from and to servers using various protocols, including HTTP, HTTPS, FTP, and others. It is often used to test connectivity to servers, download files, or retrieve information from APIs.
  • software-properties-common: This package provides a collection of scripts and utilities that are used to manage software repositories on Debian-based systems. It allows you to add, remove, and configure repositories from the command line, as well as manage authentication keys for third-party repositories.

Then add the GPG key for the official Docker repository to your system:

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

Add the Docker repository to APT sources:

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
bash

Make sure you are about to install from the Docker repo instead of the default Ubuntu repo:

apt-cache policy docker-ce
bash

You should see an output similar like this :

Finally, install Docker:

sudo apt install docker-ce
bash

Docker should now be installed, the daemon started, and the process enabled to start on boot. Check that it's running:

sudo systemctl status docker
bash

Once Docker is installed, you can test it by running the following command:

sudo docker run hello-world
bash

This command will download a Docker image and run it in a container. If everything is working correctly, you should see a message that says "Hello from Docker!"

(optional) Execute docker without sudo

In order to run the docker command without having to use sudo or be in the docker group, you need to add your username to the docker group. By default, only the root user or a user in the docker group can run the docker command otherwise, you will get an error message stating that the Docker daemon cannot be connected to.

To add your username to the docker group, you can use the following command:

sudo usermod -aG docker ${USER}
bash

After running this command, you will need to log out of the server and log back in, or run the following command to apply the new group membership:

su - ${USER}
bash

You will be prompted to enter your user password to continue.

To confirm that your user has been added to the docker group, you can run the following command:

groups
bash

This will display a list of groups your user is a member of, including the docker group.

If you need to add a user to the docker group that you're not currently logged in as, you can declare that user explicitly using the following command:

sudo usermod -aG docker username
bash

It's important to note that the rest of the article assumes that you are running the docker command as a user in the docker group. If you choose not to add your user to the docker group, you will need to use sudo when running docker commands.

We will go onto more details about using docker here.

Install Docker Compose

Docker Compose is a tool that allows you to define and run multi-container Docker applications. Since Docker Engine 23, Compose ships as a built-in plugin — no separate install needed. Verify it's available:

docker compose version
bash

If it's missing (older installs), add it via apt:

sudo apt install docker-compose-plugin
bash

Once Docker Compose is installed, you can test it by creating a simple Docker Compose file. Create a new file called docker-compose.yml and add the following contents:

version: '3'
services:
  web:
    image: nginx
    ports:
      - "80:80"
yaml

This file defines a single service called "web" that uses the Nginx Docker image and maps port 80 on the container to port 80 on the host.

To start the service, run the following command:

sudo docker compose up -d
bash

This command will start the service in the background. You can then test it by opening a web browser and navigating to your VPS's IP address.

Conclusion

Setting up your own server on a VPS can be a challenging task, but with the right guidance, it can be a breeze. By following the steps outlined in this article, you can set up your own server on a VPS using Ubuntu and Docker. With your own server, you can host your own applications and websites, giving you complete control over your online presence.

Last updated: September 7, 2023

⚡ Who am i to talk about this? ⚡

Honestly i am no one, i've just been coding for 3 years now and i like to document every solutions to every problem i encounter. Extracting as much code snippets and tutorials i can so that if i ever need it again i just have to pop back here and get a quick refresher.

Feel free to me through this learning journey by providing any feedback and if you wanna support me: