systemd service is not running on reboot

3.5k Views Asked by At

I've written a systemd script for starting up docker containers using dcoker-compose in ubuntu 16.04 LTS server.

Here is the script file /etc/systemd/system/swoop.mongo.service

[Unit]
Description=Swoop MongoDb Server container
Requires=docker.service
After=docker.service

[Service]
TimeoutStartSec=0

ExecStart=/usr/bin/docker-compose -f /var/www/swoop/docker-compose.yml up -d mongo3 mongo2 mongo1 mongosetup

Restart=always
RestartSec=10s
Type=notify
NotifyAccess=all

[Install]
WantedBy=local.target

When I start the service with sudo systemctl start swoop.mongo.service It works well. When I reboot the server, It gets failed and the status of the service is goes to inactive.

$ systemctl status swoop.app.service 
● swoop.app.service - Swoop App Server container
Loaded: loaded (/etc/systemd/system/swoop.app.service; disabled; vendor preset: enabled)
Active: inactive (dead)
1

There are 1 best solutions below

5
On BEST ANSWER

There is an option --restart=always for docker run command, If you don't have some special requirements in running containers I would recommend that.

Same concept applies to docker-compose:

restart: always

Policies:

  • no : Do not automatically restart the container when it exits. This is the default.
  • on-failure[:max-retries] : Restart only if the container exits with a non-zero exit status. Optionally, limit the number of restart retries the Docker daemon attempts.
  • always : Always restart the container regardless of the exit status. When you specify always, the Docker daemon will try to restart the container indefinitely. The container will also always start on daemon startup, regardless of the current state of the container.
  • unless-stopped : Always restart the container regardless of the exit status, but do not start it on daemon startup if the container has been put to a stopped state before.

Now as far as your systemd service goes, You need to enable that service so it automatically get started after reboots.

sudo systemctl enable <service name>

Update:

You will need to replace WantedBy=local.target to WantedBy=multi-user.target

multi-user.target: Multi-user, non-graphical. Users can usually login via multiple consoles or via the network.