Docker----搭建python3+django+mysql开发环境

首先需要安装docker,以及docker-compose

参考链接:https://blog.csdn.net/yedongfeng_1314/article/details/82555588

案例:创建django项目,根据docker实现项目的开发

用到的python版本为3.x(python:2.7到2020年就停止维护了,而网上大多数还是此版本的内容)

1. 新建工程项目site,并进入该目录

mkdir site && cd site

2. 在site目录下创建两个目录

mkdir db achievements

3. 编写Dockerfile

vi Dockerfile

并写入以下内容

FROM python:3.5
ENV PYTHONUNBUFFERED 1
RUN echo python -V
RUN mkdir /code
RUN mkdir /code/db
WORKDIR /code
#ADD ./requirements.txt /code/
RUN pip install -i https://pypi.douban.com/simple django
RUN pip3 install PyMySQL
RUN pip3 install mysqlclient
#RUN pip3 install -r requirements.txt
ADD . /code/

4. 编写docker-compose.yml

vi docker-compose.yml

并写入以下内容

db:
  image: mysql
  expose:
    - "3306"
  volumes:
    - ./db:/var/lib/mysql
  environment:
    - MYSQL_DATABASE=achievements
    - MYSQL_ROOT_PASSWORD=root

web:
  build: .
  command: python3 ./achievements/manage.py runserver 0.0.0.0:8000
  volumes:
    - .:/code
  ports:
    - "8000:8000"
  links:
    - db

5. 编写requirements.txt

vi requirements.txt

内容如下:

django

mysqlclient

#pymysql

6. 构建镜像

docker-compose build

[root@VM_0_9_centos site]# docker-compose build
db uses an image, skipping
Building web
Step 1/10 : FROM python:3.5
3.5: Pulling from library/python
6f2f362378c5: Already exists
494c27a8a6b8: Already exists
7596bb83081b: Already exists
372744b62d49: Already exists
615db220d76c: Already exists
1865698adfb0: Already exists
c506c1442a1e: Already exists
79230d91d387: Already exists
8db41df01a2f: Already exists
Digest: sha256:6f69a14679ebf9be7613517da5153830ed7b43c15f6e60920ce1699897fe8cde
Status: Downloaded newer image for python:3.5
 ---> 6da24b346f6e
Step 2/10 : ENV PYTHONUNBUFFERED 1
 ---> Running in ef5354743cb3
Removing intermediate container ef5354743cb3
 ---> 26cc756f879f
Step 3/10 : RUN echo python -V
 ---> Running in e56e3a79e7d2
python -V
Removing intermediate container e56e3a79e7d2
 ---> 44223b48b228
Step 4/10 : RUN mkdir /code
 ---> Running in 601ede1c64e4
Removing intermediate container 601ede1c64e4
 ---> f113019172b7
Step 5/10 : RUN mkdir /code/db
 ---> Running in 0f6d9ae16bcf
Removing intermediate container 0f6d9ae16bcf
 ---> 11e2ee747342
Step 6/10 : WORKDIR /code
 ---> Running in a8de419afd57
Removing intermediate container a8de419afd57
 ---> 412c39355c54
Step 7/10 : RUN pip install -i https://pypi.douban.com/simple django
 ---> Running in 2dc6399c3027
Looking in indexes: https://pypi.douban.com/simple
Collecting django
  Downloading https://pypi.doubanio.com/packages/b1/1d/2476110614367adfb079a9bc718621f9fc8351e9214e1750cae1832d4090/Django-2.2.1-py3-none-any.whl (7.4MB)
Collecting pytz (from django)
  Downloading https://pypi.doubanio.com/packages/3d/73/fe30c2daaaa0713420d0382b16fbb761409f532c56bdcc514bf7b6262bb6/pytz-2019.1-py2.py3-none-any.whl (510kB)
Collecting sqlparse (from django)
  Downloading https://pypi.doubanio.com/packages/ef/53/900f7d2a54557c6a37886585a91336520e5539e3ae2423ff1102daf4f3a7/sqlparse-0.3.0-py2.py3-none-any.whl
Installing collected packages: pytz, sqlparse, django
Successfully installed django-2.2.1 pytz-2019.1 sqlparse-0.3.0
Removing intermediate container 2dc6399c3027
 ---> 2df1ba010b7a
Step 8/10 : RUN pip3 install PyMySQL
 ---> Running in 1de1b81baabd
Collecting PyMySQL
  Downloading https://files.pythonhosted.org/packages/ed/39/15045ae46f2a123019aa968dfcba0396c161c20f855f11dea6796bcaae95/PyMySQL-0.9.3-py2.py3-none-any.whl (47kB)
Installing collected packages: PyMySQL
Successfully installed PyMySQL-0.9.3
Removing intermediate container 1de1b81baabd
 ---> 974c80b79a3a
Step 9/10 : RUN pip3 install mysqlclient
 ---> Running in 5f906280f5ad
Collecting mysqlclient
  Downloading https://files.pythonhosted.org/packages/f4/f1/3bb6f64ca7a429729413e6556b7ba5976df06019a5245a43d36032f1061e/mysqlclient-1.4.2.post1.tar.gz (85kB)
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (setup.py): started
  Building wheel for mysqlclient (setup.py): finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/30/91/e0/2ee952bce05b1247807405c6710c6130e49468a5240ae27134
Successfully built mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.4.2.post1
Removing intermediate container 5f906280f5ad
 ---> 5b8fd9ec8963
Step 10/10 : ADD . /code/
 ---> a1d72b19f175
Successfully built a1d72b19f175
Successfully tagged site_web:latest

7. 创建django项目,工程名为achievements

docker-compose run web django-admin.py startproject achievements ./achievements

[root@VM_0_9_centos site]# docker-compose run web django-admin.py startproject achievements ./achievements
Pulling db (mysql:latest)...
latest: Pulling from library/mysql
fc7181108d40: Already exists
787a24c80112: Already exists
a08cb039d3cd: Already exists
4f7d35eb5394: Already exists
5aa21f895d95: Already exists
a742e211b7a2: Already exists
0163805ad937: Already exists
87f18876c3ff: Already exists
78082f25f167: Already exists
0a510f055c17: Already exists
312b0999e433: Already exists
f864cfdc0264: Already exists
Digest: sha256:415ac63da0ae6725d5aefc9669a1c02f39a00c574fdbc478dfd08db1e97c8f1b
Status: Downloaded newer image for mysql:latest
Starting f3ee08562d43_f3ee08562d43_f3ee08562d43_site_db_1

8. 修改工程目录的权限

chmod -R 777 achievements/

9. 修改工程内的settings.py文件的mysql部分为如下:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'achievements',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': 'db',
        'PORT': 3306,
    }
}

10. 修改settings.py中的ALLOWED_HOSTS部分

ALLOWED_HOSTS=['*']

11. 运行

docker-compose up

12. 运行测试

Docker----搭建python3+django+mysql开发环境_第1张图片

本文参考链接:https://blog.csdn.net/qq_25639809/article/details/78958647

你可能感兴趣的:(docker)