This is going to be a quick and easy post while I'm working on a couple of projects, maybe related to this one.

We're going to assume that you already have podman-compose installed, and you have a folder somewhere with your compose file for use.

  1. Find your podman-compose
  2. Create a service file
  3. Reload systemd and Start your service
  4. Enable your service on boot

Find your podman-compose

First, we need to know where our podman-compose binary is, so we can put the exact location in our service file. By default, you can probably find it at /usr/bin/podman-compose, but if you've installed it to your home directory, it could be somewhere else. Either way, we can use which to find it. Take a note of this, because we'll need it later.

trevor@torracat:~$ which podman-compose
/home/trevor/.local/bin/podman-compose

Create a Service File

Next, we need to make a service file, so systemd will have all the info to start up our containers. If you're using Debian, those service files can be found in /etc/systemd/system, and can be named anything fitting the pattern *.service. We'll go ahead and make a file, and give it a name based on what we're wanting to start up. In this example, I'll be making a service for Mimic3, a text to speech system that I've been playing with.

trevor@torracat:~$ sudo nano /etc/systemd/system/mimic3.service

Now we'll need to fill in the service file. I've put a copy of mine below, but you'll probably need to change the Description, User, Group, and locations of the podman-compose binary, and your compose file.

[Unit]
Description=Mimic3
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
RemainAfterExit=true
User=trevor
Group=trevor
ExecStart=/home/trevor/.local/bin/podman-compose -f /home/trevor/containers/mimic3/compose.yaml up -d
ExecStop=/home/trevor/.local/bin/podman-compose -f /home/trevor/containers/mimic3/compose.yaml down

[Install]
WantedBy=multi-user.target

Reload systemd and Start Your Service

Now that we've got the service file created, we'll need to reload systemd, so that it'll be able to find the service file we just made.

trevor@torracat:~$ sudo systemctl daemon-reload

Then, we'll start the service, and make sure that there aren't any errors.

trevor@torracat:~$ sudo systemctl start mimic3.service

That one should take a moment, because it's trying to recreate all the containers in the background. But, eventually it should just finish without any errors. And, we can double check the status.

trevor@torracat:~/containers/mimic3$ sudo systemctl status mimic3.service
● mimic3.service - Mimic3
     Loaded: loaded (/etc/systemd/system/mimic3.service; disabled; preset: enabled)
     Active: active (exited) since Tue 2023-10-17 17:54:30 EDT; 6s ago
    Process: 2322154 ExecStart=/home/trevor/.local/bin/podman-compose -f /home/trevor/containers/mimic3/compose.yaml up -d (code=exited, status=0/SUCCESS)
   Main PID: 2322154 (code=exited, status=0/SUCCESS)
        CPU: 342ms

Oct 17 17:54:26 torracat podman-compose[2322154]: ['podman', '--version', '']
Oct 17 17:54:26 torracat podman-compose[2322154]: using podman version: 4.3.1
Oct 17 17:54:26 torracat podman-compose[2322154]: ** excluding:  set()
Oct 17 17:54:26 torracat podman-compose[2322154]: ['podman', 'ps', '--filter', 'label=io.podman.compose.project=mimic3', '-a', '--format', '{{ index .Labels >
Oct 17 17:54:26 torracat podman-compose[2322154]: ['podman', 'network', 'exists', 'mimic3_default']
Oct 17 17:54:26 torracat podman-compose[2322154]: podman run --name=mimic3_mimic3_1 -d --label io.podman.compose.config-hash=9629fe2a052b696ece7b3cad75d73105>
Oct 17 17:54:28 torracat podman-compose[2322154]: exit code: 125
Oct 17 17:54:29 torracat podman-compose[2322154]: podman start mimic3_mimic3_1
Oct 17 17:54:29 torracat podman-compose[2322154]: exit code: 0
Oct 17 17:54:30 torracat systemd[1]: Finished mimic3.service - Mimic3.

Enable Your Service on Boot

As long as the last exit code is 0, and we didn't get any errors, we can go ahead and enable our service at boot.

trevor@torracat:~/containers/mimic3$ sudo systemctl enable mimic3.service
Created symlink /etc/systemd/system/multi-user.target.wants/mimic3.service → /etc/systemd/system/mimic3.service.

And, that's it! We've now got our containers set to start automatically on boot!

References

https://blog.zedas.fr/posts/start-a-podman-container-at-system-boot/

Previous Post Next Post