⬅️ Back to repo home ▪️
Capturing the logs generated by an application in a file and binding it to a volume outside the container for data persistence.
- Run a logging app which creates random logs inside a text file.
- Bind the log file with a directory in the host machine.
- Retrieve real-time logs.
You have two files in your aplication folder:
generate_logs.py
which acts as the main application.app_logger.py
which acts a repository for helper functions.
You can configure a logger function which will write application logs to a text file by appending lines of text.
The logger function is defined inside app_logger.py
which can act as a repository for helper functions inside your application.
The app_logger.py
uses two environment variables to work (as in any other proper application):
DEBUG
allows the logger function to generate log data.LOG_FILE_PATH
tells the logger the location of the log file to write to.
To run this setup you can either use a single docker command:
cd 004.\ generate\ a\ log\ file\ outside\ of\ the\ container/
# build your image with name "log_generator"
docker build -t log_generator .
# start your image
docker run --name log_gen -v .:/app -w /app -e DEBUG=1 -e LOG_FILE_PATH="logs.txt" -d log_generator
or alternatively, you can simply run the docker-compose.yml
file which is the equivalent to the docker commands shown above:
cd 004.\ generate\ a\ log\ file\ outside\ of\ the\ container/
docker compose up
You will see your logs being generated like this: