5

I want to run docker by docker run command and want to pass crontab command like bleow.

crontab -l | '{ /bin/cat; /bin/echo "*/5 * * * * <some command>"; }' | crontab -

The above command will create a cronscript which will run every 5 mins inside the new created docker container.

I dont need to supply this command when building image. *This docker will be created when job is scheduled.

Hikmat
  • 450
  • 4
  • 19
  • Docker containers are not full Linux VMs, so a typical docker container does not have a running `crond` to take your request as a VM could. You either need to include a `cron` install in your `Dockerfile` or use some one else's cron-ready container. You may also need to start `crond` which raises other issues about whether it is wise to run multiple processes in a docker container. Some examples and more warnings are in the answers to [How to Run a Cron Job Inside a Docker Container?](https://stackoverflow.com/questions/37458287/how-to-run-a-cron-job-inside-a-docker-container) – Paul Jan 10 '18 at 06:18

3 Answers3

3
docker run -it image /bin/bash -c "crontab -l | /bin/cat; /bin/echo \"*/5 * * * * <some command>\" |crontab - ; service cron restart"
Ravi
  • 912
  • 6
  • 12
  • 1
    +1 for using `bash -c` and for taking the time to properly escape quotes, as well as being the most correct answer to the question being asked – bluescores Jan 11 '18 at 18:50
2

Based on the docs, you can indeed specify a command with docker run.

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

One thing to note here as this will override any CMD instruction that is baked in the image, which may not be desired. If overriding is not an option, then you would have to EXEC into the container with docker exec -it <CONTAINER_NAME> bash, and run the command that way, as per Abe's answer.

So if the image you are running "does not" have a CMD instruction in the Dockerfile, you should be able to accomplish this with the following:

docker run -it some/image /bin/bash -c "crontab -l | '{ /bin/cat; /bin/echo \"*/5 * * * * <some command>\" }' | crontab -"

grizzthedj
  • 7,131
  • 16
  • 42
  • 62
0

You can get access to bash line command of your container and run the command, like this:

docker exec -it <my container> /bin/bash

and, you'll gain access to your container cli.

Abe
  • 1,357
  • 13
  • 31