Running Services

We are not going to explain all about systemd, just what we are going to use to run our services as docker containers.

Why systemd?

  • concise "unit files"

  • process dependencies

  • unified toolset

How to use systemd?

Unit files

List unit files created by Debian.

sudo systemctl list-unit-files

Unit files have multiple sections. Unit section have description and dependencies, like dependency to network.target that needs to be executed first.

Service sections have type oneshot and it makes sure that action will be executed.

Install sections says another dependency, when this should be started.

[Unit]
Description=Restore iptables firewall rules
Before=network.target
Conflicts=shutdown.target

[Service]
Type=oneshot
ExecStart=/sbin/iptables-restore /var/lib/iptables/rules-save

[Install]
WantedBy=basic.target

Postgres service

Service sections can have multiple phases. %p will resolve to name of the service, which is name of the file, postgres.service. It says the service needs to be started after docker service is up. Also, it says the service needs to be always restarted, which will make the service running all the time.

Mobydock service (the example service)

Here is the file that will make sure our example service is running.

Redis service

Here is the Redis service, redis.service.

Put unit files on staging server

Lets see how we can put postgres.service on server and apply that.

First connect to the server and create the file. Then change owner.

Now, lets enable the services. Enable will make sure it is loaded at the system boot. Start will start it immediately.

Now we can try to play with the services. Lets try to restart redis service and see, that redis the docker container was just started.

Disable a service

We can disable the service.

Service status

We can find out the status of the service. This is useful when debugging the service.

Reloading services

When we update the service file, we want to reload the changes.

Service crash

What happens when the service crashes? Lets try it out. Lets kill Redis service. We can see that when we kill the service, is is started again, thanks to systemd.

Last updated

Was this helpful?