nickjj / build-a-saas-app-with-flask

Learn how to build a production ready web app with Flask and Docker.
https://buildasaasappwithflask.com
MIT License
959 stars 189 forks source link

Getting blank responses from cat watch #32

Closed treldor closed 8 years ago

treldor commented 8 years ago

attempt to deploy cat watch on linux server.

nginx sends request and is working fine. but cat watch gives error 404 in browser. see log:

`[2016-03-13 17:07:43 +0000] [7] [INFO] Starting gunicorn 19.3.0 [2016-03-13 17:07:43 +0000] [7] [INFO] Listening at: http://0.0.0.0:8000 (7) [2016-03-13 17:07:43 +0000] [7] [INFO] Using worker: sync [2016-03-13 17:07:43 +0000] [12] [INFO] Booting worker with pid: 12

Initializing Flask-DebugToolbar log handler

INFO in register [/catwatch/catwatch/register.py:170]:

GET "/" in 4ms for 94.119.68.63

GET "/" in 4ms for 94.119.68.63

`

nickjj commented 8 years ago

Hard to say without more details but a 404 makes me think maybe you need to update your SERVER_NAME setting to match your domain name or IP.

treldor commented 8 years ago

No registered domain yet, still using server's IP. In config.py currently SERVER_NAME = '46.xxx.xx.xxx:8000'. Correct no?

nickjj commented 8 years ago

Yes, that should work assuming it's the correct IP of your server.

At the moment there's too many unknowns. How are you deploying it? Are you one my students in the Docker for DevOps course (it goes into great detail on deploying a Flask app to production).

treldor commented 8 years ago

yes yes I deploy through dockerfordevops by running deploy.sh I have the Udemy everythng works postgres nginx redis, except for the cat watch with the 404 so issue with cat watch so I post here

treldor commented 8 years ago

docker ps output maybe useful:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0e69ef949457 nginx "/docker-entrypoint n" About an hour ago Up About an hour 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp nginx 2131badaa7d5 catwatch "/bin/sh -c 'gunicorn" About an hour ago Up About an hour 0.0.0.0:8000->8000/tcp catwatch b6de2bfc7ef8 postgres:9.4.5 "/docker-entrypoint.s" About an hour ago Up About an hour 0.0.0.0:5432->5432/tcp postgres 41e2af60ce0e redis:2.8.22 "/entrypoint.sh redis" About an hour ago Up About an hour 0.0.0.0:6379->6379/tcp redis

nickjj commented 8 years ago

Do you have the 46.X IP address set in nginx?

Also if you're using nginx, then you will need to remove the :8000 in SERVER_NAME and just access your IP address directly with no port.

After making 1 or both of those changes make sure you rebuild/deploy nginx, or copy over the new settings config, etc..

nickjj commented 8 years ago

I've deleted your comment that contained the source code to the course's material, since it's not open source.

I did download it and will take a look at it soon.

nickjj commented 8 years ago

Have you looked at the nginx config on your server? You can docker exec into a running instance of nginx and then run bash so you can snoop around the config file.

I'd be curious to see if they are what you think they should be.

treldor commented 8 years ago

sorry ofcourse did not think ok i will check too

nickjj commented 8 years ago

Any luck?

treldor commented 8 years ago

starting on fresh server to do everything again now

treldor commented 8 years ago

unfortunately not work I try mobydock again and it work! but cat watch always fail I can't know why

elie-h commented 8 years ago

I never managed to combine both courses together either. However I did see the same issue you're facing treldor. I remember making progress by redoing the SSL certificates as nick suggested in an email. So maybe try that?

nickjj commented 8 years ago

Yep, you'll need to make sure you copied over both the SSL certificates as well as your instance/settings.py file.

The easiest way to determine that's wrong would be to look at the docker logs for both nginx and catwatch on your server.

treldor commented 8 years ago

SSL exists i make again. on fresh server from systemd now i always getting: Failed to start Run catwatch. so if cat watch does not run then nginx does not run.

nickjj commented 8 years ago

Does it say why it failed to start?

Try doing sudo journalctl -u catwatch (or whatever your systemd unit file is named). If that doesn't give you anything useful then do a docker ps -a and then run a docker logs abc123 where abc123 is the failed container ID for catwatch.

You can repeat the same steps for nginx too at some point.

treldor commented 8 years ago

OK! maybe I find error!!! I do like you said: docker exec bash then check /etc/nginx/conf.d/default.conf server value still = www.production.com not the droplet IP

Like this:

server { listen 80; listen 443; server_name www.productionexample.com; return 301 https://productionexample.com$request_uri; }

but in the nginx folder I using locally and git pushing it is the correct IP I try fixing manually but when i restart docker it reset values www.productionexample.com

nickjj commented 8 years ago

What is the hostname on your server? You can run hostname on your server. It will only replace the correct server_name if the hostname doesn't match what you define in the docker-entrypoint file for nginx.

Also in your zip file you have a semi-colon after the domain name, that should be removed.

This strategy was explained in the Docker course. It allows you to use the same nginx configs for both staging and production. I would double check the nginx.conf from the Docker course as well as the entrypoint file to see how it's done.

treldor commented 8 years ago

hostname: treldor_moby, different from STAGING_HOSTNAME. I make DOMAIN_NAME= "4x.xxx.xx.xxx" correct IP push new code & restart docker but still 404 in browser :/

From docker logs: Nginx logs sending http request:

 - - [16/Mar/2016:22:48:51 +0000] "GET / HTTP/1.1" 404 136 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36"
 - - [16/Mar/2016:22:48:52 +0000] "GET / HTTP/1.1" 404 136 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36"
- - [16/Mar/2016:22:48:53 +0000] "GET / HTTP/1.1" 404 136 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36"

Catwatch logs is still blank:

[2016-03-16 22:45:01 +0000] [7] [INFO] Starting gunicorn 19.3.0 [2016-03-16 22:45:01 +0000] [7] [INFO] Listening at: http://0.0.0.0:8000 (7) [2016-03-16 22:45:01 +0000] [7] [INFO] Using worker: sync [2016-03-16 22:45:01 +0000] [12] [INFO] Booting worker with pid: 12

Initializing Flask-DebugToolbar log handler

INFO in register [/catwatch/catwatch/register.py:170]:

GET "/" in 6ms for xxx.xxx.x.x

GET "/" in 6ms for xxx.xxx.x.x

INFO in register [/catwatch/catwatch/register.py:170]:

GET "/" in 0ms for xxx.xxx.x.x

nickjj commented 8 years ago

did you put STAGING_HOSTNAME="treldor_moby" in the entrypoint file?

You'd also need to add -> commit -> push nginx.

treldor commented 8 years ago

no i have STAGING_HOSTNAME="stagingserver" and DOMAIN_NAME="46.xx.xx.xx" < droplet ip

correct? because then if statement will replace staging IP with domain IP inside "/etc/nginx/conf.d/default.conf"

yes I git add and commit and push correctly

nickjj commented 8 years ago

STAGING_HOSTNAME needs to be the hostname of your server. I recommend reviewing how the staging server is set up and replicate that exactly on your end for catwatch.

treldor commented 8 years ago

but this is for production droplet in digital ocean! following lecture 80 dockerfordevops

nickjj commented 8 years ago

The only possible way for it to be using productionexample.com as the server_name is because you haven't commit/pushed nginx to the DO server.

The only place that's being set is in the docker-entrypoint file of nginx.

treldor commented 8 years ago

Ok now I do:

rm -rf .git git init git add -A git commit -am "Initial Commit" git remote add production ssh://me@x.x.x.x:/var/git/nginx.git git push production master

maybe before I skip step. now "/etc/nginx/conf.d/default.conf" is correct in docker image:

upstream catwatch { server catwatch:8000; }

AND

server_name 46.xxx.xx.xxx; (correct IP)

if default.conf is open source i can post file here if easier to debug

treldor commented 8 years ago

still 404 in browser!

nickjj commented 8 years ago

If what you say about the default.conf file is true (it's not open source) then it should be good.

Now, are you sure SERVER_NAME is set to the IP address without the port 8000? Check the config on your DO server.

Have you re-looked at any of the Docker logs for both containers since pushing the correct version?

treldor commented 8 years ago

yes I go on droplet: docker exec nginx bash cat /etc/nginx/conf.d/default.conf

SERVER catwatch:8000 and SERVER_NAME 46.xx.xx.xx; (droplet ip with no port)

elie-h commented 8 years ago

Could you please do: docker ps docker images

treldor commented 8 years ago

ok:

IMAGES THEN PS:

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
nginx               latest              482f5bdcbf59        10 minutes ago      190.5 MB
catwatch            latest              96153d66f027        About an hour ago   693.9 MB
nginx               1.9                 6f62f48c4e55        7 days ago          190.5 MB
postgres            9.4.5               de35bfc3a186        7 weeks ago         263.1 MB
python              2.7.10-slim         0d91c552e0d8        3 months ago        204.9 MB
redis               2.8.22              579cf374438d        5 months ago        109 MB

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                      NAMES
236ff10bfe17        nginx               "/docker-entrypoint n"   10 minutes ago      Up 10 minutes       0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   nginx
82711661058e        catwatch            "/bin/sh -c 'gunicorn"   45 minutes ago      Up 45 minutes       0.0.0.0:8000->8000/tcp                     cat watch
fe37bd5d6092        postgres:9.4.5      "/docker-entrypoint.s"   45 minutes ago      Up 45 minutes       0.0.0.0:5432->5432/tcp                     postgres
9ed0064eeb80        redis:2.8.22        "/entrypoint.sh redis"   45 minutes ago      Up 45 minutes       0.0.0.0:6379->6379/tcp                     redis
nickjj commented 8 years ago

That's a different server name. I mean the one in instance/settings.py for catwatch. It needs to be 46.xx.xx.xx and this file needs to be copied/mounted to your catwatch container in the same way moby dock was in the Docker course.

This one: https://github.com/nickjj/build-a-saas-app-with-flask/blob/master/instance/settings.py.production_example#L3

elie-h commented 8 years ago

Nick, shouldn't the other docker images be running too? Faye and cadvisor

nickjj commented 8 years ago

Yep, Faye should be at least. cadvisor should technically be running but it's very possible he just removed it since it's not essential.

treldor commented 8 years ago

i have settings.py not settings.py.production? in the folder of the production or the local? it is for sure committed to the code? or should it be manual?

elie-h commented 8 years ago

SSH into your droplet then:

sudo nano /home/YOURUSERNAME/instance/settings.py add the line: SERVER_NAME = 'DROPLETS_IP' sudo systemctl restart docker

This isn't the way it's done in the course. But just to confirm if it is causing the 404 do it manually like above.

nickjj commented 8 years ago

It should be settings.py. It has the .production in the git repo because settings.py is added to gitignore, since this file would contain sensitive information.

treldor commented 8 years ago

YES! IT WORK!!!! THNX SO MUCH TWO OF YOU!!!

nickjj commented 8 years ago

Glad you got it working.

It's similar to the course, the only difference is the SERVER_NAME is set to a domain name rather than the IP address of the droplet. Same concept, different values.

Thanks for helping out @eliehamouche.

elie-h commented 8 years ago

Great! Thank Nick!

Add Faye to the docker images in the deploy script in: DOCKER_PULL_IMAGES If I remember correctly that's the only addition you have to make to deploy.sh to accommodate for Catwatch.

nickjj commented 8 years ago

Right, since the Docker course's example app didn't have Faye.

Also to expand on your previous comment, you only need to restart the catwatch container after editing that instance/settings.py file. You don't need to restart docker fully (this will restart everything).

You can simply run: docker stop catwatch , and systemd will automatically restart it with the new changes.

treldor commented 8 years ago

good very good!

any .service file for Faye?

nickjj commented 8 years ago

I'll leave that as a homework assignment for you. The service file would be very similar to the other service files. Just look at the postgres or redis service file and modify it.

It's part of the paid content for the course that goes over this open source project.

However, I don't recommend buying it just yet because I'm in the process of making massive changes in the near future.

You can read about them @ http://nickjanetakis.com/blog/build-a-saas-app-with-flask-is-getting-a-complete-make-over-soon.

treldor commented 8 years ago

ok i will do it! thank you. very good learning experience. i close finally! thank you for patience

nickjj commented 8 years ago

Thanks, and no problem.

You'll also want to create a service file for Celery too. Just use the catwatch one as a reference, it's nearly the same.