Docker内如何使用crontab
-
run.sh
#!/bin/bash timestamp=`date +%Y/%m/%d-%H:%M:%S` echo "System path is $PATH at $timestamp"
-
entrypoint.sh
#!/bin/bash
# Start the run once job.
echo "Docker container has been started"
declare -p | grep -Ev 'BASHOPTS|BASH_VERSINFO|EUID|PPID|SHELLOPTS|UID' > /container.env
# Setup a cron schedule
echo "SHELL=/bin/bash
BASH_ENV=/container.env
* * * * * /run.sh >> /var/log/cron.log 2>&1
# This extra line makes it a valid cron" > scheduler.txt
crontab scheduler.txt
cron -f
-
Dockefile
FROM ubuntu:16.04 MAINTAINER Neha Bhardwaj # Install cron RUN apt-get update && apt-get install -y cron # Add files ADD run.sh /run.sh ADD entrypoint.sh /entrypoint.sh RUN chmod +x /run.sh /entrypoint.sh ENTRYPOINT /entrypoint.sh
Setting up a cron in docker
Build Docker image
docker build -t docker-cron .
Initiate the container
docker run -p 8080:8080 docker-cron
Test the cron output
- Copy the container id from the output of this command
docker ps | grep docker-cron - Login to the docker container using the
container_id
docker exec -it <container_id> /bin/bash
Example: docker exec -it bc8a9bbfbba9 /bin/bash
- Copy the container id from the output of this command
-
结果