Setup a new server with Docker
After my previous posts, I wanted to setup a new server, using my new docker-compose setup and do it right this time.
SSH Keys
One of the security features I want to use, is to only allow login using SSH keys. Therefore I’m going to start generating some keys and then upload them to the server.
|
|
This will ask you where to save the keys. I use the default location of `~/.ssh/id_rsa`. Next we enter a passphrase, this could be omittet if we want the key to be all we need to login. I don’t mind the extra security, so enter some password to pair the key with. This password is needed everytime you login with the key.
The whole output looks like:
|
|
Now we copy they key to the server, this can be done using:
|
|
When you now login using `ssh root@<server-ip>` it will ask you for your passphrase which is the passphrase you used when generating the key.
Logging in to `root` I can disable login uisng root+password.
|
|
Find the line with `PermitRootLogin` and replace with
|
|
Finally restart the sshd service
|
|
Trying to login using `root` + `<root password>` will result in nothing, whereas using your ssh keys will allow you to login.
Setup Ubuntu
Updating
My server runs Docker on Ubuntu so before I start installing a bunch of things, let’s update it.
|
|
Now everything is up to date and we can create a new user to login with, so I don’t do everything using `root`.
New User
In order to create a new user we run
|
|
Now my regular user can use `sudo` so I don’t have to switch to `root` all the time.
Next, I want to avoid using `sudo docker <cmd>` and just be able to run docker commands without `sudo`. This can be done by writing
|
|
SSH keys
Right now we can use ssh keys to login to `root` but logging in to `<username>` does not require keys.
First copy the keys generated previously to the new user.
|
|
Login using `root` and ssh keys and edit `sshd_config`
|
|
Find these two lines and set them to no
|
|
Then save the file and restart the service
|
|
We are not forced to login to the server using the ssh keys and cannot login using passwords.
Docker-Compose
Installing docker-compose is pretty easy. First I made the mistake of using `apt-get install docker-compose`, which giSo starting over I went to the docker website and followed
|
|
Now the version is correct and I could continue without problems.
Folder structure
I like to keep a simple folder structure for my Docker containers. This is easy to keep track of local data and backups.
Docker
Let’s go through them one at a time and then see it all together. For Docker containers I don’t like using volumes to store data. I prefer to keep it in folders in my docker-folder.
|
|
Above is an example of three containers (traefik, portainer and wallabag) and their folder structure. It is pretty simple, since with one main folder for each container, inside the folder are the subfolders that the container uses and the docker-compose file.
From previous blog posts, I know that traefik has one folder called `config`, portainer have a `data` folder and wallabag has two folders called `data` and `images`.
Backup
For backups it is about the same structure, except there is another nested folder for each backup. The backups are named for the date and time.
|
|
Each blog posts about containers should have backup scripts for their containers.
Restore
Restore is the easiest. This is just temporary folders where I dump backups before restoring from them.
|
|
Combined
Combined the structure is as follows:
|
|
This might seem like a lot, but it makes everything a lot easier to work with compared to using volumes.
Containers
Let’s run through some containers docker-compose file, although more info can be found in their respective blog posts.
Traefik
First go to the traefik folder
|
|
In the docker-compose file, we write:
|
|
Here we setup all the needed information to use Traefik and Let’s Encrypt. Notice how in the `frontend.rule` i added `${DOMAIN}` instead of a my domain name. This is because I am using an environment file.
|
|
Write your domain name in the `.env` file.
|
|
We are not using the default network, so we have to create it before we can use it.
|
|
Lastly we need the `config.toml` file. This
#begin_src toml defaultEntryPoints = [“http”, “https”]
[entryPoints] [entryPoints.http] address = “:80” [entryPoints.https] address = “:443” [entryPoints.https.tls] [entryPoints.http.redirect] entryPoint = “https” [entryPoints.web] address = “:8080” [entryPoints.web.auth] [entryPoints.web.auth.basic] users = ["<username>:$apr1$9WtvIi9R$7UmmK6YEs0dDtLlM.1sbh."] [api] entryPoint = “web”
[acme] email = “xxx@xxx.com” entryPoint = “https” storage = “acme.json” onHostRule = true [acme.httpChallenge] entryPoint = “http”
[docker] endpoint = “unix:///var/run/docker.sock” domain = “domain.com” exposedByDefault = false #+end_src
Notice how we have a username and hashed password for our authentication. This is done using `htpasswd`
|
|
Finally start the container
|
|
Portainer
For Portainer, it’s a simple small container. Create the docker-compose file
|
|
Then copy the `.env` file
|
|
And we are ready to start the container.
|
|
Go to `portainer.<domain>.<ext>` and create an admin user. Then login and you are good to go.
Wallabag
|
|
Open the wallabag site and create a new user. Afterwards you can install the firefox extension or the iOS app if needed.
Cypht
|
|