Skip to content

Instantly share code, notes, and snippets.

@zenthangplus
Last active April 3, 2019 09:22
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 zenthangplus/c38851b8ae4810a29b5a5e9b65b1f21e to your computer and use it in GitHub Desktop.
Save zenthangplus/c38851b8ae4810a29b5a5e9b65b1f21e to your computer and use it in GitHub Desktop.
Redirect logs from file to docker's output
# To do that, you need to create symlink from your file (inside container) to docker's output file.
# Add bellow line to your Dockerfile
RUN ln -sf /proc/1/fd/1 /var/log/cronjob.log
# EXPLAIN:
# The first 1 is the PID of docker process.
# The second 1 is standard output
# Note: If you launch your container with --pid=host, please use following line
RUN ln -sf /proc/$$/fd/1 /var/log/cronjob.log
# EXPLAIN:
# $$ is the PID of the current script itself.
FROM php:7.2-fpm
RUN apt-get update && apt-get -y install cron
# Create crontab file in the cron directory
RUN echo "* * * * * /usr/local/bin/php -r \"echo time() . PHP_EOL;\" >> /var/log/cronjob.log 2>&1" > /etc/cron.d/crontab
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/crontab
# Apply cron job
RUN crontab /etc/cron.d/crontab
# Create cronjob log file
RUN touch /var/log/cronjob.log
# Redirect cronjob log to docker log collector
RUN ln -sf /proc/$$/fd/1 /var/log/cronjob.log
CMD ["bash", "-c", "cron -f & php-fpm"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment