hikmat30ce
(Hikmat Ullah)
January 6, 2018, 1:14pm
1
Hi Experts,
I want to run docker by “docker run” command and want to pass crontab command like bleow.
crontab -l | ‘{ /bin/cat; /bin/echo "*/5 * * * * "; }’ | crontab -
The above command will create a cronscript which will run every 5 mins.
I have already done it in Dockerfile, but I need to use it with “docker run” command because I am creating docker from code base on creating schedules.
In short this docker will be created when job is scheduled.
-Hikmat
sdetweil
(Sam)
January 6, 2018, 1:17pm
2
you want the container to issue docker run commands? is that right?
if you mount the docker host socket, then you can use it inside the container to issue docker commands back thru docker daemon on the host to manage containers.
--mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock
1 Like
hikmat30ce
(Hikmat Ullah)
January 6, 2018, 1:20pm
3
No, I want to supply crontab command when I create docker container by “docker run” command
I you help in command line command, I will handle it in code.
thanks for your comment
sdetweil
(Sam)
January 6, 2018, 2:12pm
4
ok, when I try to execute this I get an error…
sam@buildserver:~/Downloads/test$ crontab -l | ‘{ /bin/cat; /bin/echo "*/5 * * * * "; }’ | crontab -
no crontab for sam
‘{: command not found
*/5 * * * *
}’: command not found
sdetweil
(Sam)
January 6, 2018, 2:31pm
5
ok, i see it now…
you could write a script to use as the entrypoint command in the dockerfile
the script would look like this
#!/bin/bash
cron_file=/tmp/cron_file
# check and start cron if not running
if [ `ps -ef | grep cron| wc -l` -eq 0 ]; then
cron &
fi
crontab -l > $cron_file
echo "$@" >> $cron_file
crontab $cron_file
sleep 100000000
1 Like
hikmat30ce
(Hikmat Ullah)
January 6, 2018, 2:34pm
6
hikmat30ce:
I have already done it in Dockerfile, but I need to use it with “docker run” command because I am creating docker from code base on creating schedules.
In short this docker will be created when job is scheduled.
I need in docker run
command.
Already have in Dockerfile
thanks
sdetweil
(Sam)
January 6, 2018, 2:45pm
7
then the docker run would be
docker run -d image_name '*/5 * * * * ___cmdstring___'
where cmdstring is the oneliner for the commands to be issued… (with quotes escaped as required)
my test Dockerfile is
from ubuntu
run apt-get update
run apt-get install cron ack-grep
add ./startup.sh .
entrypoint ["/startup.sh"]
1 Like
hikmat30ce
(Hikmat Ullah)
January 6, 2018, 3:00pm
8
When I run below command, cron job is not adding!
docker run -d hikmat30cd/schedulewebjob ‘*/5 * * * * /usr/bin/php /app/yii cronrun/runjob’
sdetweil
(Sam)
January 6, 2018, 3:03pm
9
AND u need the startup.sh entrypoint command to build the crontab file, right?
if you docker exec container_id cat /tmp/cron_file what do you see, also you can docker exec crontab -l to see if it was added…
had to fix the dockerfile to add grep to the image
1 Like
hikmat30ce
(Hikmat Ullah)
January 6, 2018, 3:25pm
10
sdetweil:
AND u need the startup.sh entrypoint command to build the crontab file, right?
if you docker exec container_id cat /tmp/cron_file what do you see, also you can docker exec crontab -l to see if it was added…
had to fix the dockerfile to add grep to the image
I got executed below command successfuly, now converting it to json object as I have to call it from code. Can you help me in that?
docker exec mydocker bash -c “echo ‘*/5 * * * * /usr/bin/php /app/yii cronrun/runjob’>> /var/spool/cron/crontabs/root”
sdetweil
(Sam)
January 6, 2018, 3:29pm
11
what does ‘call it from code’ need in the json object?
hikmat30ce
(Hikmat Ullah)
January 6, 2018, 3:30pm
12
Yes, I need to call it in php
sdetweil
(Sam)
January 6, 2018, 3:39pm
13
no… in the json object, what fields are required? named? anonymous, array, structure?
what php command are you using?
1 Like
hikmat30ce
(Hikmat Ullah)
January 6, 2018, 3:54pm
14
If you provide me json object that will enought,
by the way I am using below library
docker-php
sdetweil
(Sam)
January 6, 2018, 4:01pm
15
ok, setCmd method takes an anonymous array ([…,…,…])
$containerConfig = new ContainerConfig();
$containerConfig->setImage(imagename );
$containerConfig->setCmd([“bash”, “-c \“echo ‘*/5 * * * * /usr/bin/php /app/yii cronrun/runjob’>> /var/spool/cron/crontabs/root\””]);
containerCreateResult = $containerManager->create($containerConfig);
1 Like
huk9791
(Hikmat Ullah)
January 8, 2018, 6:40pm
17
Thanks @sdetweil for your help.
it worked by following below steps
1 create exec instance
2 start exec instance
docker