Deploying Docker Containers With DigitalOcean

DevOps
Published May 6, 2022 · 3 min read
Running different technologies has been made easier by using Docker, where you are not coupled to what the hosting server offer's you anymore, all you need is a place that runs Docker containers. Digital is a very good place to do so, with a very reasonable pricing.

Prerequisite

In this article, we assume that you already have a docker image(s) that runs locally on your machine. In this article, we will be running it on a public server that is accessible from the browser.

DigitalOcean

DigitialOcean is a very straightforward and easy-to-use hosting server, with very reasonable prices. What we will be doing is basically renting a virtual machine on their servers, where we will have root access to its shell. Then we will open an SSH connection to it, copy our dumps, and run our container from the command line.

Setting up your account

  1. First, you need to register at their platform. You can do it from here.
  2. Then, you will need to create a new project from the left sidebar.
  3. After you have your project set up, you then need to create a droplet. It is DigitalOcean's notion of a virtual server. For the choice of OS, I went with Ubuntu, with the basic plan ($5/month as of writing this article).
    1. During the creation of the droplet, you'll be asked to generate an SSH key. Do so, by following the steps shown in the popup window, as this will be your way in from your local machine to the remote one.

After setting up your droplet, you'll be able to see it as follows:

The IP address that you see is the public address that you can use to access the server from the browser. For now, it is still not bound to any entry point.

Adding you domain

If you have an available domain name that you would like to bind to the above IP address, you can click on the ⋯ on the right of the droplet > Add a domain.

You need to add the domain name in the portal, then go to your domain name provider (e.g. Namecheap) and add the following nameservers in the domain's DNS configurations:

  • ns1.digitalocean.com.
  • ns2.digitalocean.com.
  • ns3.digitalocean.com.

This configuration redirects the domain to DigitalOcean's DNS servers.

Connecting to the shell

Now that you have your droplet set and ready, you need to connect to its shell to do all the magic. You can do so by either opening an SSH connection from your local machine (the one you used to generate the SSH key earlier) to the remote machine, or by directly opening a console from the browser by clicking on the ⋯ on the right of the droplet > Access console.

Open SSH connection

However, if you wanna do it in a more professional way:

  1. Open a UNIX shell on your local machine
  2. Enter the following command:
ssh root@{droplet_ip_address}

You will be asked to enter the passphrase you used when generating the SSH key. This proves that you are not trying to compromise someone else's identity.

Setting up Docker

Now that you have access to the shell, it is a computer by itself, so you must treat it as one. You may want to install some dependencies related to Docker and Docker Compose based on your need.

Then copy your docker-compose.yml to the machine and run the command:

docker-compose up -d

or you may want to run the docker container directly from the Docker CLI.

Container Registry

You may need a place to store your Docker images and pulling from. Consider Docker Registry as it is a free platform just for this.