To deploy a Django project that uses Docker Compose to Heroku, you will need to make a few changes to your project.
First, create a Procfile
at the root of your project directory and add the following line:
web: gunicorn myproject.wsgi
This tells Heroku to use the gunicorn
web server to run your Django app. Replace myproject
with the name of your Django project.
Next, create a requirements.txt
file that lists the Python packages that your app depends on. You can generate this file by running the following command:
$ pip freeze > requirements.txt
You will also need to modify your Dockerfile
to use the gunicorn
web server instead of the Django development server. Here is an example Dockerfile
that you can use:
FROM python:3.8
ENV PYTHONUNBUFFERED 1
RUN mkdir /app
WORKDIR /app
COPY requirements.txt /app/
RUN pip install -r requirements.txt
COPY . /app/
CMD gunicorn myproject.wsgi --bind 0.0.0.0:$PORT
Make sure to replace myproject
with the name of your Django project.
Finally, you will need to create a heroku.yml
file at the root of your project directory with the following contents:
build:
docker:
web: Dockerfile
heroku:
app: myapp
stack: container
run:
web: docker-compose up
Replace myapp
with the name of your Heroku app.
To deploy your app to Heroku, make sure you have the Heroku CLI installed, and then run the following commands:
$ heroku login
$ git init
$ git add .
$ git commit -m "Initial commit"
$ heroku create
$ git push heroku main
This will build and push your Docker image to Heroku, and start the app.
Happy coding...
Top comments (1)
Great post, thank you for sharing!