When I first started off with running my own server, I was still installing all my services with the Ubuntu package manager and enabling them with systemctl enable. After doing that for a couple of months I got introduced to containerisation and Docker through my work and soon migrated my services to Rancher 1.6 (which has been superseeded by Rancher 2 and is now in maintainance mode).

To run Rancher persistently and not have to rely on docker to keep it running here are the Systemd unit files I use to run both the Rancher db and server.

systemd

# /etc/systemd/system/rancher-db.service
[Unit]
Description=Rancher Mysql Server
After=docker.service
Requires=docker.service

[Service]
TimeoutStartSec=0
Restart=always
ExecStartPre=-/usr/bin/docker rm -f rancher-db
ExecStartPre=/usr/bin/docker pull mysql:5.7
ExecStart=/usr/bin/docker run --name rancher-db \
  --rm \
  -v /srv/rancher/mysql:/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD=secret \
  -e MYSQL_USER=cattle \
  -e MYSQL_PASSWORD=cattle \
  -e MYSQL_DATABASE=cattle \
  mysql:5.7
ExecStop=-/usr/bin/docker rm -f rancher-db
# /etc/systemd/system/rancher-server.service
[Unit]
Description=Rancher Server
After=rancher-db.service
Requires=rancher-db.service
[Service]
TimeoutStartSec=0
Restart=always
ExecStartPre=-/usr/bin/docker rm -f rancher-server
ExecStartPre=/usr/bin/docker pull rancher/server:stable
ExecStart=/usr/bin/docker run --name rancher-server \
  --rm \
  -p 8080:8080 \
  --link rancher-db \
  rancher/server:stable --db-host rancher-db --db-pass secret
ExecStop=-/usr/bin/docker rm -f rancher-server
[Install]
WantedBy=multi-user.target

Be sure to use a proper password for your db, or set up an isolated network for the db!


You could also just run it all in one blob: (Booo! Where is your microservice mindset?)

# /etc/systemd/system/rancher-server.service
[Unit]
Description=Rancher Server
After=docker.service
Requires=docker.service
[Service]
TimeoutStartSec=0
Restart=always
ExecStartPre=-/usr/bin/docker rm -f rancher-server
ExecStartPre=/usr/bin/docker pull rancher/server:stable
ExecStart=/usr/bin/docker run --name rancher-server \
  --rm \
  -p 8080:8080 \
  -v /srv/rancher/mysql:/var/lib/mysql \
  rancher/server:stable
[Install]
WantedBy=multi-user.target

docker-compose

You can of course, do all of this with docker-compose. Although with this config, the stack randomly died and didn’t come back automatically when a friend of mine was trying it out. At least for me the systemd version has yet to fail me.

# docker-compose.yaml
version: '3'
services:
  rancher-server:
    image: rancher/server:stable
    ports:
    - 8080:8080
    command: "--db-host rancher-db"
    networks:
      rancher-net: {}
  rancher-db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_USER: cattle
      MYSQL_PASSWORD: cattle
      MYSQL_DATABASE: cattle
    volumes:
    - /srv/rancher/mysql:/var/lib/mysql
    networks:
      rancher-net: {}
networks:
  rancher-net:
    external: false