Run Custom command within Container

Is there a way to have a custom command run within a container after the container is started? I am looking to have this done automatically.

Background, I am running a TrueCharts container and using the Gluetun addon. There is a directory within the container that I need the app to be able to access. By default, it doesn’t have read access to it and fails to read a file within. Using the shell for the container within the gui, I can manually run a command to change the permissions so it can view the files within. If there is a way to automate this process if the app restarts or anything, that would be ideal.

Probably depends on the version of Scale. The answer is yes, it is possible. The command will start with k3s kubectl exec and continue from there. I’ve given out commands to some that work for me on Cobia, but they have not always worked on newer Scale versions without some changes.

Here’s an example of a backup I run from cron on Scale host (not in the container) with my nextcloud container:

k3s kubectl exec -n ix-nextcloud -q "$(/usr/local/bin/k3s kubectl -n ix-nextcloud get pods --field-selector status.phase=Running -o name | cut -f2 -d'/')" -- /home/nextcloud/Scripts/NextcloudBackup.sh

You’ll want to read up on what each exec parm does in the man pages. It’s just an example and have no idea about your container.

You’ll note I use a subshell, this is to get Running pods as at times I’ve had non running pods so the command fails in this case.

I know I am able to run a command via k3s kubectl, but I am more of hoping to be able to utilize something within the gui itself to run the command upon container start or restart?

I could schedule a crown job to run the command I need, but I am hoping for a more elegant on container start only kind of solution.

Aha, so, you are looking for a postinit script, which is done in the GUI. System Settings → Advanced → Init/Shutdown Scripts section, click add. You’ll simply tell it to run your command or script, and when will be post init. You may need a sleep command as part of it for some time as your container would need to be up first. That is the closest you can get.

The post init script would work for entire system restarts, but if I update the container or stop and start it back up again, I would have to run the command manually.

I am hoping for something to run automatically every time a specific container is started or restarted.

Sorry, but there is no UI way to do that. An alternative is to make your own docker container and run it as a custom app, I don’t use any apps, all custom apps. In this way, you can modify anything you want.

Guess I will go with crown job for now.

I am currently using TrueCharts with gluetun. Not sure how to include a VPN solution in my own docker container for the app I need, so cronjob will just have to suffice for now.