Heena Shaikh
Heena Shaikh

Reputation: 1

How can I execute a docker container using shell script and also enter in that container and run a cron job?

I am trying to execute a docker container using shell script. One the container is up need to get inside the container using the same script and check the status of the cronjob running inside the container. All this needs to be done in one script

Upvotes: 0

Views: 134

Answers (1)

Adiii
Adiii

Reputation: 59966

One the container is up need to get inside the container using the same script and check the status of the cronjob running inside the container.

you do not need to check the status of cron process is running or not, you should design your docker image in way if the container is running its mean cronjob is running, cron should be the main process of the container, if cron process die container should die. For example

FROM alpine
RUN echo "* * * * * echo \"hello from cronjob\" " | crontab - 
CMD ["crond","-f"]

build the image

docker build -t my_cronjob_image

Now the script will be

#!/bin/bash
container_name=my-cront-container
image_name="my_cronjob_image"
if sudo docker ps -a --format '{{.Names}}' | grep -Eq "^${container_name}\$"; then
   echo "container is up and running, its mean job is also runinng"
   # but still you can see the process
   docker exec -it $container_name ash -c "ps"
   #or  list of cron
   docker exec  $container_name ash -c "crontab -e"

else
  echo 'container not running, starting container'
  docker run -dit --name $container_name $my_cronjob_image
fi

Upvotes: 1

Related Questions