Skip to content

Self-Hosting: Containers, the Easy Way!

This blog is part of a series on self-hosting. I bought a server, and I’m setting it up. But I wonder: Am I doing things right?
By sharing, others can share their ideas, and together we can help people.

In the last blog post, we have set up Ubuntu Server with automatic updates, so we don’t have to do things. But now our server does nothing yet. I want many things on here. Home Automation, my own personal cloud, my own media server, everything! For this, there’s an amazing solution: Containers!

Why use Containers?

Imagine we’re moving in to our first apartment. Space is tight, so we only have the living room to put stuff. But we have many hobbies! Painting, reading, baking & crafts. So we’ll enter the room and throw everything in the first possible location!

Unfortunately, now I can never find where I left something, and if I realize I haven’t done a painting in a year, I can’t easily get rid of all my art supplies. I can remove most of it, but a year later I’ll find a random paintbrush in my bookshelf.

Instead, we’ll put things in storage bins. When we want our painting supplies, we can easily get the storage bin for painting supplies and if we want to get rid of it, we’ll just throw out the storage bin.

This is exactly what containers do. Each software is put in a container, and you can easily remove them later without any effects.

The Solution: Docker!

So, we want to use containers. I’ll spare you the details about choosing which way of running them. Most people use Docker, and this is also what I’m using in my daily job.

If you’re doing the same as me, and installed Ubuntu Server, then install using their apt repository using this guide. Make sure to also do the post-installation steps, so you don’t need to use sudo.

In the third step of the guide, you’ll run sudo docker run hello-world and you should see a nice hello world. If that was the case, you should be good to go!

Managing containers: Portainer

We don’t want to use the command line to manage our server. We want something to manage it, and that’s Portainer! It’s ‘Container Management Software’, which is exactly what we need. There’s a free & paid version, but the free one is more than enough for our home server.

I’ve installed it following this guide. This is installing the community edition for ‘Docker Standalone’ and Linux. There’s one change: Updating Portainer from within Portainer is only supported for the paid version. We’ll have a little trick up our sleeves, so at the step with the docker run command, change the version to latest like this:

docker run -d -p 8000:8000 -p 9443:9443 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:latest

Assuming you’re not doing this from the same computer, you’ll need to know the server’s IP to open Portainer. If you don’t know your current server IP address, you can run hostname -I to find it. You can then run the setup at https:// [IP] :9443. Replace [IP] with the server IP address.

Updating containers: Watchtower

Now we want to deploy our first container with Portainer, and I have the perfect one: Watchtower. Watchtower stops a container, updates it, and then restarts it again. Since we selected the ‘latest’ version for Portainer, Watchtower will also automatically update that!

A little warning: Watchtower is currently not being updated. It seems the maintainer ran out of time for this project. There’s also Diun, which will notify you of updates, so you can update all images except Portainer manually inside Portainer.

Let’s install Watchtower. When you open Portainer, you’ll see this screen:

This page is a list of ‘Environments’. We only have one, that’s “local”: the machine Portainer is on. On the left, you’ll find a lot of options. For us, only one matters: Containers. Click on Containers & then on the top-right, click ‘Add container’.

On the Create container screen, enter a name. I recommend keeping it simple. In this case, just watchtower will be perfect. For the image, use containrrr/watchtower.

Now we scroll down a bit, and in the advanced settings we go to Volumes.

In the Volumes, we need to enter some information according to the watchtower documentation. A volume can be compared to a symlink. It gives the container access to a location on the host. In this case, we give access to the socket of Docker, which will enable watchtower to sent commands to it.

Make sure to select “Bind” on the right. Enter /var/run/docker.sock in both container & host, and make sure “Writable” is selected.

As a last thing, I always go to Restart policy and select “Unless stopped” for each container. This prevents the container not starting again after a server reboot, but also prevents a stopped container from starting again after a server reboot.

Now we click the “Deploy the container” button. Docker will now fetch the image and run it. If you now go to Containers, you see it has appeared!

Next to “Running”, check out the little icons. The first 3 are Logs, Inspect and Stats. These are fun. They give you insight in the container. In logs, you can see Watchtower did its first check, but found nothing to update. In Stats, you can see statistics on memory, CPU and a few others.

Conclusion

That’s it! Our first container deployed, and it will keep all our containers, including Portainer, up to date! What do you think? What would you do differently? Let me know below or on Mastodon.

See you in the next blog, where we talk about networking.

Published inSelf Hosting

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *