Deploy Django Docker Compose to Heroku

geetechlab

Gerard Nwazuruoke

Posted on January 6, 2023

Deploy Django Docker Compose to Heroku

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This will build and push your Docker image to Heroku, and start the app.
Happy coding...

💖 💪 🙅 🚩
geetechlab
Gerard Nwazuruoke

Posted on January 6, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related