Deploying a Docker on Digital Ocean Droplet

In this tutorial, I will walk you through deploying your Docker container to a Digital Ocean droplet. All commands will be manually run. You can automate this process if you would like but the purpose is for getting you up and running. My Docker container will be running a Springboot app on port 8080. I expect you to have some sort of Docker container with a basic web page (API, hello world, full web app, anything at the root).

Docker Release

We will need to release our Docker image to a centralized hub. This is where the droplet will eventually pull it from. I’m going to use Docker.com. They offer one free repository. There are other options like Amazon ECR, Github Repository, you can setup AWS S3 or Digital Ocean Spaces, etc. The overall goal is to get the Docker container onto the droplet.

Digital Ocean Homepage

Create your repository. Once you are all setup on the Docker.com page. You will need to create your Dockerfile in your actual project. My Dockerfile looks like this:

FROM openjdk:8u151-jre-alpine
MAINTAINER me@keithweaver.ca
WORKDIR /opt/app/
ADD target/yourapi-1.0.0.jar /opt/app/app.jar
RUN chmod +x /opt/app/app.jar
EXPOSE 8080

This runs my Springboot app and exposes port 8080. I can build it with the following command:

docker --version
mvn clean package # java only
docker build -t <docker_hub_user_id>/<docker_hub_repo> .
docker run -p 8080:8080/tcp <docker_hub_user_id>/<docker_hub_repo>

This should run your Docker container locally. For doing a proper Docker release you need to sign into your account and issue the release command.

echo '<your_docker_hub_password>' | docker login -u '<your_docker_hub_user_id>' --password-stdin
docker build -t <docker_hub_user_id>/<docker_hub_repo> .
docker push <docker_hub_user_id>/<docker_hub_repo>:latest

Your container should now be released. You can look it up on your Docker repository.

Deploying your Droplet

Now it’s time to deploy your Droplet. You can do this via the console, the API, or tools like Terraform. Start by signing in to your Digital Ocean account.

Digital Ocean Homepage

Digital Ocean Homepage

Digital Ocean Homepage

Digital Ocean Homepage

Digital Ocean Homepage

Or you can do this via API with curl:

curl
 -X POST
 -H "Content-Type: application/json"
 -H "Authorization: Bearer your_Digital_Ocean_API>"
 -d '{"name": "your_Droplet_name>", "region":"tor1", "size":"s-1vcpu-1gb", "image": "ubuntu-16-04-x64", "ssh_keys":[], "backups":false, "ipv6":true, "user_data":null, "private_networking":null, "volumes": null, "tags":[ "web"]}' "https://api.digitalocean.com/v2/droplets"

This will create your new Droplet. Either you will have setup SSH keys or you will be emailed credentials.

ssh root@<your_ip>
sudo apt-get update -y
sudo apt-get upgrade -y

sudo apt install docker.io -y
docker --version
echo "<your_docker_hub_password>" | docker login -u "<your_docker_hub_user_id>" --password-stdin

docker pull <docker_hub_user_id>/<docker_hub_repo>:latest
docker run -p 8080:8080/tcp --detach <docker_hub_user_id>/<docker_hub_repo>:latest

sudo apt-get install nginx -y
sudo rm -f /etc/nginx/sites-available/default
sudo vi /etc/nginx/sites-available/default

Sub in real values for all throughout script.

server {
 listen 80;
 location / {
   proxy_pass http://127.0.0.1:8080;
   proxy_redirect off;
 }
}
sudo systemctl reload nginx

Open your IP address in your web browser. You should see your page.

Adding a Domain

The next steps will be adding a domain. You will need to sign in to your account that manages your domains (Ex. GoDaddy) and find the DNS for that domain. You will want to create a type a record that has a host value of @ and it will point to your server ip.

Once, your domain updates - you should see the same message as the IP address in your web browser.

On your server, you’ll have to configure it to expect this domain. You will have to ssh back into the server.

sudo vi /etc/nginx/sites-available/default
server {
  listen 80;
  server_name your_domain.ca www.your_domain.ca;
  location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_redirect off;
  }
}

Update your domain where your_domain.ca is.

sudo systemctl reload nginx

Your domain should all be setup on a HTTP connection.

Side note: GoDaddy has an API and if you are automating this deployment process, try it out. The curl call looks like this:

curl -X PUT \
  https://api.godaddy.com/v1/domains/your_domain.ca/records/A/%40  \
  -H 'authorization: sso-key <your_api_key_part1>:<your_api_key_part2>'  \
  -H 'cache-control: no-cache'  \
  -H 'content-type: application/json' \
  -d "[
    {
      "data": "<your_ip>",
      "type": "A",
      "name": "Main Domain Record",
      "ttl": 600
    }
  ]"

Sub in your GoDaddy API key, your domain and your IP address.

Adding an SSL

The last step is setting up your server to accept traffic over HTTPS. Handling traffic over a secured traffic is highly recommended and really should be mandatory. You will not be able to do this if you do not have a domain.

The first step is to ssh into your server.

sudo apt-get update -y
sudo add-apt-repository ppa:certbot/certbot -y
sudo apt-get install certbot python-certbot-nginx -y
sudo certbot --nginx --agree-tos --non-interactive --text --rsa-key-size 4096 --email your@email.ca --webroot-path /var/www/html --domains "your_domain.ca, www.your_domain.ca"
sudo systemctl reload nginx

Sub in your domain where your_domain.ca and your email where your@email.ca.

If this is successful, it will say congratulations. For those who are adding pipelines or automated deployments, you can only run this 5 times a week.

Conclusion

You should have a fully setup Digital Ocean droplet. You will have access to this lesson for as long as it exists.