Running LXC using systemd

Helllo,

I have created a lxc container using a rootfs.tar.bz2 and a metadata.yaml. Using the below command:

sudo lxc image import metadata.tar.gz rootfs.tar.bz2 --alias <conatainer_image_name>

I have launched it through terminal using below command:

lxc launch <image_name> <container_name>

I got the IP and all. Now I want to control this container image through systemd. (In other words I can say I want to run this container as a systemd service). I tried many unit files but nothing worked for me. Can anybody suggest me how can I proceed in this issue.

The service file I am using is as below:

[Unit]
Description=run_rungdpLXC Container

[Service]
Type=simple
User=root
TimeoutStartSec=0
ExecStartPre=-/usr/bin/lxc stop <container_name>
ExecStartPre=-/usr/bin/lxc delete <container_name>
ExecStart=/usr/bin/lxc launch <image_name> <container_name>
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

On crosschecking by “journalctl -u lxc-myimage.sercice” I am getiing below log:

Mar 25 20:19:16 docker-lxc systemd[1]: Starting run_runLXC Container...
Mar 25 20:19:19 docker-lxc systemd[1]: Started run_runLXC Container.
Mar 25 20:19:19 docker-lxc systemd[1]: lxc-image.service: Main process exited, code=exited, status=1/FAILURE
Mar 25 20:19:19 docker-lxc systemd[1]: lxc-image.service: Failed with result 'exit-code'.
Mar 25 20:19:24 docker-lxc systemd[1]: lxc-image.service: Scheduled restart job, restart counter is at 1.
Mar 25 20:19:24 docker-lxc systemd[1]: Stopped run_runLXC Container.
Mar 25 20:19:24 docker-lxc systemd[1]: Starting run_runLXC Container...
Mar 25 20:19:24 docker-lxc systemd[1]: Started run_runLXC Container.
Mar 25 20:19:25 docker-lxc systemd[1]: lxc-image.service: Main process exited, code=exited, status=1/FAILURE
Mar 25 20:19:25 docker-lxc systemd[1]: lxc-image.service: Failed with result 'exit-code'.
Mar 25 20:19:30 docker-lxc systemd[1]: lxc-image.service: Scheduled restart job, restart counter is at 2.
Mar 25 20:19:30 docker-lxc systemd[1]: Stopped run_runLXC Container.
Mar 25 20:19:30 docker-lxc systemd[1]: Starting run_runLXC Container...
Mar 25 20:19:30 docker-lxc systemd[1]: Started run_runLXC Container.
Mar 25 20:19:31 docker-lxc systemd[1]: lxc-image.service: Main process exited, code=exited, status=1/FAILURE
Mar 25 20:19:31 docker-lxc systemd[1]: lxc-image.service: Failed with result 'exit-code'.
Mar 25 20:19:36 docker-lxc systemd[1]: lxc-image.service: Scheduled restart job, restart counter is at 3.
Mar 25 20:19:36 docker-lxc systemd[1]: Stopped run_rungdpLXC Container.
Mar 25 20:19:36 docker-lxc systemd[1]: Starting run_rungdpLXC Container...
Mar 25 20:19:36 docker-lxc systemd[1]: Started run_rungdpLXC Container.
Mar 25 20:19:36 docker-lxc systemd[1]: lxc-image.service: Main process exited, code=exited, status=1/FAILURE
Mar 25 20:19:36 docker-lxc systemd[1]: lxc-image.service: Failed with result 'exit-code'.
Mar 25 20:19:41 docker-lxc systemd[1]: lxc-image.service: Scheduled restart job, restart counter is at 4.
Mar 25 20:19:41 docker-lxc systemd[1]: Stopped run_runLXC Container.
Mar 25 20:19:41 docker-lxc systemd[1]: Starting run_runLXC Container...
Mar 25 20:19:42 docker-lxc systemd[1]: Started run_runLXC Container.
Mar 25 20:19:42 docker-lxc systemd[1]: lxc-image.service: Main process exited, code=exited, status=1/FAILURE
Mar 25 20:19:42 docker-lxc systemd[1]: lxc-image.service: Failed with result 'exit-code'.
Mar 25 20:19:47 docker-lxc systemd[1]: lxc-image.service: Scheduled restart job, restart counter is at 5.
Mar 25 20:19:47 docker-lxc systemd[1]: Stopped run_runLXC Container.

Am I doing anything wrong here? Please guide me if I am missing something

Regards,
Siddhartha V

I guess you’d want to make sure to start that unit after LXD itself is started but that doesn’t really explain the current failure. It’s actually pretty unclear what’s failing there…

Hi,

Sorry for the late reply. It is resolved. I used an external script that contains "lxc start <container_name> to start the container and running it through systemd service file.

But now my understanding is if I give “systemctl stop <service_file>” then the container should stop. But here in my case container is still running state even after stop command. Is it the right behavior for containers?

Any pointers will help me a lot.

regards,
Siddhartha V

Dont you need an ExecStop with a script that runs lxc stop <container_name>, how else would systemd know how to shutdown the container?

lxc start isn’t long running, so killing it won’t stop it indeed.
You’d want to make the unit persist and then use a ExecStop to call stop.

Hello @turtle0x1,

Actually “ExecStart=/usr/bin/lxc launch <image_name> <container_name>” is not starting the container for me. I have a script in /etc which contains, "lxc start ". My service file would look like “ExecStart=/etc/mysript.sh” .

After your suggestion I added the “ExecStop=/usr/bin/lxc stop <container_name>” but it is not working for me. Any pointers on this please.

Hello @stgraber ,

Actually “ExecStart=/usr/bin/lxc launch <image_name> <container_name>” is not starting the container for me. I have a script in /etc which contains, "lxc start ". My service file would look like “ExecStart=/etc/mysript.sh” .

After your suggestion I added the “ExecStop=/usr/bin/lxc stop <container_name>” but it is not working for me. Any pointers on this please.

Also I didn’t get what exactly it is. Could you please elaborate this to me. I am truly a newbee to this container and systemd concepts sorry if I am asking really basic things here.

You could create another script I.E /etc/stopMyScript.sh (which contains lxc stop <container-name>)

And then in your systemd file you would have;

ExecStop=/etc/stopMyScript.sh

Which should call your script when you call systemctl stop <file>

Hello @turtle0x1 and @stgraber

I tried adding ExecStop but now container will not start. i.e ExecStart itself is not working. If I remove Execstop then again everything works normal. Here I am attaching my service file snap.

image

request your suggestion here please.

Im no systemd expert but the following worked, I think its probably the “Type” and I dont think you need the restart parameters;

create file /etc/systemd/system/my_container.service

In that file I added

[Unit]
Description=Stop / Start Container

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/snap/bin/lxc start <container_name>
ExecStop=/snap/bin/lxc stop <container_name>

[Install]
WantedBy=multi-user.target

I then did

sudo systemctl enable /etc/systemd/system/my_container.service
sudo systemctl start my_container
lxc list // verify the container is started
sudo systemctl stop my_container
lxc list // verify the container is stopped
1 Like

Hello @turtle0x1,

Great and thank you so much…!!!

It worked for me. It’s not only “Type” but also I have added that “RemainAfterExit=true” , then everything going well.

Thank you s much. :blush:

regards,
Siddhartha V