利用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
链接:https://www.jianshu.com/p/0fe67286cafc