Run script when docker starts


Recommended Posts

I'd like to run a .sh script in the background when a docker image starts.

 

Some suggestions I've seen are using crontab @reboot or calling the script from rc.local. I've tried both without success but I'm not sure if I'm doing it right anyway.

 

Any suggestions would be appreciated.

 

Thanks.

 

unRaid 6.0

Link to comment

I'd rebuild the image using the Dockerfile to run your script at build time or modify the CMD to include your script somehow. Does that suit your needs?

I didn't build the image and have no idea how to :) So kinda stumped at this point. Prior to this I'd exec into the docker and start the script and leave it, but now the tower is headless so can't do it.

Link to comment

I'd rebuild the image using the Dockerfile to run your script at build time or modify the CMD to include your script somehow. Does that suit your needs?

I didn't build the image and have no idea how to :) So kinda stumped at this point. Prior to this I'd exec into the docker and start the script and leave it, but now the tower is headless so can't do it.

You could probably make a script and place it in the appropriate folder according to this: http://lime-technology.com/forum/index.php?topic=26201.0 to execute when the array is started (with an appropriate delay to let the container get up and running) that would issue the docker exec commands required.

 

(or do it manually through telnet - no reason why you can't do it simply because the system is headless)

Link to comment

I'd rebuild the image using the Dockerfile to run your script at build time or modify the CMD to include your script somehow. Does that suit your needs?

I didn't build the image and have no idea how to :) So kinda stumped at this point. Prior to this I'd exec into the docker and start the script and leave it, but now the tower is headless so can't do it.

 

Give us some more info then. What's the script you need to run? Which container etc....

Link to comment
  • 2 months later...

I'd like to run a .sh script in the background when a docker image starts.

 

Some suggestions I've seen are using crontab @reboot or calling the script from rc.local. I've tried both without success but I'm not sure if I'm doing it right anyway.

 

Any suggestions would be appreciated.

 

Thanks.

 

unRaid 6.0

 

This topic seems to have been dropped, but I had the same question and figured out one way.  The Docker "run" syntax includes a target script/argument

$ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

 

See https://docs.docker.com/reference/run/

 

I wanted to run a target script /my/script/lives/here/script.sh but I couldn't find where to put that in the UNRAID docker template.  I first tried to edit the container from the UNRAID Docker page and paste the path in the docker "extra parameters" field within UNRAID.  This works for --cpuset-cpus but adding the script path just deleted the container, though it was easy to restore.

 

Instead, I decided to do it through the CLI.  First, I restarted the container, and copied the command that was successfully loaded.  Then in terminal I ran

 

docker stop <container>
docker rm <container>

 

To stop and then remove the image and then pasted the run command from the UNRAID page and added <space>/my/script/lives/here/script.sh

 

This is literally the command (deleting port numbers):

/usr/bin/docker run -d --name="handbrake" --net="bridge" -e WIDTH="1280" -e HEIGHT="720" -e TZ="America/New_York" -p 8**2:8080/tcp -v "/mnt/user/Media/":"/media":rw -v "/mnt/user/Media/handbrake_watch/":"/input":rw -v "/mnt/cache/appdata/handbrake/":"/nobody/.config/ghb":rw -v "/mnt/user/Media/handbrake_out/":"/output":rw --cpuset-cpus="3" pinion/docker-handbrake-cli:latest nobody/.config/ghb/scripts/convert.sh

 

Executing it gave a running container that launched my script.

 

I wasn't sure if the cmd-script would be launched upon container restart, but it is.  Now when I start or restart the container the script is launched inside it.

 

The script directory is mapped in the container, so I can edit it in /mnt/cache/appdata/etc

 

The suggestion to edit the container works and I tried that in an earlier iteration, which worked after 'committing' the container.  However you will lose these edits if you update the image, which is surprisingly easy to do; it happened to me twice.  I like having an editable script launched with the container that can easily be rebuilt.

 

Hope yours works.

 

Dennis

 

  • Upvote 1
Link to comment
  • 4 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.