├── .gitignore ├── Dockerfile ├── README.md ├── docker-compose.yml ├── docker_remote_interpreter ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py ├── manage.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | *.idea 6 | 7 | # C extensions 8 | *.so 9 | 10 | # Distribution / packaging 11 | .Python 12 | env/ 13 | build/ 14 | develop-eggs/ 15 | dist/ 16 | downloads/ 17 | eggs/ 18 | .eggs/ 19 | lib/ 20 | lib64/ 21 | parts/ 22 | sdist/ 23 | var/ 24 | wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .coverage 43 | .coverage.* 44 | .cache 45 | nosetests.xml 46 | coverage.xml 47 | *.cover 48 | .hypothesis/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | 58 | # Flask stuff: 59 | instance/ 60 | .webassets-cache 61 | 62 | # Scrapy stuff: 63 | .scrapy 64 | 65 | # Sphinx documentation 66 | docs/_build/ 67 | 68 | # PyBuilder 69 | target/ 70 | 71 | # Jupyter Notebook 72 | .ipynb_checkpoints 73 | 74 | # pyenv 75 | .python-version 76 | 77 | # celery beat schedule file 78 | celerybeat-schedule 79 | 80 | # SageMath parsed files 81 | *.sage.py 82 | 83 | # dotenv 84 | .env 85 | 86 | # virtualenv 87 | .venv 88 | venv/ 89 | ENV/ 90 | 91 | # Spyder project settings 92 | .spyderproject 93 | .spyproject 94 | 95 | # Rope project settings 96 | .ropeproject 97 | 98 | # mkdocs documentation 99 | /site 100 | 101 | # mypy 102 | .mypy_cache/ 103 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.6.4 2 | LABEL maintainer twtrubiks 3 | ENV PYTHONUNBUFFERED 1 4 | RUN mkdir /docker_remote_interpreter 5 | WORKDIR /docker_remote_interpreter 6 | COPY . /docker_remote_interpreter/ 7 | RUN pip install -r requirements.txt -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # docker-remote-interpreter 2 | 3 | * [Youtube Tutorial - docker-remote-interpreter-using-pycharm](https://youtu.be/bOUGyitONQ0) 4 | 5 | ## 前言 6 | 7 | 這篇文章主要是要教大家如何使用 Pycharm remote interpreter ( Using Docker ),這邊需要注意的是, 8 | 9 | 要使用此功能,你的 Pycharm 必須是 **Professional** 的版本。 10 | 11 | 為什麼我會突然介紹這個呢?原因是之前我在 windows 上用 Anaconda 安裝套件 ( Channels ) 時, 12 | 13 | 一直有問題,於是我就想說不然用 docker 來解決,當然同時也需要 debug 阿:weary: 14 | 15 | 於是就有了這篇文章的教學:smiley: 16 | 17 | ## 教學 18 | 19 | 首先,要先對 docker 有一些基本的認識,如果你對 docker 非常的陌生,可以參考我之前寫的文章 20 | 21 | * [Docker 基本教學 - 從無到有 Docker-Beginners-Guide 教你用 Docker 建立 Django + PostgreSQL 📝](https://github.com/twtrubiks/docker-tutorial) 22 | 23 | 基本上,這篇的範例就是用上面文章的範例下去教學的:smirk: 24 | 25 | 我主要是參考 [Configuring Remote Interpreter via DockerCompose](https://www.jetbrains.com/help/pycharm/using-docker-compose-as-a-remote-interpreter-1.html) 這篇文章,但步驟不一樣相同,接下來的教學, 26 | 27 | 使用的環境是 Win10,如果你是 MAC 用戶,也是可以使用 ( 只是手邊目前沒有 MAC,但確定可以使用 ,方法大同小異 )。 28 | 29 | 先設定,將 Expose daemon... 這個打勾 30 | 31 | ![alt tag](https://i.imgur.com/R3ot2a9.png) 32 | 33 | File -> Settings ( 或是快捷鍵`Ctrl+Alt+S`) 設定以下參數 34 | 35 | ![alt tag](https://i.imgur.com/mttgpFu.png) 36 | 37 | 再找到 Project Interpreter,選擇 Add Remote 38 | 39 | ![alt tag](https://i.imgur.com/zl63LUr.png) 40 | 41 | 接下來,請選擇 Docker Compose (這邊使用 docker-compose 來建立環境 ), 42 | 43 | 其他參數,理論上預設就會幫你帶入了, 44 | 45 | ![alt tag](https://i.imgur.com/kaJF9bw.png) 46 | 47 | 先來介紹一下資料夾內的東西, 48 | 49 | [Dockerfile](https://github.com/twtrubiks/docker-remote-interpreter/blob/master/Dockerfile) 50 | 51 | 基本上就是將目錄底下的檔案複製到 docker 容器裡面以及安裝 [requirements.txt](https://github.com/twtrubiks/docker-remote-interpreter/blob/master/requirements.txt)。 52 | 53 | [docker-compose.yml](https://github.com/twtrubiks/docker-remote-interpreter/blob/master/docker-compose.yml) 54 | 55 | 裡面也很簡單,就是一個 postgres database,然後一個 app 啟動 django。 56 | 57 | docker_remote_interpreter/[settings.py](https://github.com/twtrubiks/docker-remote-interpreter/blob/master/docker_remote_interpreter/settings.py) 58 | 59 | `settings.py` 裡面必須設定兩個東西,第一個是 `ALLOWED_HOSTS` 這部分, 60 | 61 | ```python 62 | ALLOWED_HOSTS = ["*"] 63 | ``` 64 | 65 | ( 這邊提醒一下大家,設定 `*` 只是為了測試方便而已 )。 66 | 67 | 第二個部分是將你的 db 修改成 postgres 68 | 69 | ```python 70 | DATABASES = { 71 | 'default': { 72 | 'ENGINE': 'django.db.backends.postgresql_psycopg2', 73 | 'NAME': 'postgres', 74 | 'USER': 'postgres', 75 | 'PASSWORD': 'password123', 76 | 'HOST': 'db', 77 | 'PORT': 5432, 78 | } 79 | } 80 | ``` 81 | 82 | 接下來就是等,可能需要等一小段時間 ( 尤其是第一次,先去泡杯咖啡吧:relaxed: ) 83 | 84 | ![alt tag](https://i.imgur.com/k3jVYKb.png) 85 | 86 | 當他跑完之後,按 ok ,會看到下圖 87 | 88 | ![alt tag](https://i.imgur.com/gFqUXDD.png) 89 | 90 | 右下角 91 | 92 | ![alt tag](https://i.imgur.com/d0WHzT1.png) 93 | 94 | 等全部都執行完之後 95 | 96 | ![alt tag](https://i.imgur.com/iWnjI7t.png) 97 | 98 | 填入 `0.0.0.0`,以這個範例來講, port 就是 8000 99 | 100 | ![alt tag](https://i.imgur.com/y82Dvo3.png) 101 | 102 | 直接開始 debug 103 | 104 | ![alt tag](https://i.imgur.com/YbsC8yt.png) 105 | 106 | 啟動成功 107 | 108 | ![alt tag](https://i.imgur.com/CSq5oOl.png) 109 | 110 | 可以使用 `docker ps` 確認 111 | 112 | ![alt tag](https://i.imgur.com/8zeSK9s.png) 113 | 114 | 最後我們直接進去容器 migrate 115 | 116 | ```cmd 117 | python manage.py makemigrations 118 | python manage.py migrate 119 | ``` 120 | 121 | ![alt tag](https://i.imgur.com/CWJIHD6.png) 122 | 123 | 直接瀏覽,[http://localhost:8000/](http://localhost:8000/) ( 有時候可能會是 [http://0.0.0.0:8000/](http://0.0.0.0:8000/),要看你電腦的 host 設定 ) 124 | 125 | ![alt tag](https://i.imgur.com/A0kF1wt.png) 126 | 127 | 觀看容器內的 log ( 我們的確有成功連線上 ) 128 | 129 | ![alt tag](https://i.imgur.com/HbejCAg.png) 130 | 131 | 到這邊就完成了:smile: 132 | 133 | ## 後記 134 | 135 | 整體來看,相信大家應該會覺得用 Pycharm 設定 remote-interpreter 蠻簡單的,但他也有缺點,就是如果你今天 136 | 137 | 需要增加一個套件,你的 docker 就需要重新 build。 138 | 139 | 可能有人會問,有沒有 vscode 的 remote-interpreter 教學呢 ? 這是個好問題,我後來也有嘗試用 vscode 設定,但 140 | 141 | 設定一直失敗,如果有人成功,麻煩可以再指點一下,我會把教學補進文章裡面:grin: 142 | 143 | ## 執行環境 144 | 145 | * Python 3.6.4 146 | * Win10 147 | 148 | ## Reference 149 | 150 | * [Configuring Remote Interpreter via DockerCompose](https://www.jetbrains.com/help/pycharm/using-docker-compose-as-a-remote-interpreter-1.html) 151 | 152 | ## Donation 153 | 154 | 文章都是我自己研究內化後原創,如果有幫助到您,也想鼓勵我的話,歡迎請我喝一杯咖啡:laughing: 155 | 156 | ![alt tag](https://i.imgur.com/LRct9xa.png) 157 | 158 | [贊助者付款](https://payment.opay.tw/Broadcaster/Donate/9E47FDEF85ABE383A0F5FC6A218606F8) 159 | 160 | ## License 161 | 162 | MIT licens -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | 4 | db: 5 | container_name: 'postgres' 6 | image: postgres 7 | environment: 8 | POSTGRES_PASSWORD: password123 9 | ports: 10 | - "5432:5432" 11 | volumes: 12 | - pgdata:/var/lib/postgresql/data/ 13 | 14 | app: 15 | build: . 16 | command: bash -c "python manage.py runserver 0.0.0.0:8000" 17 | restart: always 18 | ports: 19 | - "8000:8000" 20 | volumes: 21 | - .:/docker_remote_interpreter 22 | depends_on: 23 | - db 24 | 25 | volumes: 26 | pgdata: 27 | -------------------------------------------------------------------------------- /docker_remote_interpreter/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/twtrubiks/docker-remote-interpreter/f16166cfb3b5d3de6c59386accf5ca61fa5ffded/docker_remote_interpreter/__init__.py -------------------------------------------------------------------------------- /docker_remote_interpreter/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for docker_remote_interpreter project. 3 | 4 | Generated by 'django-admin startproject' using Django 1.11.3. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.11/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/1.11/ref/settings/ 11 | """ 12 | 13 | import os 14 | 15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'fs5v(sv_k3nn5uq_0)5n@@ap#cyj@$3lkp9gj6@505lweh9n)s' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = ["*"] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | ] 41 | 42 | MIDDLEWARE = [ 43 | 'django.middleware.security.SecurityMiddleware', 44 | 'django.contrib.sessions.middleware.SessionMiddleware', 45 | 'django.middleware.common.CommonMiddleware', 46 | 'django.middleware.csrf.CsrfViewMiddleware', 47 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 48 | 'django.contrib.messages.middleware.MessageMiddleware', 49 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 50 | ] 51 | 52 | ROOT_URLCONF = 'docker_remote_interpreter.urls' 53 | 54 | TEMPLATES = [ 55 | { 56 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 57 | 'DIRS': [os.path.join(BASE_DIR, 'templates')] 58 | , 59 | 'APP_DIRS': True, 60 | 'OPTIONS': { 61 | 'context_processors': [ 62 | 'django.template.context_processors.debug', 63 | 'django.template.context_processors.request', 64 | 'django.contrib.auth.context_processors.auth', 65 | 'django.contrib.messages.context_processors.messages', 66 | ], 67 | }, 68 | }, 69 | ] 70 | 71 | WSGI_APPLICATION = 'docker_remote_interpreter.wsgi.application' 72 | 73 | 74 | # Database 75 | # https://docs.djangoproject.com/en/1.11/ref/settings/#databases 76 | 77 | # DATABASES = { 78 | # 'default': { 79 | # 'ENGINE': 'django.db.backends.sqlite3', 80 | # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 81 | # } 82 | # } 83 | 84 | DATABASES = { 85 | 'default': { 86 | 'ENGINE': 'django.db.backends.postgresql_psycopg2', 87 | 'NAME': 'postgres', 88 | 'USER': 'postgres', 89 | 'PASSWORD': 'password123', 90 | 'HOST': 'db', 91 | 'PORT': 5432, 92 | } 93 | } 94 | 95 | # Password validation 96 | # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators 97 | 98 | AUTH_PASSWORD_VALIDATORS = [ 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 101 | }, 102 | { 103 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 104 | }, 105 | { 106 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 107 | }, 108 | { 109 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 110 | }, 111 | ] 112 | 113 | 114 | # Internationalization 115 | # https://docs.djangoproject.com/en/1.11/topics/i18n/ 116 | 117 | LANGUAGE_CODE = 'en-us' 118 | 119 | TIME_ZONE = 'UTC' 120 | 121 | USE_I18N = True 122 | 123 | USE_L10N = True 124 | 125 | USE_TZ = True 126 | 127 | 128 | # Static files (CSS, JavaScript, Images) 129 | # https://docs.djangoproject.com/en/1.11/howto/static-files/ 130 | 131 | STATIC_URL = '/static/' 132 | -------------------------------------------------------------------------------- /docker_remote_interpreter/urls.py: -------------------------------------------------------------------------------- 1 | """docker_remote_interpreter URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/1.11/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.conf.urls import url, include 14 | 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) 15 | """ 16 | from django.conf.urls import url 17 | from django.contrib import admin 18 | 19 | urlpatterns = [ 20 | url(r'^admin/', admin.site.urls), 21 | ] 22 | -------------------------------------------------------------------------------- /docker_remote_interpreter/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for docker_remote_interpreter project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "docker_remote_interpreter.settings") 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | import sys 4 | 5 | if __name__ == "__main__": 6 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "docker_remote_interpreter.settings") 7 | try: 8 | from django.core.management import execute_from_command_line 9 | except ImportError: 10 | # The above import may fail for some other reason. Ensure that the 11 | # issue is really that Django is missing to avoid masking other 12 | # exceptions on Python 2. 13 | try: 14 | import django 15 | except ImportError: 16 | raise ImportError( 17 | "Couldn't import Django. Are you sure it's installed and " 18 | "available on your PYTHONPATH environment variable? Did you " 19 | "forget to activate a virtual environment?" 20 | ) 21 | raise 22 | execute_from_command_line(sys.argv) 23 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Django==2.0.4 2 | psycopg2==2.7.4 --------------------------------------------------------------------------------