Docker18.02 + Django2.0 + MySQL + p
利用Docker建立 Django 2.0 + MySQL + phpmyadmin 的開發環境
新建一個資料夾 如 django-web02
Dockerfile
FROM python:3 ENV PYTHONUNBUFFERED 1 RUN mkdir /code WORKDIR /code ADD requirements.txt /code/ RUN pip install -r requirements.txt ADD . /code/
requirements.txt
Django PyMySQL pytz
docker-compose.yml
version: "3.6"services: db: image: mysql ports: - "3306:3306" environment: - MYSQL_ROOT_PASSWORD=123456 - MYSQL_USER=admin - MYSQL_PASSWORD=123456 - MYSQL_DATABASE=testdb web: build: . command: python manage.py runserver 0.0.0.0:8000 volumes: - .:/code ports: - "8000:8000" depends_on: - db phpmyadmin: image: phpmyadmin/phpmyadmin environment: - PMA_ARBITRARY=1 restart: always ports: - "8082:80" volumes: - /sessions
目錄結構
uic@ubuntu:~/software/django-web02$ tree django-web02 ├── docker-compose.yml ├── Dockerfile └── requirements.txt
docker-compose build
uic@ubuntu:~/software/django-web02$ docker-compose build db uses an image, skipping Building web Step 1/7 : FROM python:3 ---> 500d0dca385d Step 2/7 : ENV PYTHONUNBUFFERED 1 ---> Using cache ---> 36c33c71b7b9 Step 3/7 : RUN mkdir /code ---> Using cache ---> 9cbc7c98d009 Step 4/7 : WORKDIR /code ---> Using cache ---> 10f5a9b82917 Step 5/7 : ADD requirements.txt /code/ ---> Using cache ---> db215e28a822 Step 6/7 : RUN pip install -r requirements.txt ---> Using cache ---> f4562d485068 Step 7/7 : ADD . /code/ ---> Using cache ---> 4a223f68b872
docker-compose run 命令啟動一個 Django 應用
uic@ubuntu:~/software/django-web02$ docker-compose run web django-admin.py startproject django_example . Starting djangoweb02_db_1 ... done uic@ubuntu:~/software/django-web02$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES05864082566d mysql "docker-entrypoint.s…" 2 minutes ago Up 21 seconds 0.0.0.0:3306->3306/tcp djangoweb02_db_1 uic@ubuntu:~/software/django-web02$
目錄結構
. ├── django_example│ ├── __init__.py│ ├── settings.py│ ├── urls.py│ └── wsgi.py├── docker-compose.yml├── Dockerfile├── manage.py└── requirements.txt
新建立的 django_example 資料夾是 root, 需要 更改 的目錄許可權
uic@ubuntu:~/software/django-web02$ ll total 28drwxrwxr-x 3 uic uic 4096 Mar 23 05:15 ./ drwxrwxr-x 7 uic uic 4096 Mar 23 01:38 ../ drwxr-xr-x 2 root root 4096 Mar 23 05:15 django_example/ -rw-rw-r-- 1 uic uic 700 Mar 23 03:23 docker-compose.yml -rw-rw-r-- 1 uic uic 150 Mar 23 03:16 Dockerfile -rwxr-xr-x 1 root root 546 Mar 23 05:15 manage.py* -rw-rw-r-- 1 uic uic 23 Mar 23 03:16 requirements.txt uic@ubuntu:~/software/django-web02$ sudo chown -R $USER:$USER django_example/ uic@ubuntu:~/software/django-web02$ ll total 28drwxrwxr-x 3 uic uic 4096 Mar 23 05:15 ./ drwxrwxr-x 7 uic uic 4096 Mar 23 01:38 ../ drwxr-xr-x 2 uic uic 4096 Mar 23 05:15 django_example/ -rw-rw-r-- 1 uic uic 700 Mar 23 03:23 docker-compose.yml -rw-rw-r-- 1 uic uic 150 Mar 23 03:16 Dockerfile -rwxr-xr-x 1 root root 546 Mar 23 05:15 manage.py* -rw-rw-r-- 1 uic uic 23 Mar 23 03:16 requirements.txt
docker-compose up -d 啟動
uic@ubuntu:~/software/django-web02$ docker-compose up -d djangoweb02_db_1 is up-to-date Creating djangoweb02_phpmyadmin_1 ... done Creating djangoweb02_web_1 ... done uic@ubuntu:~/software/django-web02$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES66151fe2bc0b phpmyadmin/phpmyadmin "/run.sh phpmyadmin" 5 seconds ago Up 4 seconds 9000/tcp, 0.0.0.0:8082->80/tcp djangoweb02_phpmyadmin_1 bee5497883fa djangoweb02_web "python manage.py ru…" 5 seconds ago Up 4 seconds 0.0.0.0:8000->8000/tcp djangoweb02_web_105864082566d mysql "docker-entrypoint.s…" 14 minutes ago Up 12 minutes 0.0.0.0:3306->3306/tcp djangoweb02_db_1 uic@ubuntu:~/software/django-web02$
檢視頁面
phpmyadmin
phpmyadmin.png
django.png
需要在 django 中的 settings.py 設定,允許任意 host 訪問
ALLOWED_HOSTS = ["*"]
django2.0.png
建立 一個 test_app
uic@ubuntu:~/software/django-web02$ docker-compose run web python manage.py startapp test_app Starting djangoweb02_db_1 ... done
目錄結構
. ├── db.sqlite3├── django_example│ ├── __init__.py│ ├── __pycache__│ │ ├── __init__.cpython-36.pyc│ │ ├── settings.cpython-36.pyc│ │ ├── urls.cpython-36.pyc│ │ └── wsgi.cpython-36.pyc│ ├── settings.py│ ├── urls.py│ └── wsgi.py├── docker-compose.yml├── Dockerfile├── manage.py├── requirements.txt└── test_app ├── admin.py ├── apps.py ├── __init__.py ├── migrations │ └── __init__.py ├── models.py ├── __pycache__ │ ├── __init__.cpython-36.pyc │ └── views.cpython-36.pyc ├── tests.py └── views.py
在 django_example/urls.py 新增
urlpatterns += [ path('app/',views.app,name='app'), ]
在 test_app/views.py 新增
from django.shortcuts import renderfrom django.http import HttpResponse# Create your views here.def app(request): response_html = "Hello Django 2.0." return HttpResponse(response_html)
檢視結果
test_app.png
作者:2010jing
連結:
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2558/viewspace-2810844/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 不一樣的django2.0筆記Django筆記
- Django2.0(一)Django框架開發流程Django框架
- Django2.0開發-前端導航欄切換activate改變Django前端
- 尚矽谷Mysql高階day5(p29-30)MySql
- P3942&P2279&P2899&P2016
- About Enqueue:P1/P2/P3ENQ
- MYSQL 錯誤 :Out of resources when opening file './datagather/mx_domain#P#p178.MYD' (Errcode: 24) 解決辦法...MySqlAI
- 從free(p)到delete[]p (轉)delete
- 關於MySQL日誌,我與阿里P9都聊了些啥?MySql阿里
- 等待事件P1 P2 P3含義事件
- P2P技術(一):NAT
- decltype(p)和decltype(*p)的區別
- P2P程式設計(十)程式設計
- P2P技術詳解(一):NAT詳解——詳細原理、P2P簡介(轉)
- SAP中MRP型別 P1,P2,P3,P4,PD的區別型別
- IBM P550/P570/P520/P52A 整機促銷開始啦!IBM
- P2P技術(2)——NAT穿透穿透
- Napster的P2P系統
- P2P行業研究報告行業
- 聯發科P30和P60區別對比 Helio P60和P30哪個好?
- MySQL 可重複讀,差點就我背上了一個 P0 事故!MySql
- 現貨供應IBM P550 P570 P520 S85 P615 P660 P630 P650 P690 F80--廣東勵康020-85551263-101IBM
- oracle等待引數含義_v$session_wait_p1_p2_p3OracleSessionAI
- 安捷財富P2P平臺
- P2P平臺很賺錢麼?
- P2P 殭屍網路入門
- 520,p550,p570 HMC IP
- 雲協議範例一 P2P協議
- redis——P2:對P1的思考Redis
- IMO 2024 (P1, P5)
- 華為P30與華為P30 Pro拍照評測:華為P30/P30 Pro拍照怎麼樣?
- 聯發科Helio P23和聯發科P22區別對比 聯發科P22和P23哪個好
- HTML p元素HTML
- HTML <p> 元素HTML
- p_getusertabsqlSQL
- p_gettabsqlSQL
- p_getusertabdata
- p_gettabdata