docker6 部署Nginx django mysql
2017-12-14 16:58
609 查看
使用docker来搭建开发环境不仅能够跟我们主机的已有的各种软件配置隔离,而且也能够很方便地分发给别人,从而使团队能够在统一的开发环境下快速开始开发、测试和部署。本文采用Docker的docker-compose来搭建python2.7+django1.7.5+mysql的web开发环境,希望可以给需要的同学参考。
1、项目目录
创建工程目录mysitepython@ubuntu:~/Desktop/mysite$ mkdir mysite && cd mysite1创建以下2个目录(这里为方便放置各个文件,可以根据需要自己组织,后面配置文件做相应修改即可)
python@ubuntu:~/Desktop/mysite$ mkdir db mysite1
2、配置文件
(1)DockerfileDockerfile包含创建镜像所需要的全部指令。在项目根目录下创建Dockerfile文件,其内容如下:
FROM python:2.7 ENV PYTHONUNBUFFERED 1 RUN mkdir /code RUN mkdir /cod 12adf e/db WORKDIR /code ADD ./mysite/requirements.txt /code/ RUN pip install -r requirements.txt ADD . /code/12345678第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:web12345678910111213141516171819db标签: images表示使用mysql镜像 expose表示暴露端口3306,但不发布到宿主机上 volume表示挂载宿主机的路径作为卷,冒号左边为宿主机路径,右边为镜像内路径 environment为环境变量,每个容器都有自己定义的环境变量,具体查看镜像手册中的mysqlweb标签: build指定建立Dockerfile路径 command将覆盖默认的命令 ports指定主机开放的端口 links指向其他容器中的服务更多该配置文件参见这里(3)requirements.txt在子目录mysite下
requirements.txt文件,该文件内容如下:
django==1.9.8MySQL-python12
3、构建镜像
在项目根目录执行以下命令yhc@yhc-E540:~/workspaces/docker/mysite$ docker-compose builddb uses an image, skippingBuilding web...Step 0 : FROM python:2.7---> d833e0b23482Step 1 : ENV PYTHONUNBUFFERED 1---> Using cache---> df633fc1ab0eStep 2 : RUN mkdir /code---> Using cache---> 49bb20e37bfcStep 3 : WORKDIR /code---> Using cache---> f84fca46a343Step 4 : ADD ./mysite/requirements.txt /code/---> e8f756bed13eRemoving intermediate container 91e677d50cd4Step 5 : RUN pip install -r requirements.txt---> Running in 7eb86e071025Collecting 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-pythonRunning setup.py install for MySQL-pythonSuccessfully installed MySQL-python-1.2.5 django-1.7.5---> 3e5c9b891397Removing intermediate container 7eb86e071025Step 6 : ADD . /code/---> 251dea7e2af2Removing intermediate container b4b0c2b08538Successfully built 251dea7e2af21234567891011121314151617181920212223242526272829303132如果本地没有python和mysql镜像的话下载需要多等待一些时间。完成之后就构建好了python+django+mysql的镜像。
4、创建django
执行以下命令yhc@yhc-E540:~/workspaces/docker/mysite$ docker-compose run web django-admin.py startproject mysite ./mysiteStarting dockermysite_db_1...12完成之后就在子目录mysite下创建了一个新的django工程 因为在镜像内是以root权限创建的,所以宿主机中对工程文件无法进行更改,这里修改一下权限 将自己的项目复杂到目录下
yhc@yhc-E540:~/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,}}12345678910
5、运行
python@ubuntu:~/Desktop/mysite$ sudo docker-compose upStarting mysite_db_1 ... Starting mysite_db_1 ... doneStarting mysite_web_1 ... Starting mysite_web_1 ... doneAttaching to mysite_db_1, mysite_web_1db_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 availabledb_1 | 2017-12-14T08:19:01.343630Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtinsdb_1 | 2017-12-14T08:19:01.343663Z 0 [Note] InnoDB: Uses event mutexesdb_1 | 2017-12-14T08:19:01.343690Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrierdb_1 | 2017-12-14T08:19:01.343712Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3db_1 | 2017-12-14T08:19:01.343733Z 0 [Note] InnoDB: Using Linux native AIOdb_1 | 2017-12-14T08:19:01.368948Z 0 [Note] InnoDB: Number of pools: 1db_1 | 2017-12-14T08:19:01.369454Z 0 [Note] InnoDB: Using CPU crc32 instructionsdb_1 | 2017-12-14T08:19:01.384139Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128Mdb_1 | 2017-12-14T08:19:01.517780Z 0 [Note] InnoDB: Completed initialization of buffer pooldb_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 12156856db_1 | 2017-12-14T08:19:01.940603Z 0 [Note] InnoDB: Doing recovery: scanned up to log sequence number 12156865db_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 tablesdb_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 startdb_1 | 2017-12-14T08:19:04.615274Z 0 [Note] InnoDB: 5.7.20 started; log sequence number 12156865db_1 | 2017-12-14T08:19:04.644320Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pooldb_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:04db_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: 3306db_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 'root@localhost' ignored in --skip-name-resolve mode.db_1 | 2017-12-14T08:19:05.893740Z 0 [Warning] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.db_1 | 2017-12-14T08:19:05.893774Z 0 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.db_1 | 2017-12-14T08:19:05.894471Z 0 [Warning] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.db_1 | 2017-12-14T08:19:05.894526Z 0 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.db_1 | 2017-12-14T08:19:05.894935Z 0 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.db_1 | 2017-12-14T08:19:06.356866Z 0 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.db_1 | 2017-12-14T08:19:06.356964Z 0 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.db_1 | 2017-12-14T08:19:07.047549Z 0 [Note] Event Scheduler: Loaded 0 eventsdb_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 tablesdb_1 | 2017-12-14T08:19:07.991986Z 0 [Note] End of list of non-natively partitioned tablesweb_1 | Performing system checks...web_1 | web_1 | System check identified no issues (0 silenced).web_1 | December 14, 2017 - 08:19:13web_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.)
相关文章推荐
- Docker部署WordPress LNMP(Nginx PHP MySQL)环境实践
- CentOS nginx django uwsgi mysql 部署项目
- docker容器里面各测试环境部署部署脚本(nginx,php,redis,mysql,nodejs)
- 使用docker部署php7 mysql nginx(richarvey/nginx-php-fpm+mysql)
- docker-web搭建(mysql,redis,django,nginx)
- Docker搭建 Nginx+PHP+MySQL 环境并部署WordPress实践
- Docker部署WordPress LNMP(Nginx PHP MySQL)环境实践
- 在nginx上部署django项目--------Gunicorn+Django+nginx+mysql
- Django Ubuntu部署: python3 + mysql + nginx + uswgi
- Docker部署WordPress LNMP(Nginx PHP MySQL)环境实践
- centos下用uwsgi + nginx + mysql部署django项目
- Docker部署WordPress LNMP(Nginx PHP MySQL)环境实践
- Docker之docker-compose部署django+mysql示例
- docker安装mysql、tomcat、nginx,应用部署
- Docker分离部署MySQL、Nginx+Tomcat复制共享 推荐
- Docker部署WordPress LNMP(Nginx PHP MySQL)环境实践
- Docker部署WordPress LNMP(Nginx PHP MySQL)环境实践
- django部署方式之Gunicorn+Django+nginx+mysql
- Docker部署WordPress LNMP(Nginx PHP MySQL)环境实践
- 在阿里云ECS上部署 Django+MySQL+uWSGI+Nginx 项目的基本流程