How to host a containerized website on DigitalOcean
Everyone who wants to host a super simple Docker containerized website for 5$/month- this might help you out.
- DigitalOcean - is a VPS (virtual private server) provider
- Docker - containerization framework
- Nginx - open source software for web serving (you can also take ghost or wordpress)
- Domain registrator (for example: Namecheap/GoDaddy)
- If you want to access your site via a custom domain name, go to Namecheap or Godaddy. There you can find a domain for already 1$ per year. Add all 3 nameservers from DigitalOcean to your domain on Namecheap GoDaddy, so they can do dns resolving. -If you don't want a domainname you can skip this step.
- Visit DigitalOcean and make yourself an account. (Tip: Keep your eyes open for a promo code I could save 10$ on my first deposit)
- Create a droplet, a Droplet is a virtual private server with allocated resources from DigitalOcean's datacenters all over the world. The configuration I took is: Ubuntu 20.4 LTS image, 1vCPU/1GB memory and 25GB SSD. My droplet is hosted in Amsterdam and I will access it through my SSH key. (this is the most secure way) This costs me 5$/month. Tip: you can create your SSH public & private keys with PuttyGen which comes with Putty.
- Access your droplet with SSH. I would recommend you Putty. But you will also have the possibility to use the online CLI once you've created the droplet. There you will also find the IPv4 address of your machine.
- Make sure you update your machine with
sudo apt-get updateand
sudo apt-get upgrade
- Install Docker, then create a webhost container. I will show you how to do it most basic with nginx. For ghost or wordpress follow this. But for nginx run:
docker run --name webhost -p 80:80 -d --restart=always -v <local directory with html files>:/usr/share/nginx/html nginx. This mounts your html path (wherever your index.html / style.css is stored) to the internal container nginx path. The container will always restart automatically, if you reboot your machine.
- Transfer your html files to your droplet and move them to directory which you specified in step 6. To transfer files I would recommed you Filezilla.
- Add your domain to your DigitalOcean account under "Networking>Domains". Then add an a record with hostname @ and direct it to the droplet.
-> You are now hosting your own website :D Now everything should work!
DigitalOcean offers a firewall to your droplet direct in the web interface. Go to "Networking>Firewalls" and create a new Firewall. Don't forget to open inbound http port 80 for webserving.
To turn on monitoring check the "Monitoring" checkbox when creating a new droplet. If you already created a droplet and want to turn it on follow this tutorial. Metrics charts can also be found in the webinterface of your droplet.
Docker image or Kubernetes
Of course you can also build your Kubernetes cluster on DigitalOcean or choose a Docker image directly on the marketplace. For my needs a common Ubuntu image is perfect - this really depends on what you're looking for.