Docker Community Forums

Share and learn in the Docker community.

How Would You Run a Startup Script Every Startup?


#1

I’ve spent a non trivial amount of time trying to figure out how to get my docker container to run a bash script on startup every startup. Basically I have a docker container that uses the mysql image as a base, runs a start script with entrypoint that in turn starts other scripts that check cpu usage and memory every couple of seconds and puts them into the mysql db. I have the container setup to restart if the host server restarts but the script that I’ve set with entrypoint only runs on initial startup. This means if I want to get the script running again I either have to run a docker exec command every time the server restarts or destroy the container and rebuild a new one that runs the script. I have a few ideas as to how to make it work and I would appreciate some input as to which option would be best or if another route would work better.

TL;DR Need startup script to build and put in data into mysql db periodically

So far the options I have come up with:

  1. Script that runs docker exec at system startup (in the host server not container)
  2. Script that rebuilds container on host startup
  3. Seperate Docker container for cpu and memory check scripts
  4. Figure out if CMD would work (I’ve tried but probably messed it up)
  5. Figure out if I can make my own service out of the script (never done this and pretty sure docker doesn’t use systemd)

What I’ve tried:

  1. Entrypoint works for the time being but I still have to rebuild container on system reboot
  2. CMD /src/start.sh runs the script and exits the container rather than starting mysql
  3. CMD /src/start.sh && tail -F /var/log that I got from this Stack Overflow post gets the container to not exit but doesn’t start mysql

Code in question

Any help would be appreciated as I’ve tried searching every combination of keywords related to this and have found very little (which leads me to believe this is the wrong way of doing things). I have no experience with docker compose and tend to ignore articles related to it out of the desire to not have to rework everything but if it comes to that I don’t mind learning it


#2

Create a Dockerfile, use the mysql image as the base image and apply your changes to it. Then build the customized image and use it to create your container…

Dockerfiles are very easy to write. You can copy files from your host to the image and run shell commands to taylor the image for you needs. Though, you need to know how to express what you want to do in shell commands.

If you never created a Dockerfile before, start to learn it. Otherwise you will end up using messy workarrounds all your docker life…