Install and run Docker on Ubuntu 18.04 at Infomaniak (or anywhere else)

Infomaniak is one of the best and reliable hosting provider in Switzerland. They are evolving fast, but unfortunately do not (yet?) provide a solution out of the box to host containers. If you want to to host your data in Switzerland and want to use docker to deploy your apps, that is nevertheless possible.

In this tutorial I will walk you through the different steps to get started and you will see that it is easy. First of all, we need an unmanaged cloud server. You can choose to install Unbunt 18.04 an generate an ssh key.

When this is done, you can connect to your server with the following command:

$ ssh -i [key path] [user]@[server]

The key path is the location on your disk where you saved the key you have created. The user is ubuntu because you have installed it and it is configured like this by Infomaniak. The server is the IP of your server that you can find in the manager console.

It is then dead simple to install docker:

$ sudo apt install docker.io

We have to make sure that docker restarts if we need to restart the server:

$ sudo systemctl start docker
$ sudo systemctl enable docker

Of course you can use the `disable` keyword to revert the previous command.

Check that everything is installed correctly with for example:

$ docker --version
>>> Docker version 17.12.1-ce, build 7390fc6

You might have an error at this stage telling you that you do not have permissions for that command. This is annoying and you do not want to type sudo this or sudo that all the time. So let’s add the current user to the docker group :

$ sudo usermod -a -G docker $USER

If you try right away, it will not work you need to logout and log back in again. Type exit and use the above ssh command and you will be able to run the docker commands without any problem.

Docker is nice, but `docker-compose` really helps out, so let’s install it as well, this is one command away:

$ sudo apt install docker-compose

Of course say, yes when prompted.

Now we have our environment and to save you the trouble of not being able to connect to your running containers through http, let’s open two ports:

$ sudo ufw allow 80
$ sudo ufw allow 443

It looks like we are done, but in fact if you check the size of the disk, you will be disappointed: 10G… We still have some work to do as this is only the system disk. We need to mount the data disk. Let’s do that.

Infomaniak provides you with two volumes a small system one /dev/vda and your data volume /dev/vdb. I simply formatted mine with the EXT4 file system:

$ sudo mkfs.ext4 /dev/vdb

I wanted my disk to be mounted on my /home directory. Like this the users folder works normally or as expected, but be aware, that if you just mount your disk, you will loose access to your hosting as the .ssh folder will not be found on your newly mounted disk.

Unfortunately, and here is experience speaking, you will have to reset your hosting. You will not loose data, as all the data that happens to be on your virtual disk b will be there, but you will have to reinstall your system, and this time, move your users files to your second volume

  1. Create a temporary folder to mount your disk
  2. Mount the volume
  3. Copy the data (with correct permissions)
  4. Unmount the volume
  5. Mount the disk to the /home directory
  6. Clean up
$ mkdir /mnt/home
$ mount /dev/vdb /mnt/home
$ rsync -rlptgoDHAX /home/ /mnt/home/
$ umount /mnt/home
$ mount /dev/vdb /home
$ rmdir /mnt/home

Just to have fun $ exit and try to login again ;) you should not have to reinstall anything.

That is it you can now play with your server and deploy your containers!

If you are interested I could tell you how to automatically direct traffic to your container, discover newly added containers and generate ssl certificates on the fly, install simple websites etc. just let me know in the comment section!