Hikmat
Hikmat

Reputation: 460

How to pass crontab command when running docker

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.

Upvotes: 5

Views: 756

Answers (3)

grizzthedj
grizzthedj

Reputation: 7505

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 -"

Upvotes: 2

Ravi
Ravi

Reputation: 982

docker run -it image /bin/bash -c "crontab -l | /bin/cat; /bin/echo \"*/5 * * * * <some command>\" |crontab - ; service cron restart"

Upvotes: 3

Abe
Abe

Reputation: 1415

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.

Upvotes: 0

Related Questions