Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the container's logs? #6

Closed
aleonchen opened this issue Apr 24, 2017 · 7 comments
Closed

How to get the container's logs? #6

aleonchen opened this issue Apr 24, 2017 · 7 comments

Comments

@aleonchen
Copy link

aleonchen commented Apr 24, 2017

Hi tiangolo

Great thanks for your dockers, I could setup a simple web app with flask.
I found when the app kept running somethings, it will hang up.
When I connect the running container's bash, I could only get the flask logs on the bash.
But could not get the nginx logging.

Do you have any advice for it?
Thank you

@tiangolo
Copy link
Owner

@aleonchen all the logs are redirected to stdout. It means, that you don't need to connect with bash, they are readable with:

docker logs <your container>
  • If you only want, let's say, the last 50 lines:
docker logs --tail=50 <your container>
  • If you want it to keep showing the log "live" (without having to re-type the previous commands) you can:
docker logs -f <your container>

@aleonchen
Copy link
Author

@tiangolo
Great thanks. I have tried this.

But it could only show the flask logging info.

How could I get the nginx logging?

For example the access.log or error.log

@tiangolo
Copy link
Owner

The thing is, you should be already getting those logs.

For example, if you try requesting something inside /static/, let's say /static/index.html. You should get something in your log like:

10.42.144.135 - - [26/Apr/2017:17:26:50 +0000] "GET /static/index.html HTTP/1.1" 200 7429 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36" "186.154.85.228"

That log would be from Nginx. As Flask doesn't even get the request. Nginx reads it and sees that you are asking for something under /static/, and then it just goes away and serves it, without asking anything from the Python side.


But if you are running any CMD in your image, you are overriding all the "magic" done by this image. The same if you are passing a specific command to your docker run, as in:

docker run -it myimage python /app/main.py

That is useful for developing and / or debugging, but for "production", or when you want to test the Supervisord / Nginx / uWSGI functionality that this image provides, you should remove those CMD or direct commands.

@aleonchen
Copy link
Author

@tiangolo

OK, I got what you say, but I found that when I run the docker for some time. The flask web service will hung up, and I could not found what the issue is.

For example
Step 1
I start up the docker as production.

docker run --rm -d -v $(pwd)/webapp:/app -p 80:5000 --link mysql:mysql --name containername imagename

Step 2
After sometime no response with the request.

I use the docker logs -f containername to see the logs
It is always stop on some logs like

xx.xx.xx.xx - - [27/Apr/2017 18:22:15] "GET / HTTP/1.1" 302

Step 3
I want to check if where is wrong, the nginx or python, but I have no idea about it.

@tiangolo
Copy link
Owner

Well, the first thing, the trick of the $(pwd)/webapp:/app should be used in development, not in production.

If Nginx was not being able to communicate with uWSGI (Flask) it would report it, with something saying that it couldn't connect to the socket or something similar.

I can't help you debug it if you only post the idea you have of the log. You need to copy the real log for me to be able to try to help you. When you get the error, copy a big chunk of the log. With what you have provided I can't really check anything.


Also, let me suggest you to use Sentry. You can create a free account or deploy yourself the opensource version with Docker. That will help you a lot detecting problems in your code.

@aleonchen
Copy link
Author

aleonchen commented Apr 29, 2017

@tiangolo After some digging I found the problem.

When I use docker exec -it <container> ps aux
The uwsgi and nginx is not running.

2017-04-29 9 27 14

Then I check the Dockerfile and found the issue.

FROM tiangolo/uwsgi-nginx-flask:flask-python3.5
MAINTAINER aleonchen  "aleonchen@gmail.com"
COPY ./webapp/requirements.txt /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["main.py"]

I shoud remove the last two lines.

Thanks a lot for your patient.

But now my question is if I do not use $(pwd)/webapp:/app, what is the good way to update the code on server side?
Every time update the code rebuild the image?

@tiangolo
Copy link
Owner

I'm glad you were able to solve your issue!


The best way is to COPY ./webapp/ /app/ in your Dockerfile. And when you have a newer version, re-build the image and replace the old container.

Your "state" shouldn't be in that container anyway, it should be in a database in another container. Or at least, in a volume, like a named volume.


Then, I will now close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants