Add sample on how to run a cron task in a docker container

This commit is contained in:
Marcel Schwarz 2023-12-01 22:47:58 +01:00
parent 42fb4a0d19
commit 5538470a5d
5 changed files with 41 additions and 0 deletions

12
Dockerfile Normal file
View File

@ -0,0 +1,12 @@
# Dockerfile to create image with cron services
FROM ubuntu:latest
# Install Cron
RUN apt-get update \
&& apt-get -y install cron \
&& rm -rf /var/lib/apt/lists/*
RUN useradd test
# Run the command on container startup
CMD crontab -u test /home/test/cron/crontab-file && cron -f

1
crontab-file Normal file
View File

@ -0,0 +1 @@
* * * * * bash /test/cron/task.sh

8
docker-compose.yml Normal file
View File

@ -0,0 +1,8 @@
version: '3'
services:
cron:
build: .
volumes:
- ./output:/output
- ./:/home/test/cron

19
output/out.txt Normal file
View File

@ -0,0 +1,19 @@
Hello World at Thu Nov 30 21:45:01 UTC 2023
Hello World at Thu Nov 30 21:46:01 UTC 2023
Hello World at Thu Nov 30 21:47:01 UTC 2023
Hello World at Thu Nov 30 21:48:01 UTC 2023
Hello World at Thu Nov 30 21:52:01 UTC 2023
Hello World at Thu Nov 30 21:53:01 UTC 2023
Hello World at Thu Nov 30 21:54:01 UTC 2023
Hello World at Thu Nov 30 21:55:01 UTC 2023
Hello World at Thu Nov 30 21:56:01 UTC 2023
Hello World at Thu Nov 30 21:57:01 UTC 2023
Hello World at Thu Nov 30 22:02:01 UTC 2023
Hello World at Thu Nov 30 22:03:01 UTC 2023
Hello World at Thu Nov 30 22:04:01 UTC 2023
Hello World at Thu Nov 30 22:05:01 UTC 2023
Hello World at Thu Nov 30 22:06:01 UTC 2023
Hello World at Thu Nov 30 22:07:01 UTC 2023
Hello World at at at at at Thu Nov 30 22:08:01 UTC 2023
Hello World at at at at at Thu Nov 30 22:09:01 UTC 2023: /root
Hello World at at at at at Thu Nov 30 22:10:01 UTC 2023: /root

1
task.sh Executable file
View File

@ -0,0 +1 @@
echo "Hello World at at at at at $(date): $(pwd)" >> /output/out.txt