Skip to content

Instantly share code, notes, and snippets.

@haeramkeem
Last active August 3, 2022 04:27
Show Gist options
  • Save haeramkeem/7912ce945c79a46a6642113165b812d7 to your computer and use it in GitHub Desktop.
Save haeramkeem/7912ce945c79a46a6642113165b812d7 to your computer and use it in GitHub Desktop.
docker.io/haeramkeem/py-timelog:0.0.2

docker.io/haeramkeem/py-timelog:0.0.2

Source: https://github.com/haeramkeem/multidockers/tree/main/py-timelog

Build image

docker build -t $IMAGE_TAG .

Environment variables

  1. LOG_FILE_PATH: Path of the file which container writes the current time. Default is /var/log/time.log.
  2. LOG_INTERVAL: Time interval for log writing. Default is 1.

Releases

Official docker image can be found in here

FROM python:3.8-slim
# Set environments
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV LOG_FILE_PATH=/var/log/time.log
ENV LOG_INTERVAL=1
# Set working dir
WORKDIR /etc/py-timelog
COPY . .
# Set entrypoint
ENTRYPOINT [ "python3", "main.py" ]
import os
from datetime import datetime
from time import sleep
def log_to_file(content):
with open(os.environ.get("LOG_FILE_PATH"), 'a') as file_obj:
file_obj.write(content + '\n')
def main():
while True:
log_to_file("Current time: " + datetime.now().astimezone().isoformat())
sleep(int(os.environ.get("LOG_INTERVAL")))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment