Nginx Reverse Proxy Manager on Raspberry Pi 4 doesn't automatically start up after reboot

Viewed 777

I set up the Nginx Reverse Proxy Manager by Jamie Curnow on my Raspberry Pi using docker and docker-compose.

This is the yml stack file:

---
version: '3'
services:
  app:
    image: 'jc21/nginx-proxy-manager:latest'
    ports:
      - '80:80' #HTTP Traffic
      - '81:81' #Dashboard Port
      - '443:443' #HTTPS Traffic
    volumes:
      - ./config.json:/app/config/production.json
      - ./data:/data
      - ./letsencrypt:/etc/letsencrypt
  db:
    image: 'jc21/mariadb-aria:10.4'
    environment:
      MYSQL_ROOT_PASSWORD: 'npm'
      MYSQL_DATABASE: 'npm'
      MYSQL_USER: 'npm'
      MYSQL_PASSWORD: 'npm'
    volumes:
      - ./data/mysql:/var/lib/mysql

(I use different passwords)

Now, I already once composed it using sudo docker-compose up -d, however, whenever I sudo reboot my Raspberry Pi, it doesn't start as a service.

This is my Raspberry Pi Kernel, btw:

Linux Pi-Server 5.10.17-v7l+ #1403 SMP Mon Feb 22 11:33:35 GMT 2021 armv7l GNU/L

And this is my OS version:

Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 10 (buster)
Release:        10
Codename:       buster

The interesting part is that whenever I try to recompose it using sudo docker-compose up -d again, I get the following output:

Starting nginx_db_1  ... done
Starting nginx_app_1 ... done

Afterwards, both Nginx and its GUI by Mr. Curnow work.

Is there a more fancy way other than running this docker-compose on startup every time?

2 Answers

Host -- docker service level

Update: the initial answer works within docker itself. If you want docker itself to also start on boot, then that's a different story. You should be able to do that with:

sudo systemctl enable docker.service
sudo systemctl enable containerd.service

VM -- docker container level (nginx)

You can definitely start nginx service on boot.

The commands are self-explanatory -- enable, disable, and check status of running nginx on boot:

sudo systemctl enable nginx
sudo systemctl disable nginx
sudo systemctl status nginx

Resources:

  1. Launch Nginx on startup.
  2. systemd examples

If this is not available at your disposal, let me know and I'll try to find other ways and update my answer.

You can also edit the YML file to say "restart: always" in the "services:"

for example like this:

version: '3'

services:
  postgresql:
    image: postgres:9.6.6
    ports:
      - 9932:5432
    expose:
      - "5432"
    environment:
      - POSTGRES_PASSWORD=pass
    restart: always
    volumes:
      - /data:/var/lib/postgresql/data

  myapp:
    image: myapp
    links:
      - postgresql
    depends_on:
      - "postgresql"
    restart: always
    ports:
      - "5000:5000"

I would also recommend adding your specific docker-compose as a service using this service directory:

/etc/systemd/system/

In here simply create the file using nano or vim. The filename is not important, however, it needs to be a ".service" file, so I recommend the name "docker-compose-app.service"

[Unit]
Description=Docker Compose Application Service
Requires=docker.service
After=docker.service

[Service]
WorkingDirectory=/wherever/your/docker-compose/file/is
ExecStart=/usr/local/bin/docker-compose up
ExecStop=/usr/local/bin/docker-compose down
TimeoutStartSec=0
Restart=on-failure
StartLimitIntervalSec=60
StartLimitBurst=3

[Install]
WantedBy=multi-user.target

(WorkingDirectory = wherever your docker-compose file is, please change!)

All you then need to do is:

systemctl enable docker-compose-app

(If you've given it a different file name, use that name here)

And just to be sure:

sudo systemctl enable docker

This is a StackOverflow question which might help you answer your problem

How the restart: always policy works.

Related