I have a python server that I run in a docker container. I log my messages with logging from python and I don't know how to find/view my log file from the container.
# Pull base image
FROM python:3
# Copy code to the container
ADD src /usr/app
WORKDIR /usr/app
# run configuration
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
# Start script
CMD [ "python", "./main.py"]
I start the server like this:
sudo docker run -it -p 5000:5000 --rm my_app
How can I access server logs?
Thanks
First of all, to list all running containers, use the docker ps command. Then, with the docker logs command you can list the logs for a particular container. Most of the time you'll end up tailing these logs in real time, or checking the last few logs lines.
Where Are Docker Logs Stored By Default? The logging driver enables you to choose how and where to ship your data. The default logging driver as I mentioned above is a JSON file located on the local disk of your Docker host: /var/lib/docker/containers/[container-id]/[container-id]-json.
docker logs --tail 200 -f docker-container-id
I think you can use -v parameter in your "docker run" to mount your host folder to a folder in container.
sudo docker run -it -p 5000:5000 -v hostPath:containerPathContainingLogs --rm my_app
Then you will view your log in hostPath(It is mapped into containerPathContainingLogs in your container).
Or in another way, if your docker image contains bash, you can use docker ps -a, to find the container id you run, and "docker exec -it your-container-id bash" to login your container, cd to your log path and view your log.
Or you can try docker logs your-container-id.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With