Skip to content

Instantly share code, notes, and snippets.

@Natata
Created April 7, 2018 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Natata/ff83c0b5a348b02884ede5162ae90e1d to your computer and use it in GitHub Desktop.
Save Natata/ff83c0b5a348b02884ede5162ae90e1d to your computer and use it in GitHub Desktop.
Dockerfile example for run cron job on ubuntu
FROM ubuntu:16.04
MAINTAINER natataworld@love.com
ARG USER=docker_user
RUN apt-get update && apt-get -y install cron sudo
# create a normal user
RUN adduser --disabled-password --gecos '' $USER
RUN adduser $USER sudo
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
USER $USER
# Add crontab file in the cron directory
ADD crontab /home/$USER/hello-cron
RUN sudo chown $USER:$USER /home/$USER/hello-cron
# Give execution rights on the cron job
RUN chmod 0644 /home/$USER/hello-cron
# Create the log file to be able to run tail
RUN touch /home/$USER/cron.log
# Run the command on container startup
RUN crontab /home/$USER/hello-cron
CMD sudo service cron start && /bin/bash
# How to use:
# # docker build --rm -t natata/cron-example .
# # docker run -it natata/cron-example:latest
# in the container:
# # tail -f /home/docker_user/cron.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment