Skip to content
Advertisement

run multiple commands in docker after container start

how can I run /bin/run1.sh and /bin/run2.sh after the container startup! also, if you can tell me how can I send the logs of /bin/run1.sh and /bin/run2.sh to container logs!!

Docker file

FROM ruby:2.5
COPY run1.sh /bin
COPY run2.sh /bin
RUN chmod +x /bin/run1.sh
RUN chmod +x /bin/run2.sh
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
CMD ["rails", "server", "-b", "0.0.0.0"]

entrypoint.sh

#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

run1.sh

#!/bin/sh
echo `date` $@ >> /log.txt;
cat log.txt;

run2.sh

#!/bin/sh
echo `date` $@ >> /log2.txt;
cat log.txt;

Advertisement

Answer

You can change ENTRYPOINT ["entrypoint.sh"] to ENTRYPOINT ["entrypoint.sh", "run1.sh", "run2.sh"] which will run your custom bash scripts

you also need to create the log.txt file if it does not exist and to add exec "$@" to resume the container’s main process

so your runX.sh files should look like this

#!/bin/sh
touch log.txt;
echo `date` $@ >> log.txt;
cat log.txt;
exec "$@"

note that if you are running DB migration on rails they should be logged in your application log which can be accessed through the docker container itself (get the docker id from docker ps then access the bash docker exec -it /bin/bash where you can navigate to /logs/{your environment}.log)

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement