Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access logs from docker container

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

like image 602
Dorin Avatar asked Dec 24 '17 14:12

Dorin


People also ask

How do I access docker container logs?

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 container logs stored?

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.


2 Answers

docker logs --tail 200 -f docker-container-id

like image 61
args Avatar answered Sep 30 '22 23:09

args


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.

like image 36
firesurfing Avatar answered Oct 01 '22 00:10

firesurfing