For work I was asked to run cronjobs in a Docker container. These are jobs that will be run on a server related to work. The actual cron script I am currently running in this container consists of just ” filler” jobs that are meant to be silly, and are just being used to debug the process.
I am running a docker container using a Ubuntu image. Here is the Docker file:
FROM ubuntu:latest
# Install necessary software
RUN apt-get update &&
apt-get -y install cron
# Add crontab file
COPY crontab /etc/cron.d/crontab
# make the directory
#RUN mkdir etc/cron.d
# create a crontab file in a new location
RUN touch etc/cron.d/crontab
# Give execution rights on the cron job
RUN chmod 644 /etc/cron.d/crontab
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Start the cron service
CMD cron && tail -f /var/log/cron.log`
Here is the docker compose ( I changed the username and pword just for privacy, it’s something more specific in the actual file):
version: "3"
services:
ubuntu_cron_sql_container:
build:
context: .
dockerfile: docker/Dockerfile
image: ubuntu:latest
container_name: ubuntu_cron_sql_container
environment:
MYSQL_USER: 'username'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
volumes:
- ./mysql_server/init.d:/docker-entrypoint-initdb.d
ports:
- 3306:3306
# expose:
# - '3306'
restart: on-failure
(I am planning on adding MySQL to the container next, that’s why it’s in the docker compose, but not in the Dockerfile.)
I am starting the container using the following Bash scripts (these were actually provided to me by someone else at work, I modified them on my own).
I run “build.sh” first:
#!/bin/sh
docker container rm ubuntu_cron_sql_container
docker-compose -f docker-compose.yml build --force-rm
Followed by “run-db.sh”
#!/bin/sh
#docker container rm ubuntu_cron_sql_container
docker-compose -f docker-compose.yml up ubuntu_cron_sql_container
When I access the running container using the Bash shell, and run
ps -ef | grep cron | grep -v gre
I get:
root 1 0 0 17:04 ? 00:00:00 /bin/sh -c cron && tail -f /var/log/cron.log
root 8 1 0 17:04 ? 00:00:00 cron
root 9 1 0 17:04 ? 00:00:00 tail -f /var/log/cron.log
When I run the following at the CL inside the container:
/sbin/service cron start
I get the following output:
* Starting periodic command scheduler cron
But the log file ( which is indeed where it should be) is still empty:
cat cron.log
(literally no output)
The ‘crontab’ file itself, and it is indeed in etc/cron.d inside the container
MAILTO= ( my work email is here in the actual file)
*/10 * * * * echo 'ABC' >> /var/log/cron.log
*/10 * * * * /Users/myname/cron_docker_practice/kitten.sh
*/10 * * * * echo 'TESTING'
*/10 * * * * echo " Running cronjob in Ubuntu container"
*/10 * * * * echo "Cronjob running in ubuntu container" >> /var/log/cron.log
To try to debug this on my own, I tried the above commands to test if cron was running in the Docker container, I checked that the files copied into the right directories, and I tried a few commands to force cron to run after the Docker container is up and running (from inside the container).
2