Follow these instructions to prepare your Heroku app for Docker-based deployment.
- Open a terminal.
- Move into your project directory. For example:
cd ~/Code/example/api
Confirm you’re in the right place by listing the files:
ls
You should see your Dockerfile
and Python app files here.
Authenticate with the Heroku CLI:
heroku login
Ensure the Heroku CLI is ready for Docker-based deployments by installing the required plugin:
heroku plugins:install @heroku-cli/plugin-container-registry
Replace example-api
with the name you want for your app:
heroku create example-api
Replace example-api
with the name of your Heroku app:
heroku git:remote -a example-api
Since we are deploying with Docker, clear any buildpacks set for the Heroku app:
heroku buildpacks:clear --app example-api
Authenticate Docker with Heroku’s container registry:
heroku container:login
Set environment variables required for your Python app. Replace placeholder values with actual data:
heroku config:set FLASK_ENV=production --app example-api
heroku config:set MONGO_URI=mongodb+srv://<username>:<password>@cluster.example.com/dbname --app example-api
heroku config:set ADMIN_USERNAME=admin --app example-api
heroku config:set ADMIN_PASSWORD=securepassword --app example-api
Make sure the app has at least one dyno running for the web
process:
heroku ps:scale web=1 --app example-api
Before pushing the image to Heroku, build it locally and tag it for the Heroku container registry:
docker build -t registry.heroku.com/example-api/web .
Push the Docker image to Heroku and release it to confirm that the app is live:
heroku container:push web --app example-api
heroku container:release web --app example-api
Your Heroku app is now configured for Docker-based deployments. You’ve also pushed a build to Heroku to confirm everything is working. Going forward, you can automate this process using GitHub Actions.
To automate deployments, consider the following example workflows