You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Docker将Django Cookiecutter项目上传并部署至Heroku?

Deploying Your Django Cookiecutter App to Heroku (with Docker)

Hey there, first off—great job getting this far as a first-timer with Django, Docker, and Heroku! Let's walk through this step-by-step since you've already got the basics set up (Heroku app, domain, PostgreSQL, CLI installed).

Open your terminal, navigate to your project root, and run these commands to connect your local repo to your Heroku app:

# Log in to Heroku (a browser window will pop up if you haven't already authenticated)
heroku login

# Associate your local project with your Heroku app (replace with your actual app name)
heroku git:remote -a your-heroku-app-name

2. Configure Critical Environment Variables

Heroku needs your app's environment variables to run properly—let's set the most important ones first. Run these one by one (replace placeholders with your actual values):

# Set DEBUG to False (non-negotiable for production!)
heroku config:set DEBUG=False

# Set ALLOWED_HOSTS to include your custom domain and Heroku's default app domain
heroku config:set ALLOWED_HOSTS=your-custom-domain.com,your-heroku-app-name.herokuapp.com

# Generate a secure SECRET_KEY first (run this locally to get a value, then paste it below)
# Local command to generate: python -c "import secrets; print(secrets.token_urlsafe(50))"
heroku config:set SECRET_KEY=your-generated-secure-secret-key

# Heroku auto-sets DATABASE_URL when you add PostgreSQL, but double-check it exists
heroku config:get DATABASE_URL

Add any other environment variables your app needs (like API keys) using the same heroku config:set KEY=VALUE format.

3. Deploy with Docker

Since your project uses Docker, we'll use Heroku's Container Registry to push your production image:

# Log in to Heroku's Container Registry
heroku container:login

# Build and push your Docker image for the web process
heroku container:push web

# Release the image to make it live on Heroku
heroku container:release web

Note: If your Cookiecutter setup uses a Dockerfile.prod for production, specify it explicitly with heroku container:push web --file Dockerfile.prod if needed.

4. Run Database Migrations

Your app is deployed, but the database still needs to be initialized. Run migrations with:

heroku run python manage.py migrate

If you need an admin account for your app, create one with:

heroku run python manage.py createsuperuser

5. Verify Deployment & SSL

  • Wait 2-3 minutes for Heroku to provision the SSL certificate (included with the Hobby plan), then visit your custom domain—you should see your app running over HTTPS.
  • If something breaks, check the real-time logs to debug:
    heroku logs --tail
    

Common issues here are missing environment variables, incorrect ALLOWED_HOSTS, or migration errors.

Quick Pro Tips for First-Timers

  • Test your production setup locally first! Use Docker Compose to run the production config: docker-compose -f production.yml up—this catches issues before deploying.
  • Never hardcode secrets or sensitive values in your code—always use environment variables.
  • If you make changes to your app, repeat steps 3 (push/release) and 4 (migrations if needed) to update the deployment.

内容的提问来源于stack exchange,提问作者Diggytael

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:06:15