0

I have docker container starting with command:

"CMD [\"/bin/bash\", \"/usr/bin/gen_new_key.sh\"]"

script looks like:

#!/bin/bash
/usr/bin/generate_signing_key -k xxxx -r eu-west-2 > /usr/local/nginx/s3_signature_key.txt

{ read -r val1
  read -r val2
  sed -i "s!AWS_SIGNING_KEY!'$val1'!;
          s!AWS_KEY_SCOPE!'$val2'!;
  " /etc/nginx/nginx.conf
} < /usr/local/nginx/s3_signature_key.txt

if [ -z "$(pgrep nginx)" ]
  then
     nginx -c /etc/nginx/nginx.conf
  else
     nginx -s reload
fi

Script is working itself as I can see all data in docker layer in /var/lib/docker.. It is intended to run it by cron for every 5 days as AWS signature key generated in first line is valid for 7 days only. How can I prevent Docker to quit after script is finished and keep is running?

DisplayName
  • 479
  • 2
  • 7
  • 20
  • Possible duplicate of [Docker container will automatically stop after "docker run -d"](https://stackoverflow.com/questions/30209776/docker-container-will-automatically-stop-after-docker-run-d) – andolsi zied Jan 30 '19 at 11:36

1 Answers1

0

You want a container that is always ON with nginx, and run that script every 5 days.

First you can just run nginx using:

CMD ["nginx", "-g", "daemon off;"]

This way, the container is always ON with nginx running.

Then, just run your script as a usual script with the cron:

chmod +x script.sh
0 0 */5 * * script.sh

EDIT: since the script must be running in the first time

1) one solution (the pretty one), it's to load manually the AWS valid signing key the first time. After that first time, the script will update the AWS valid signing key automatically. (using the solution previously presented)

2) the other solution, it's to run a docker entrypoint file (that it's your script)

# Your script
COPY docker-entrypoint.sh /usr/local/bin/
RUN ["chmod", "+x", "/usr/local/bin/docker-entrypoint.sh"]

ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]

# Define default command.
CMD ["/bin/bash"]

On your script:

service nginx start
echo "Nginx is running."

#This line will prevent the container from turning off
exec "$@";

+ info about reason and solution to use the exec line

andre-lx
  • 312
  • 3
  • 14