{"id":20619763,"url":"https://github.com/twtrubiks/deploying_django_to_heroku_with_docker","last_synced_at":"2025-07-30T11:10:18.578Z","repository":{"id":84518916,"uuid":"153440943","full_name":"twtrubiks/Deploying_Django_To_Heroku_With_Docker","owner":"twtrubiks","description":"How Deploying Django To Heroku With Docker ( tutorial )","archived":false,"fork":false,"pushed_at":"2018-10-17T12:12:00.000Z","size":11,"stargazers_count":23,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-15T12:12:26.729Z","etag":null,"topics":["deploying","django","docker","heroku"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/twtrubiks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-17T10:48:28.000Z","updated_at":"2022-04-07T20:57:25.000Z","dependencies_parsed_at":"2023-03-02T04:30:27.332Z","dependency_job_id":null,"html_url":"https://github.com/twtrubiks/Deploying_Django_To_Heroku_With_Docker","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2FDeploying_Django_To_Heroku_With_Docker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2FDeploying_Django_To_Heroku_With_Docker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2FDeploying_Django_To_Heroku_With_Docker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2FDeploying_Django_To_Heroku_With_Docker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/twtrubiks","download_url":"https://codeload.github.com/twtrubiks/Deploying_Django_To_Heroku_With_Docker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249067779,"owners_count":21207396,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deploying","django","docker","heroku"],"created_at":"2024-11-16T12:12:27.870Z","updated_at":"2025-04-15T12:13:03.588Z","avatar_url":"https://github.com/twtrubiks.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Deploying_Django_To_Heroku_With_Docker\n\n教你如何 Deploying Django To Heroku With Docker\n\n* [Youtube Tutorial - Deploying Django To Heroku With Docker](https://youtu.be/2Pl6iALuJgQ)\n\n## 簡介\n\n之前已經教學過大家如何將 Django 以及 Flask 佈署到 Heroku 上了，如果你還沒看過或不懂，請先參考以下兩篇，\n\n* [Deploying a Django App To Heroku Tutorial](https://github.com/twtrubiks/Deploying_Django_To_Heroku_Tutorial)\n\n* [Deploying a Flask App To Heroku Tutorial](https://github.com/twtrubiks/Deploying-Flask-To-Heroku)\n\n那今天我要更進階教大家使用 Docker 將 Django 佈署到 Heroku，如果你不懂 Docker，也請先閱讀以下文章\n\n* [Docker 基本教學 - 從無到有 Docker-Beginners-Guide](https://github.com/twtrubiks/docker-tutorial)\n\n這次使用 Docker 將 Django 佈署到 Heroku 上遇到很雷的事情 :cry:\n\n讓我們看下去:smiley:\n\n## 教學\n\n請先確認你的電腦安裝了 [Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli) ( 如果你有曾經部署過，應該都有安裝了 )，\n\n另一個要安裝的是 [Docker](https://docs.docker.com/) ( 現在要下載 Docker 必須要登入，簡單去註冊一個吧 :wink:)。\n\n以上都做好了之後，請先確認使否已經登入 Docker 以及 Heroku，\n\n登入 Docker，\n\n```cmd\ndocker login\n```\n\n登入 Heroku，\n\n```cmd\nheroku login\n```\n\n接著 [Logging in to the registry](https://devcenter.heroku.com/articles/container-registry-and-runtime#logging-in-to-the-registry)，執行以下指令，\n\n```cmd\nheroku container:login\n```\n\n( 這邊如果你是 windows 用戶請注意，請使用 **內建的命令提示字元** 執行，像我如果使用 cmder 執行會錯誤，很重要 :exclamation:)\n\n( 另外，當你執行 `heroku login` 時，如果你已經登入過，然後再次執行時，請重新輸入一次，不要使用 Ctrl+C 中斷 ，不然也會導致錯誤 )\n\n以上兩點很重要，我就是被這個雷到:sob:\n\n我們先在本地端使用 docker 測試，確認測試沒問題，我們再部署到 Heroku 上，\n\n我們使用的範例是我之前寫的這篇 [Docker 基本教學 - 從無到有 Docker-Beginners-Guide](https://github.com/twtrubiks/docker-tutorial)，\n\n然後有一點要注意的是，Heroku 的佈署並不支援 docker-compose 的方式 :sob: ( 這個我們後面再進一步說明 )，\n\n現在我們只使用一個 image ( 也可以說一個 container )。\n\n先來看 [Dockerfile](https://github.com/twtrubiks/Deploying_Django_To_Heroku_With_Docker/blob/master/Dockerfile)，\n\n```Dockerfile\nFROM python:3.6.2\nLABEL maintainer twtrubiks\nENV PYTHONUNBUFFERED 1\nRUN mkdir /docker_api\nWORKDIR /docker_api\nCOPY . /docker_api/\nRUN pip install -r requirements.txt\n\n# Run the app.  CMD is required to run on Heroku\n# $PORT is set by Heroku\nCMD python manage.py runserver 0.0.0.0:$PORT\n```\n\n比較要注意的就是最後一行，首先，這邊我們就先簡單的使用 `python manage.py runserver`，\n\n反正現在已經在 Docker 裡面了，如果你未來想要加上 gunicorn 或是 uwsgi，請再自行修改:smirk:\n\n另外一點是 `$PORT`，這個是由 Heroku 設定的。\n\n如果佈署到 Heroku 上，我們會使用 [Heroku Postgres](https://www.heroku.com/postgres)，\n\n可以參考 [Pushing your containers to Heroku](https://devcenter.heroku.com/articles/local-development-with-docker-compose#pushing-your-containers-to-heroku) 這邊的說明，\n\n```text\nUse Heroku add-ons in production\n    * For local development: use official Docker images, such as Postgres and Redis.\n    * For staging and production: use Heroku add-ons, such as Heroku Postgres and Heroku Redis.\n```\n\n[settings](https://github.com/twtrubiks/Deploying_Django_To_Heroku_With_Docker/blob/master/django_rest_framework_tutorial/settings.py) 中設定 db 的部分如下，\n\n```python\nDATABASES = {\n    'default': {\n        'ENGINE': 'django.db.backends.postgresql_psycopg2',\n        'NAME': os.environ.get('DATABASE_NAME'),\n        'USER': os.environ.get('DATABASE_USER'),\n        'PASSWORD': os.environ.get('DATABASE_PASSWORD'),\n        'HOST': os.environ.get('DATABASE_HOST'),\n        'PORT': os.environ.get('DATABASE_PORT'),\n    }\n}\n```\n\n到時候這些環境變數會設定到 Heroku 的後台。\n\n接著先到 Heroku 上建立一個 app，建立完 app 之後，再去建立 [Heroku Postgres](https://www.heroku.com/postgres)，\n\n可參考之前我的文章 [如何在 heroku 上使用 database](https://github.com/twtrubiks/Deploying-Flask-To-Heroku#%E5%A6%82%E4%BD%95%E5%9C%A8-heroku-%E4%B8%8A%E4%BD%BF%E7%94%A8-database)。\n\n建立完之後，你會看到 db 的連線資訊，\n\n![alt tag](https://i.imgur.com/TsgF88a.png)\n\n這些連線資訊可以先設定到 Heroku 的後台，\n\n![alt tag](https://i.imgur.com/iZRaqRH.png)\n\n那接著因為我們希望先在本地端執行，但我們需要 Heroku 上的環境變數才可以執行，那該怎麼辦:question:\n\nHeroku 有提供在 local 環境中設定 Heroku 上的環境變數的方法，使用方法就是建立一個 `.env` 檔案\n\n( 可參考[setting-multiple-environment-variables](https://devcenter.heroku.com/articles/container-registry-and-runtime#setting-multiple-environment-variables) )，\n\n然後將環境變數設定在裡面 ( win 用戶請在 terminal 中輸入 `touch .env` 建立檔案 )。\n\n[.env](https://github.com/twtrubiks/Deploying_Django_To_Heroku_With_Docker/blob/master/.env) 內容大致如下，\n\n```text\nPORT=8000\nDATABASE_NAME=\u003cyour heroku db name\u003e\nDATABASE_USER=\u003cyour heroku db user\u003e\nDATABASE_PASSWORD=\u003cyour heroku db password\u003e\nDATABASE_HOST=\u003cyour heroku db host\u003e\nDATABASE_PORT=5432\n```\n\n一般來說 `.env` 應該要被寫入 `.dockerignore` 裡面，但這邊方便大家測試，我就不寫進去了。\n\n到這邊我們終於可以在本地端啟動 Docker 來測試了:satisfied:\n\n( db 的部分直接使用 [Heroku Postgres](https://www.heroku.com/postgres)，所以我才將連線資訊都寫進 `.env` 裡面 )。\n\n接下來就是 Docker 指令而已，首先，\n\n先 build image ( 使用命令提示字元切換到 `Deploying_Django_To_Heroku_With_Docker` 底下 )，\n\n```cmd\ndocker build --tag web_image .\n```\n\n![alt tag](https://i.imgur.com/VCZkTBl.png)\n\n如果順利 build 成功，使用下列指令會看到 web_image 這個 image，\n\n```cmd\ndocker images\n```\n\n![alt tag](https://i.imgur.com/x3rADSF.png)\n\n最後就是把它啟動，\n\n```cmd\ndocker run -p 8000:8000 --env-file .env web_image\n```\n\n`.env` 前面說明過了，主要是方便 local environment test，可參考 [Setting multiple environment variables](https://devcenter.heroku.com/articles/container-registry-and-runtime#setting-multiple-environment-variables)，\n\n![alt tag](https://i.imgur.com/vAztk85.png)\n\n接下來，我們進去 container 中執行 db migrate，\n\n```cmd\ndocker ps\ndocker exec -it xxxxx bash\n```\n\n![alt tag](https://i.imgur.com/bbyYWEi.png)\n\n然後再 db migrate 以及建立 superuser ( 這邊的 db 是使用 Heroku 上的 )，\n\n```cmd\npython manage.py makemigrations musics\npython manage.py migrate\npython manage.py createsuperuser\n```\n\n建立完之後，可以瀏覽 [http://127.0.0.1:8000/api](http://127.0.0.1:8000/api)，\n\n如果正確無誤，應該會看到下圖，\n\n![alt tag](https://i.imgur.com/Rqk4kPK.png)\n\n## Deploying Django To Heroku With Docker\n\n既然在本地端測試沒問題，那現在我們就可以佈署到 Heroku 了，步驟只有幾個，\n\n首先，使用命令提示字元切換到 `Deploying_Django_To_Heroku_With_Docker` 底下，\n\n接著使用以下指令，\n\nBuild the image and push to Container Registry\n\n```cmd\nheroku container:push web -a \u003cyour heroku app name\u003e\n```\n\n先說明這個 web，這個 web 的意思其實就像之前說的 Procfile 一樣，像是 [Procfile](https://github.com/twtrubiks/Deploying-Flask-To-Heroku/blob/master/Procfile)。\n\n![alt tag](https://i.imgur.com/QevIz3k.png)\n\n從上圖可以發現，其實他就是抓目錄資料夾底下是否有 [Dockerfile](https://github.com/twtrubiks/Deploying_Django_To_Heroku_With_Docker/blob/master/Dockerfile) 的檔案，\n\n另外，`sdadss` 是我建立的 Heroku app name ( 請使用你自己的 app name， 你建立的 name 不會和我一樣 )。\n\n如果順利執行，接著就是 release the image to your app，\n\n```cmd\nheroku container:release web -a \u003cyour heroku app name\u003e\n```\n\n![alt tag](https://i.imgur.com/wPvWWSf.png)\n\n最後再執行以下指令，\n\n```cmd\nheroku open -a \u003cyour heroku app name\u003e\n```\n\n會自動幫你開一個網頁，如果看到畫面，就代表你成功使用 Docker 佈署了:smiley:\n\n(也可以順便玩玩新增資料是否正常，也就是測試資料庫，這邊不需要再 migrate 了，\n\n因為我們在本機端測試時就已經 migrate 了 )\n\n\n[範例連結](https://sdadss.herokuapp.com/api/)\n\n測試帳號和密碼如下，\n\n```text\naccount : test123\npassword : password123\n```\n\n## 後記\n\n整體來說，這次帶大家嘗試了使用 Docker 佈署 Django 到 Heroku 上，步驟算是還好，就一些小地方要注意，\n\n不過我個人是覺得有些缺點，像是沒辦法使用 docker-compose 的方式佈署，docker-compose 只支持本地端的測試 ，\n\n( 可參考 [Local Development with Docker Compose](https://devcenter.heroku.com/articles/local-development-with-docker-compose) )，\n\n希望有一天可以支援，不然現在如果你有很多的 container ( 也就是使用 docker-compose )，要佈署到 Heroku 上會很麻煩:tired_face:\n\n總之，以上就是帶大家體驗如何使用 Docker 佈署到 Heroku 上:sunglasses:\n\n## 執行環境\n\n* Python 3.6.2\n* Win 10\n\n## Reference\n\n* [Container Registry \u0026 Runtime (Docker Deploys)](https://devcenter.heroku.com/articles/container-registry-and-runtime)\n* [Local Development with Docker Compose](https://devcenter.heroku.com/articles/local-development-with-docker-compose)\n\n## Donation\n\n文章都是我自己研究內化後原創，如果有幫助到您，也想鼓勵我的話，歡迎請我喝一杯咖啡:laughing:\n\n![alt tag](https://i.imgur.com/LRct9xa.png)\n\n[贊助者付款](https://payment.opay.tw/Broadcaster/Donate/9E47FDEF85ABE383A0F5FC6A218606F8)\n\n## License\n\nMIT license\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwtrubiks%2Fdeploying_django_to_heroku_with_docker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftwtrubiks%2Fdeploying_django_to_heroku_with_docker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwtrubiks%2Fdeploying_django_to_heroku_with_docker/lists"}