Hosting your site With Digital Ocean and Docker

Affiliate links:

Please be aware that the links in this article are affiliate links. If you click on them, I will gain some sort of commission. All money gained will help future blog posts.


Installing Docker on Digital Ocean

I want to share with you how I am hosting my website with Digital Ocean (hosting company) and Docker (places your app in containers).

First off, I created my droplet on DO and used the docker image from the list of “One-click Apps”. The droplet was the $10 a month size as I don’t currently need anything more. (If you want to try Docker for a month or two then click here).

Pull in an image

After going through the setup process, I accessed my server through SSH and downloaded the Ubuntu image.

docker pull ubuntu

Creating and setting up containers

Once I have downloaded the Ubuntu image, I created my own container. I know I could have pulled in the php-fpmcontainer (and I did), I couldn’t quite work out how I was supposed to get it working properly. So while this next part was really tedius, it has taught me a valuable lesson.

Onto the command for creating my container…

docker run -tid -p 80:80 -p 443:443 -v /home/project/public:/home/project/public --name CONTAINERNAME

Now my container is up and running and sharing my data with the server. I then mount the container by running…

docker attach CONTAINERNAME

Once in, I begin to install PHP 7, NGINX, PHP-FPM and everything else I need for hosting my site (the list is long as the Ubuntu image is barebones). I then add my server block info for http and https rules as well as subdomain redirects.

Detaching from Docker Containers

Once finished, I press…

ctrl+p ctrl+q

This will detach from your continer, but won’t stop any services. I would recommend you never type exit within the container if you want to keep it running along with it’s services.

Create a database container

Now we need a database container, I user MariaDB and followed the documentation from their Docker Hub page.

docker run -p 3306 --name some-mariadb -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mariadb:tag --character-set-server=utf8mb4 --collation-server=utf8m4bd

This will create a container for your MariaDB database, you can then attach yourself to the container and create your personal database using the usual MySQL commands.

Now quit your database and detach from the container with…

ctrl+p ctrl+q

Then run…

docker network inspect bridge

It will list your containers and display your IP address. You need to look out for your MariaDB container and take not of your IP address. You can then edit your database configuration file and where it says DB_HOST. Place your IP here and along with your username/password details, it should connect.

Make sure to either run any migrations or export/import your database. And your website is now fully up and running on Docker instances.

As I become more acquainted with the behemouth that is Docker, I will create more tutorials with regards to what it is is I wanted to accomplish and how I did it.

Leave a Reply