↓ Skip to main content
Self Hosting Part 4.1

A Ready Template for Docker Containers

A port plan for the containers and a shared NGINX proxy template with WebSocket support.

Series map 11/23
Self Hosting
  1. 0 Planning and Architecture
  2. 1 Post-Install Configuration
  3. 2 Mail Server Setup
    1. 2.1 Setting Up SSL for the Mail Server
    2. 2.2 Adding a New Domain to the Mail Server
    3. 2.3 Using a Mail Client
    4. 2.4 Moving the Mail Server
    5. 2.5 Extra Settings
  4. 3 NGINX Configuration
  5. 4 Docker Setup
    1. 4.1 A Ready Template for Docker Containers
  6. 5 Firewalld Configuration
  7. 6 Fail2Ban Configuration
  8. 7 WireGuard Setup
    1. 7.1 Adding WireGuard Clients
    2. 7.2 Putting Services Behind the VPN
    3. 7.3 Possible Problems
  9. 8 Vault Setup
  10. 9 Vaultwarden Setup
  11. 10 WordPress Setup
    1. 10.1 Moving an Old WordPress Site into This Setup
  12. 11 Usememos Setup
  13. 12 Syncthing Setup
On this page
Requirements

Assumptions

  • Container ports are published in the compose files as 127.0.0.1:<HOST_PORT>:<CONTAINER_PORT> and opened to the outside only through NGINX. The settings in 4 Docker Setup also bind ports published without an IP address to 127.0.0.1.
  • Commands are run on the server with root rights (sudo).

Let’s put the containers we’ll install into a standard layout.

Let’s keep the Docker Compose files in one place:

bash
sudo mkdir /opt/docker

Port Plan
#

The containers open their ports only on 127.0.0.1, and NGINX forwards to these ports. To avoid clashes, the ports of the services installed in this series are:

ServicePortNote
8 Vault Setup127.0.0.1:8200Not Docker, runs as a service on the host
9 Vaultwarden Setup127.0.0.1:8004
10 WordPress Setup127.0.0.1:8030–8049One port for each site
11 Usememos Setup127.0.0.1:8001
12 Syncthing Setup127.0.0.1:8384, 22000Not Docker, runs as a service on the host

You can check the ports in use with sudo ss -tlnp | grep docker-proxy.

NGINX Settings
#

WebSocket Support
#

First let’s check whether the $connection_upgrade variable is already defined. If the same map is defined twice, NGINX gives an error.

bash
sudo grep -rn connection_upgrade /etc/nginx/   # output should be empty

If the output is empty, let’s create /etc/nginx/conf.d/websocket.conf like this:

/etc/nginx/conf.d/websocket.confnginx
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

Proxy Template
#

Let’s create /etc/nginx/templates/proxy.tmpl like this:

/etc/nginx/templates/proxy.tmplnginx
proxy_http_version 1.1;

proxy_set_header Host              $host;
proxy_set_header X-Real-IP         $remote_addr;
proxy_set_header X-Forwarded-For   $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host  $host;

proxy_set_header Upgrade           $http_upgrade;
proxy_set_header Connection        $connection_upgrade;
Note

The basic configuration for a reverse proxy; we’ll use it in most of the virtual hosts that forward to containers.

Warning

proxy_set_header is inherited the same way as add_header: if even one proxy_set_header is defined in a location block, all the ones from the parent block stop working in that block. That’s why the safest way is to include the template in the same location block as proxy_pass.