Configure Torizon OS to execute custom script before docker-compose.service is started

I need that a custom shell script is run (or a custom service is up and running) before Torizon OS launches docker-compose.service to process docker-compose.yml.
I found this old topic that describes more or less my situation.
I checked the up-to-date docker doc and if I’m right this is not possible from inside docker-compose.yml.
In that topic @jeremias.tx suggested to modify or override docker-compose.service, adding ExecStartPre field
https://github.com/torizon/meta-toradex-torizon/blob/kirkstone-6.x.y/recipes-containers/docker-compose-up/files/docker-compose.service

Is this possible without recompiling Torizon OS by sources?
Where is the file to be edited?

Hi, I’m the Toradex AI Assistant. The Toradex Support team will follow up with you shortly!

Did you like my answer?

:+1: :-1:


Yes, it is possible to configure Torizon OS to execute a custom script before the docker-compose.service is started without recompiling Torizon OS from sources. You can achieve this by creating a custom systemd service that includes the ExecStartPre directive.

Here are the steps to do this:

  1. Create a Custom Systemd Service File:
    You need to create a custom systemd service file that includes the ExecStartPre directive to run your custom script before starting the docker-compose.service.

  2. Edit the Custom Service File:
    You can create a new service file or override the existing docker-compose.service by creating a drop-in configuration file.

Creating a Drop-in Configuration File

  1. Create the Directory for the Drop-in File:

    sudo mkdir -p /etc/systemd/system/docker-compose.service.d
    
  2. Create the Drop-in Configuration File:
    Create a file named override.conf in the directory you just created:

    sudo nano /etc/systemd/system/docker-compose.service.d/override.conf
    
  3. Add the ExecStartPre Directive:
    Add the following content to the override.conf file:

    [Service]
    ExecStartPre=/path/to/your/custom-script.sh
    

    Replace /path/to/your/custom-script.sh with the actual path to your custom script.

  4. Reload Systemd Configuration:
    After creating the drop-in configuration file, reload the systemd configuration to apply the changes:

    sudo systemctl daemon-reload
    
  5. Restart the Docker Compose Service:
    Finally, restart the docker-compose.service to ensure it picks up the new configuration:

    sudo systemctl restart docker-compose.service
    

Example Custom Script

Make sure your custom script is executable:

sudo chmod +x /path/to/your/custom-script.sh

Summary

By following these steps, you can configure Torizon OS to execute a custom script before the docker-compose.service is started without the need to recompile Torizon OS from sources. The key is to use systemd’s drop-in configuration mechanism to add the ExecStartPre directive.

Please, also check if these links can help you solve your problem:

[TorizonCoreBuilder] Dropped capabilities while building image.

I’ll give it a try and I let you know.
If I need to wait for another service up and running, should I use

[Unit]
Requires=my-custom-service.service

in the override.conf file?

Greetings @vix,

You can either add the connection/dependency in docker-compose.service via an override (systemctl edit <service-name>). Or if you don’t want to override the base service you could probably create the connection in your new custom service. See the following systemd documentation for an idea: systemd.unit

Particularly the Before/After options. With this, in theory you could use Before in your custom service so that it’s configured to start before docker-compose.service starts. That way you wouldn’t have to touch/override the existing service.

I forgot to mention this prior, but it may not be ideal to modify docker-compose.service if you plan to update the OS. There is always a possibility that we update or change docker-compose.service ourselves and then you’ll need to account for this since you’re modifying/overriding the service yourself.

Best Regards,
Jeremias

I configured my service in this way

[Unit]
Description=My custom service
Before=docker-compose.service

[Service]
ExecStart=/usr/bin/bash /etc/my-custom-script.sh
Type=simple

As you can see, my service doesn’t have any requisite/dependency (After …)
It seems working, but I’m going to test it heavily.
My custom script runs a mkfifo command, so I think it requires that the file system is “ready”.
Should I specify this with some dependency/requirement?

My custom script runs a mkfifo command, so I think it requires that the file system is “ready”.
Should I specify this with some dependency/requirement?

Probably what you want to add is After=systemd-remount-fs.service to your service. The systemd-remout-fs.service is a special service. In summary this service ensures all mount options have been applied to the filesystem, this implies the filesystem is “ready”. So configuring your service to run after this, should ensure the root filesystem is in a “ready” state. For more info on this service you can see here: systemd-remount-fs.service

Best Regards,
Jeremias

Hello @vix,

Did your service work as expected?
Or do you need more support with this topic?

Best Regards,
Bruno

It works.
I hadn’t marked as a solution yet.
Sorry

1 Like