Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Docker Systemd Unit File
[Unit]
Description=PostgreSQL container
Requires=docker.service
After=docker.service
[Service]
Restart=on-failure
RestartSec=10
ExecStartPre=-/usr/bin/docker stop postgres
ExecStartPre=-/usr/bin/docker rm postgres
ExecStart=/usr/bin/docker run --name postgres \
--volume /srv/db/postgres:/var/lib/pgsql/data \
kiasaki/alpine-postgres
ExecStop=/usr/bin/docker stop postgres
[Install]
WantedBy=multi-user.target
@f4th4n

This comment has been minimized.

Copy link

commented Jun 8, 2018

I got error:

core@9742f9078a38 ~ $ journalctl --no-pager -u postgres -- Logs begin at Sun 2017-03-19 23:02:17 UTC, end at Fri 2018-06-08 05:28:51 UTC. -- Jun 08 05:28:23 9742f9078a38 systemd[1]: Starting PostgreSQL container... Jun 08 05:28:23 9742f9078a38 docker[1153]: Error response from daemon: No such container: postgres Jun 08 05:28:23 9742f9078a38 docker[1168]: Error response from daemon: No such container: postgres Jun 08 05:28:23 9742f9078a38 systemd[1]: Started PostgreSQL container.

@booch

This comment has been minimized.

Copy link

commented Jul 18, 2018

@f4th4n, the output you show says Started PostgreSQL container, so I think you're fine. The ExecStartPre lines start with -, which means to ignore errors. Apparently it's still printing those errors out. But those ExecStartPre lines are just stopping any existing Docker instances, to ensure that the Docker instance will start up correctly. You should be able to ignore the error messages in the logs.

I don't think you can add redirection to any of the Exec parameters. Otherwise I'd suggest adding >/dev/null 2>&1 to the ExecStartPre lines. Not sure if it's worth adding that; if so, it'd look like this:

ExecStartPre=-/bin/bash -c '/usr/bin/docker stop postgres >/dev/null 2>&1'
ExecStartPre=-/bin/bash -c '/usr/bin/docker rm postgres >/dev/null 2>&1'

But I'm honestly not even sure it's necessary to have those lines in there at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.