docker6 部署Nginx django mysql
使用docker来搭建开发环境不仅能够跟我们主机的已有的各种软件配置隔离,而且也能够很方便地分发给别人,从而使团队能够在统一的开发环境下快速开始开发、测试和部署。本文采用Docker的docker-compose来搭建python2.7+django1.7.5+mysql的web开发环境,希望可以给需要的同学参考。
1、项目目录
创建工程目录mysite
[email protected]:~/Desktop/mysite$ mkdir mysite && cd mysite
- 1
创建以下2个目录(这里为方便放置各个文件,可以根据需要自己组织,后面配置文件做相应修改即可)
[email protected]:~/Desktop/mysite$ mkdir db mysite
- 1
2、配置文件
(1)Dockerfile
Dockerfile包含创建镜像所需要的全部指令。在项目根目录下创建Dockerfile
文件,其内容如下:
FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
RUN mkdir /code/db
WORKDIR /code
ADD ./mysite/requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
第1行的FROM
指令表示新的镜像将基于python:2.7的镜像来构建
第2行的ENV
为环境变量(PYTHONUNBUFFERED见这里)
第3行的RUN
指令表示在镜像内新建/code目录
第4行指定指定RUN、CMD与ENTRYPOINT命令的工作目录
第5行是将./mysite/requirements.txt
文件添加到刚才新建的code目录中
第6行运行pip
安装所需的软件
Dockerfile详细可以参见《Docker入门实战》
以及官方参考
(2)docker-compose.yml
之前的Dockerfile定义了一个应用,而使用compose,可以在一个文件里,定义多容器的应用。该YAML配置语言,用于描述和组装多容器的分布式应用。在项目根目录创建docker-compose.yml
文件,其内容如下:
db:
image: mysql
expose:
- "3306"
volumes:
- ./db:/var/lib/mysql
environment:
- MYSQL_DATABASE=mysitedb
- MYSQL_ROOT_PASSWORD=11111111
web:
build: .
command: python ./mysite/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
links:
- db
nginx:
restart: always
build: ./nginx/
ports:
- "80:80"
volumes:
- /www/static
volumes_from:
- web
links:
- web:web
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
db标签:
images表示使用mysql镜像
expose表示暴露端口3306,但不发布到宿主机上
volume表示挂载宿主机的路径作为卷,冒号左边为宿主机路径,右边为镜像内路径
environment为环境变量,每个容器都有自己定义的环境变量,具体查看镜像手册中的mysql
web标签:
build指定建立Dockerfile路径
command将覆盖默认的命令
ports指定主机开放的端口
links指向其他容器中的服务
更多该配置文件参见这里
(3)requirements.txt
在子目录mysite下requirements.txt
文件,该文件内容如下:
django==1.9.8
MySQL-python
- 1
- 2
3、构建镜像
在项目根目录执行以下命令
[email protected]:~/workspaces/docker/mysite$ docker-compose build
db uses an image, skipping
Building web...
Step 0 : FROM python:2.7
---> d833e0b23482
Step 1 : ENV PYTHONUNBUFFERED 1
---> Using cache
---> df633fc1ab0e
Step 2 : RUN mkdir /code
---> Using cache
---> 49bb20e37bfc
Step 3 : WORKDIR /code
---> Using cache
---> f84fca46a343
Step 4 : ADD ./mysite/requirements.txt /code/
---> e8f756bed13e
Removing intermediate container 91e677d50cd4
Step 5 : RUN pip install -r requirements.txt
---> Running in 7eb86e071025
Collecting django==1.7.5 (from -r requirements.txt (line 1))
Downloading Django-1.7.5-py2.py3-none-any.whl (7.4MB)
Collecting MySQL-python (from -r requirements.txt (line 2))
Downloading MySQL-python-1.2.5.zip (108kB)
Installing collected packages: django, MySQL-python
Running setup.py install for MySQL-python
Successfully installed MySQL-python-1.2.5 django-1.7.5
---> 3e5c9b891397
Removing intermediate container 7eb86e071025
Step 6 : ADD . /code/
---> 251dea7e2af2
Removing intermediate container b4b0c2b08538
Successfully built 251dea7e2af2
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
如果本地没有python和mysql镜像的话下载需要多等待一些时间。完成之后就构建好了python+django+mysql的镜像。
4、创建django
执行以下命令
[email protected]:~/workspaces/docker/mysite$ docker-compose run web django-admin.py startproject mysite ./mysite
Starting dockermysite_db_1...
- 1
- 2
完成之后就在子目录mysite下创建了一个新的django工程
因为在镜像内是以root权限创建的,所以宿主机中对工程文件无法进行更改,这里修改一下权限
将自己的项目复杂到目录下
[email protected]:~/workspaces/docker/mysite$ sudo chmod -R 777 mysite/
- 1
修改setttings.py
文件中数据库配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysitedb',
'USER': 'root',
'PASSWORD': '11111111',
'HOST': 'db',
'PORT': 3306,
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
5、运行
[email protected]:~/Desktop/mysite$ sudo docker-compose up
Starting mysite_db_1 ...
Starting mysite_db_1 ... done
Starting mysite_web_1 ...
Starting mysite_web_1 ... done
Attaching to mysite_db_1, mysite_web_1
db_1 | 2017-12-14T08:19:00.677919Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
db_1 | 2017-12-14T08:19:01.107753Z 0 [Note] mysqld (mysqld 5.7.20) starting as process 1 ...
db_1 | 2017-12-14T08:19:01.343445Z 0 [Note] InnoDB: PUNCH HOLE support available
db_1 | 2017-12-14T08:19:01.343630Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
db_1 | 2017-12-14T08:19:01.343663Z 0 [Note] InnoDB: Uses event mutexes
db_1 | 2017-12-14T08:19:01.343690Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
db_1 | 2017-12-14T08:19:01.343712Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
db_1 | 2017-12-14T08:19:01.343733Z 0 [Note] InnoDB: Using Linux native AIO
db_1 | 2017-12-14T08:19:01.368948Z 0 [Note] InnoDB: Number of pools: 1
db_1 | 2017-12-14T08:19:01.369454Z 0 [Note] InnoDB: Using CPU crc32 instructions
db_1 | 2017-12-14T08:19:01.384139Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
db_1 | 2017-12-14T08:19:01.517780Z 0 [Note] InnoDB: Completed initialization of buffer pool
db_1 | 2017-12-14T08:19:01.553332Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
db_1 | 2017-12-14T08:19:01.897114Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
db_1 | 2017-12-14T08:19:01.940543Z 0 [Note] InnoDB: Log scan progressed past the checkpoint lsn 12156856
db_1 | 2017-12-14T08:19:01.940603Z 0 [Note] InnoDB: Doing recovery: scanned up to log sequence number 12156865
db_1 | 2017-12-14T08:19:01.940615Z 0 [Note] InnoDB: Database was not shutdown normally!
db_1 | 2017-12-14T08:19:01.940623Z 0 [Note] InnoDB: Starting crash recovery.
db_1 | 2017-12-14T08:19:04.158787Z 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
db_1 | 2017-12-14T08:19:04.158885Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
db_1 | 2017-12-14T08:19:04.158971Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
db_1 | 2017-12-14T08:19:04.472177Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
db_1 | 2017-12-14T08:19:04.554992Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
db_1 | 2017-12-14T08:19:04.555013Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
db_1 | 2017-12-14T08:19:04.564699Z 0 [Note] InnoDB: Waiting for purge to start
db_1 | 2017-12-14T08:19:04.615274Z 0 [Note] InnoDB: 5.7.20 started; log sequence number 12156865
db_1 | 2017-12-14T08:19:04.644320Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
db_1 | 2017-12-14T08:19:04.655519Z 0 [Note] Plugin 'FEDERATED' is disabled.
db_1 | 2017-12-14T08:19:04.924316Z 0 [Note] InnoDB: Buffer pool(s) load completed at 171214 8:19:04
db_1 | 2017-12-14T08:19:05.459894Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
db_1 | 2017-12-14T08:19:05.528144Z 0 [Warning] CA certificate ca.pem is self signed.
db_1 | 2017-12-14T08:19:05.596878Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
db_1 | 2017-12-14T08:19:05.597386Z 0 [Note] IPv6 is available.
db_1 | 2017-12-14T08:19:05.597416Z 0 [Note] - '::' resolves to '::';
db_1 | 2017-12-14T08:19:05.597484Z 0 [Note] Server socket created on IP: '::'.
db_1 | 2017-12-14T08:19:05.893524Z 0 [Warning] 'user' entry '[email protected]' ignored in --skip-name-resolve mode.
db_1 | 2017-12-14T08:19:05.893740Z 0 [Warning] 'user' entry '[email protected]' ignored in --skip-name-resolve mode.
db_1 | 2017-12-14T08:19:05.893774Z 0 [Warning] 'user' entry '[email protected]' ignored in --skip-name-resolve mode.
db_1 | 2017-12-14T08:19:05.894471Z 0 [Warning] 'db' entry 'performance_schema [email protected]' ignored in --skip-name-resolve mode.
db_1 | 2017-12-14T08:19:05.894526Z 0 [Warning] 'db' entry 'sys [email protected]' ignored in --skip-name-resolve mode.
db_1 | 2017-12-14T08:19:05.894935Z 0 [Warning] 'proxies_priv' entry '@ [email protected]' ignored in --skip-name-resolve mode.
db_1 | 2017-12-14T08:19:06.356866Z 0 [Warning] 'tables_priv' entry 'user [email protected]' ignored in --skip-name-resolve mode.
db_1 | 2017-12-14T08:19:06.356964Z 0 [Warning] 'tables_priv' entry 'sys_config [email protected]' ignored in --skip-name-resolve mode.
db_1 | 2017-12-14T08:19:07.047549Z 0 [Note] Event Scheduler: Loaded 0 events
db_1 | 2017-12-14T08:19:07.048106Z 0 [Note] mysqld: ready for connections.
db_1 | Version: '5.7.20' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
db_1 | 2017-12-14T08:19:07.048132Z 0 [Note] Executing 'SELECT * FROM INFORMATION_SCHEMA.TABLES;' to get a list of tables using the deprecated partition engine. You may use the startup option '--disable-partition-engine-check' to skip this check.
db_1 | 2017-12-14T08:19:07.048144Z 0 [Note] Beginning of list of non-natively partitioned tables
db_1 | 2017-12-14T08:19:07.991986Z 0 [Note] End of list of non-natively partitioned tables
web_1 | Performing system checks...
web_1 |
web_1 | System check identified no issues (0 silenced).
web_1 | December 14, 2017 - 08:19:13
web_1 | Django version 1.9.8, using settings 'mmcsite.settings'
web_1 | Starting development server at http://0.0.0.0:8000/
web_1 | Quit the server with CONTROL-C.
db_1 | 2017-12-14T08:20:54.431370Z 0 [Note] InnoDB: page_cleaner: 1000ms intended loop took 6758ms. The settings might not be optimal. (flushed=0 and evicted=0, during the time.)
推荐阅读
-
在Debian下配置Python+Django+Nginx+uWSGI+MySQL的教程
-
在Linux系统上部署Apache+Python+Django+MySQL环境
-
Python环境Django+uwsgi+nginx的部署
-
详解Django+Uwsgi+Nginx的生产环境部署
-
django2+uwsgi+nginx上线部署到服务器Ubuntu16.04
-
详解Django+Uwsgi+Nginx 实现生产环境部署
-
Django uwsgi Nginx 的生产环境部署详解
-
Ubuntu 14.04下Django和MySQL环境部署全过程
-
SLAM+语音机器人DIY系列:(八)高阶拓展——2.centos7下部署Django(nginx+uwsgi+django+python3)
-
在Linux系统上部署Apache+Python+Django+MySQL环境