Docker + Django + PostgreSQL + Heroku = Failed to bind to $PORT within 60 seconds - django

So I am building a Dockerized Django project and I want to deploy it to Heroku, but I am having a lot of issues. My issues are exactly the same as this post:
Docker + Django + Postgres Add-on + Heroku
Except I cannot use CMD python3 manage.py runserver 0.0.0.0:$PORT since I receive an invalid port pair error.
I'm just running
heroku container:push web
heroku container:release web
heroku open
After going to the site it stays loading until it says an error occurred. My log shows the following:
System check identified no issues (0 silenced).
2019-05-03T11:38:47.708761+00:00 app[web.1]: May 03, 2019 - 11:38:47
2019-05-03T11:38:47.709011+00:00 app[web.1]: Django version 2.2.1, using settings 'loan_app.settings.heroku'
2019-05-03T11:38:47.709012+00:00 app[web.1]: Starting development server at http://0.0.0.0:8000/
2019-05-03T11:38:47.709014+00:00 app[web.1]: Quit the server with CONTROL-C.
2019-05-03T11:38:55.505334+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/" host=albmej-loan-application.herokuapp.com request_id=9037f839-8421-46f2-943a-599ec3cc6cb6 fwd="129.161.215.240" dyno= connect= service= status=503 bytes= protocol=https
2019-05-03T11:39:45.091840+00:00 heroku[web.1]: State changed from starting to crashed
2019-05-03T11:39:45.012262+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-05-03T11:39:45.012440+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-05-03T11:39:45.082756+00:00 heroku[web.1]: Process exited with status 137
The app works locally through a virtual environment and using Docker but just not on Heroku. Not sure what else to try. You can find my code at: https://github.com/AlbMej/Online-Loan-Application
Maybe I have some glaring problems in my Dockerfile or docker-compose.yml

The Answer is not correct.
If you use container with Dockerfile, you do not need any Profile.
Just use the $PORT variable to let heroku to determine which port to use.
https://help.heroku.com/PPBPA231/how-do-i-use-the-port-environment-variable-in-container-based-apps

Quick solution is to change your Procfile to this:
web: python subfolder/manage.py runserver 0.0.0.0:$PORT
That would work, but keep in mind that you are using the development server on production, which is a really bad idea! But if you are just toying around, that's ok.
However, if you're using this as a production app with real data, you should use a real production server. Then your Procfile would look like this:
web: gunicorn yourapp.wsgi --log-file -

Related

Error Heroku , desc="No web processes running" method=GET path="/favicon.ico"? [duplicate]

error H14 happen while deploying to heroku
this is my procfile:
web: gunicorn -w 4 -b 0.0.0.0:$PORT -k gevent main:app
log on heroku:
2017-01-23T10:42:58.904480+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/" host=meetcapstone.herokuapp.com request_id=df88efb5-a81a-4ac0-86dc-4e03d71266bb fwd="81.218.117.137" dyno= connect= service= status=503 bytes=
2017-01-23T10:42:59.009135+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/favicon.ico" host=meetcapstone.herokuapp.com request_id=21cea981-36b0-4410-891f-548bbc29f0ee fwd="81.218.117.137" dyno= connect= service= status=503 bytes=
requirements:
Flask==0.11.1
passlib==1.7.0
SQLAlchemy==1.1.5
Werkzeug==0.11.15
gunicorn==19.0.0
gevent==1.2.1
The issue here is that you're not running any web dynos. You can tell Heroku to do this via:
$ heroku ps:scale web=1
This will force Heroku to spin up a web dyno, thereby executing your gunicorn command.
After 3 hours of debugging, I've figured out why my app was causing this error:
My Procfile was incorrectly cased
gunicorn wasn't installed in my venv
IMO, this error should be raised on Heroku's end. As a beginner, this sort of error is difficult to trace.
Update:
To clarify, Procfile is correctly cased and procfile is not correctly cased. It should start with a capital "P".
More info on dyno configuration – more on initializing your heroku app.
I ran into the same problem but from a different cause. I had the hobby tier, but then canceled it and reverted back to the free tier. Doing this caused the error and how I fixed it was just re running the command from the cli:
heroku ps:scale web=1
Before this command:
heroku ps:scale web=1
I had to remove and add buildpacks again and empty commit it and redeploy it to heroku.
heroku buildpacks:clear
heroku buildpacks:add --index heroku/python
I was having an issue here too. My problem was that my Procfile was "Procfile.txt" .
What solved my issue was to remove the file extension from Procfile, then recommit
and push stuff to heroku
Login to your Heroku dashboard and open your projects.
Go to Settings.
Delete heroku/python from the list of buildpacks
Then click Add buildpack → Choose "Python" → Save Changes.
Activate your environment in your code.
Run heroku ps:scale web=1.
And you're done!
This isn't the problem with your code, but I've gotten this error message a couple of times now and the mistake that I've made that has caused it has been writing
web:gunicorn
instead of
web: gunicorn
That space can really cause a lot of issues.
My issue is that Heroku removed the free plans. To solve such an issue go to Heroku and select/change your free plan to for example "eco" plan.
I have a UAT version I only enable during client development.
I have a custom dyno script but it's turned to the free version. So the app was not starting as my script was not running. When I enabled the Dyno the toggle was still off :rolleyes:
I don't have the reputation to reply to the correct comment, but for me the issue was that I didn't have the run.gunicorn.sh file in my root directory, this resulted in the same "No web processes running" error.
If you don't have this file, create it with contents:
gunicorn -b :5000 --access-logfile - --error-logfile - build:app
Where 'build' is the name of your python file (build.py in this case) and app is the name of your app in the code.
Also make sure that gunicorn is included in requirements.txt, like others have already pointed out.
Yeah I was also using web heroku-php-apache2 dyno and reverted it back to free tier and that caused the dyno to sleep fortunately executing heroku ps:scale web=1 -a <app name> did the magic.
Change your Procfile file from
web:gunicorn to web gunicorn (remove the ':')
I fixed the issue by going to Configure Dynos and enabling the only dyno I had manually.
uff..that took some time,so the fixes i had to make were:
'Procfile' with upper case P.
web: gunicorn wsgi:app (with a space after web: in procfile)
Making sure the requirements.txt are in the root project folder.
I was missing dynos on the web gui. The cli command to scale did not work. I also may have had an incorrect run:web declaration with missing $PORT. To fix:
heroku.yml must have a web declaration using the $PORT var:
build:
docker:
web: Dockerfile
run:
web: uvicorn main:app --reload --host 0.0.0.0 --port $PORT
I then pushed to heroku.
After that it must have added the web dyno, I could then run:
heroku ps:scale web=1
And now the fastapi uvicorn runs.
Pay attention to the Procfile naming and location (https://devcenter.heroku.com/articles/procfile) The Procfile is always a "simple text file" that is named Procfile without a file extension.(Procfile.txt not acceptable!) The Procfile must live in your app's root directory. It does not function if placed anywhere else.
Faced the exact same problem turns out I had the Profile in .gitignore
I was placing my django Procfile in the directory with settings.py and not the root directory and that gave me the H14 error. I fixed the error with this and I didn't need to do anything else they say.
Procfile
web: gunicorn <django-root-name(containing wsgi)>.wsgi
There are many things that can go wrong here. Its a combination of poor shepherding by heroku and ambiguous use between flask & gunicorn.
Here is a good guide that will get you up and running:
To anyone who may come across this...
delete your Procfile
create 'Procfile' with upper case P.
in your Procfile type: web: gunicorn <nameOfRootFile>:app (with a space after web: in procfile) mine for example was web: gunicorn app:app another way I wrote it that worked was this: web: gunicorn -w 4 "app:create_app()" -t 120
Making sure the requirements.txt are in the root project folder. (you can run pip freeze > requirements.txt if you do not have the file created
deploy to heroku
heroku ps:scale web=1 (you can specify app name to like this heroku ps:scale web=1 -a appname
finally in terminal run heroku restart
heroku open
these are all the steps i took to get mine to work
web: gunicorn weather.wsgi --log-file -
this worked for me, just make sure your Procfile is in the right format, and specify the app you are connecting to, in my case it's the weather app. Enjoy
What worked for me was adding on the second line of the procfile:
heroku ps:scale web=1
The first line must contain:
web: gunicorn "filename":"main method name"

Docker Image works with docker-compose up but not on Amazon ECS or Heroku

I am trying to host a Django project with a Postgres database in a Docker container. The project is a practice e-commerce site with a database for product info. I was able to get it working with docker-compose up and accessed the site running in the container at localhost:8000 but when I tried hosting it on AWS it didn't work. I uploaded the image to ECR and started a cluster. When I tried running a task with the image, it showed PENDING but as soon as I tried to refresh, the task was gone. I tried setting up cloudwatch logs but they were empty since the task was stopping immediately after starting. After that I tried hosting on Heroku. I was able to deploy the image but when I tried to open the app it showed an error (shown below).
It feels like the image is just failing immediately whenever I try hosting it anywhere, but it works fine when I use docker-compose up. I think I'm making a very basic mistake (I'm a total beginner at all this) but not sure what it is. Thanks for taking the time to help.
I'll also add my Dockerfile and docker-compose.yml
Error Message from Heroku
2022-11-25T05:13:31.719689+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/" host=hk-comic-app.herokuapp.com request_id=ea683b1d-e869-4ea9-98aa-2b9ed08f7219 fwd="98.116.68.242" dyno= connect= service= status=503 bytes= protocol=https
2022-11-25T05:22:36.083750+00:00 app[api]: Scaled to app#1:Free by user
2022-11-25T05:22:39.300239+00:00 heroku[app.1]: Starting process with command `python3`
2022-11-25T05:22:39.895200+00:00 heroku[app.1]: State changed from starting to up
2022-11-25T05:22:40.178736+00:00 heroku[app.1]: Process exited with status 0
2022-11-25T05:22:40.228638+00:00 heroku[app.1]: State changed from up to crashed
2022-11-25T05:22:40.232742+00:00 heroku[app.1]: State changed from crashed to starting
2022-11-25T05:22:43.937389+00:00 heroku[app.1]: Starting process with command `python3`
2022-11-25T05:22:44.610097+00:00 heroku[app.1]: State changed from starting to up
2022-11-25T05:22:45.130636+00:00 heroku[app.1]: Process exited with status 0
2022-11-25T05:22:45.180808+00:00 heroku[app.1]: State changed from up to crashed
2022-11-25T05:23:09.462805+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/" host=hk-comic-app.herokuapp.com request_id=f4cc3e04-0257-4336-94b3-7e48094cabd4 fwd="98.116.68.242" dyno= connect= service= status=503 bytes= protocol=https
Dockerfile
FROM python:3.9-slim-buster
ENV PYTHONUNBUFFERED=1
WORKDIR /django
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
docker-compose.yml
version: "3"
services:
app:
build: .
volumes:
- .:/django
- ./wait-for-it.sh:/wait-for-it.sh
ports:
- 8000:8000
image: app:django
container_name: django_container
command: /wait-for-it.sh db:5432 -- python3 manage.py runserver 0.0.0.0:8000
depends_on:
- db
db:
image: postgres
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=comic_db
- POSTGRES_USER=comic_user
- POSTGRES_PASSWORD=password
container_name: postgres_db
Heroku doesn't use docker-compose.yml. You'll need to make a few changes:
Update your Dockerfile to include a command that should be used to run your application, e.g.
CMD gunicorn project_name.wsgi
This shouldn't impact your development environment since your docker-compose.yml overrides the command. You'll need to make sure Gunicorn (or whatever WSGI server you choose to use) is declared as a dependency.
Update your Django application to get its PostgreSQL connection string from the DATABASE_URL environment variable that Heroku provides. A common way to do that is by adding a dependency on dj-database-url and then changing your DATABASES setting accordingly:
DATABASES["default"] = dj_database_url.config()
I suggest you read the documentation for that library as there's more than one way to use it.
For example, you can optionally set a default connection for development via a default argument here. Or, if you prefer, you could set your own DATABASE_URL environment variable in your docker-compose.yml.
Provision a PostgreSQL database for your application. Make sure to do the first step to check whether you already have a database.
Then redeploy.

Django site hosting on Heroku "No web processes runnng"

So I tried to host my Django application to Heroku and at first it worked. Then I wanted to add some release commands so I did this:
release_commands.sh
python manage.py makemigrations
python manage.py migrate
And in my Procfile:
release: bash ./release_commands.sh
web: gunicorn todo.wsgi --log-file -
The second line in my Procfile is unchanged, so I guessed that the error must be coming from the release commands, but that's not the case as in my heroku logs I have this:
2021-02-17T19:41:31.675215+00:00 heroku[release.6232]: Process exited with status 0
2021-02-17T19:41:31.772693+00:00 heroku[release.6232]: State changed from up to complete
2021-02-17T19:41:34.021990+00:00 app[api]: Release v11 created by user georgi.hr.dim#gmail.com
This is the error message:
2021-02-17T19:41:43.594322+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=POST path="/accounts/login/?next=/" host=essentialtodo.herokuapp.com request_id=5dce4816-6343-44c3-9c8f
-171e79379abc fwd="78.90.0.206" dyno= connect= service= status=503 bytes= protocol=https
Just found out what the problem was. For some reason after adding the release commands my dynos were turned off. To fix this: go to your heroku app (on the heroku website) -> go to resources -> press the edit button on your dyno -> turn it on and confirm
.

Django on Heroku. No web processes running

I have a Django app which works when I run it locally heroku local. When I push my code to heroku git push heroku main it pushes without any problem, but when I opening my site it showes an 503 error in console
1 GET https://XXX.herokuapp.com/ 503 (Service Unavailable)
inside my logs I get this
at=error code=H14 desc="No web processes running" method=GET path="/" host=omylibrary.herokuapp.com request_id=lotofnumbers fwd="somenumbers" dyno= connect= service= status=503 bytes= protocol=https
from this question I assume that the problem is in ProcFile if more precisely in web. My Procfile looks like this
web: gunicorn --chdir ./Lib library.wsgi --log-file -
I also tried this
heroku ps:scale web=1
from previous answer. What Is wrong here ?
also heroku ps returns No dynos on mysite
The problem was that I named ProcFile instead of Procfile I found the solution here I have checked logs of heroku while pushing my app by terminal and I got
remote: Procfile declares types -> (none)
so if you don't have web process running probably it's because of procfile or the code inside it

Error R10 (Boot timeout) : Django app on heroku

I have been trying to deploy a django app on Heroku. But it is always failing with the following error (R10 timeout, failed to bind to PORT in 60 seconds). The app works absolutely fine when deployed locally. I have been able to configure the postgres db correctly on heroku (bootstrapped the data and checked the contents). But somehow my web application is not starting, as can be seen in the logs below.
I have tried setting up explicit port number, as suggested in some forums. But it didn't work.
Any idea what's going wrong ?
I have put the heroku logs below. My code is here, if it helps : https://github.com/solitaryreaper/sp_mysiswrites
Thanks.
2014-02-25T06:38:08.220957+00:00 app[web.1]: 0 errors found
2014-02-25T06:38:08.225056+00:00 app[web.1]: February 25, 2014 - 06:38:08
2014-02-25T06:38:08.225056+00:00 app[web.1]: Django version 1.6.2, using settings 'sisnewsarticles.settings'
2014-02-25T06:38:08.225056+00:00 app[web.1]: Starting development server at http://127.0.0.1:8000/
2014-02-25T06:38:08.225056+00:00 app[web.1]: Quit the server with CONTROL-C.
2014-02-25T06:39:06.941786+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2014-02-25T06:39:06.942022+00:00 heroku[web.1]: Stopping process with SIGKILL
2014-02-25T06:39:08.171526+00:00 heroku[web.1]: Process exited with status 137
2014-02-25T06:39:08.179601+00:00 heroku[web.1]: State changed from starting to crashed
2014-02-25T06:39:11.684273+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/enews/ host=barkha-writes.herokuapp.com request_id=65c9cb99-c629-46b5-9dd7-fed5c9bfdc13 fwd="24.240.36.207" dyno= connect= service= status=503 bytes=
Change your Procfile according to documentation given on Heroku.
Also add gunicorn to INSTALLED_APPS in settings.py
your Procfile look like
"web: gunicorn path/of/yourwsgit/file.wsgi"
your wsgi script look like this.
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())