Docker之docker-compose部署django+mysql示例

版权声明:本文为博主原创文章,未经博主允许不得转载。更多文章www.ampliooo.com https://blog.csdn.net/m0_37904728/article/details/78626610

本文介绍如何用docker-compose部署django+mysql搭建网站。

1.安装docker-compose,并为docker-compose添加执行权限:

[root@localhost docker]# sudo curl -L https://github.com/docker/compose/releases/download/1.17.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
[root@localhost docker]# sudo chmod +x /usr/local/bin/docker-compose

2.在host上创建一个docker目录,如:

[root@localhost docker]# mkdir docker_django/


3.进入创建的目录下,创建Dockerfile/requirements.txt/docker-compose.yml这三个文件分别如下:

#Dockerfile
FROM python:2.7                         #由于我的django项目是基于python2.7,因此用python2.7作为parent image
 ENV PYTHONUNBUFFERED 1			#设置环境参数
 RUN mkdir /code			#在parent image上创建目录/code
 RUN mkdir /code/proj			#在parent image上创建目录/code/proj,用于映射host上django项目的目录
 WORKDIR /code				#设置工作目录在/code
 ADD requirements.txt /code/		#将requirements.txt添加到/code目录下(虽然最后一行也会拷贝,但由于下一行就要使用requirements.txt,所以要提前添加)
 RUN pip install -r requirements.txt	#在parent image上安装requirements.txt中列出的库
 ADD . /code/				#把host的当前目录下的文件全部拷贝到/code下
#requirements.txt
Django					#django项目需要的两个python库的名称
MySQL-python

#docker-compose.yml
version: '3'

services:
  db:										#定义服务db
    image: owenchen1992/mysql							#使用镜像为owenchen1992/mysql
    volumes:									#映射host上保存数据库数据的目录到mysql容器的工作目录				
      - /var/lib/docker/volumes/mysql:/var/lib/mysql:rw
  web:
    build: .									#用当前文件夹下的所有文件build镜像
    command: python /code/proj/manage.py runserver 0.0.0.0:8000			#服务开启后执行该命令
    volumes:									#将host上的django项目映射到/code/proj目录
      - /smb/public/eclipse-workspace/DjangoProj/djcode/mysite/:/code/proj/:rw
    ports:									#端口映射为8000:8000
      - "8000:8000"
    depends_on:									#该服务依赖于db服务
      - db

4.更改django项目的settings.py

# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'CIDB',
        'USER':'root',
        'PASSWORD':'emc123123',
        'HOST':'db',				#数据库主机名为db,即运行数据库镜像容器的服务名称
        'PORT':'3306',
    }
}


5.在项目的顶层目录,在本例即刚才创建的docker_django目录下运行docker-compose up,可以看到django网站已经成功运行:

[root@localhost docker_django]# docker-compose up
WARNING: The Docker Engine you're using is running in swarm mode.

Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.

To deploy your application across the swarm, use `docker stack deploy`.

Creating dockerdjango_db_1 ... 
Creating dockerdjango_db_1 ... done
Recreating dockerdjango_web_1 ... 
Recreating dockerdjango_web_1 ... done
Attaching to dockerdjango_db_1, dockerdjango_web_1
db_1   | 2017-11-24T09:13:22.577336Z 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-11-24T09:13:22.579015Z 0 [Note] mysqld (mysqld 5.7.20) starting as process 1 ...
db_1   | 2017-11-24T09:13:22.582707Z 0 [Note] InnoDB: PUNCH HOLE support available
db_1   | 2017-11-24T09:13:22.582731Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
db_1   | 2017-11-24T09:13:22.582736Z 0 [Note] InnoDB: Uses event mutexes
db_1   | 2017-11-24T09:13:22.582739Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
db_1   | 2017-11-24T09:13:22.582741Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
db_1   | 2017-11-24T09:13:22.582743Z 0 [Note] InnoDB: Using Linux native AIO
db_1   | 2017-11-24T09:13:22.583038Z 0 [Note] InnoDB: Number of pools: 1
db_1   | 2017-11-24T09:13:22.583160Z 0 [Note] InnoDB: Using CPU crc32 instructions
db_1   | 2017-11-24T09:13:22.584990Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
db_1   | 2017-11-24T09:13:22.593619Z 0 [Note] InnoDB: Completed initialization of buffer pool
db_1   | 2017-11-24T09:13:22.595494Z 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-11-24T09:13:22.607543Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
db_1   | 2017-11-24T09:13:22.619747Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
db_1   | 2017-11-24T09:13:22.619883Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
db_1   | 2017-11-24T09:13:22.645228Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
db_1   | 2017-11-24T09:13:22.645905Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
db_1   | 2017-11-24T09:13:22.645935Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
db_1   | 2017-11-24T09:13:22.646691Z 0 [Note] InnoDB: Waiting for purge to start
db_1   | 2017-11-24T09:13:22.697146Z 0 [Note] InnoDB: 5.7.20 started; log sequence number 12553687
db_1   | 2017-11-24T09:13:22.698025Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
db_1   | 2017-11-24T09:13:22.698219Z 0 [Note] Plugin 'FEDERATED' is disabled.
db_1   | 2017-11-24T09:13:22.700939Z 0 [Note] InnoDB: Buffer pool(s) load completed at 171124  9:13:22
db_1   | 2017-11-24T09:13:22.702304Z 0 [Warning] System table 'plugin' is expected to be transactional.
db_1   | 2017-11-24T09:13:22.703422Z 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-11-24T09:13:22.703765Z 0 [Warning] CA certificate ca.pem is self signed.
db_1   | 2017-11-24T09:13:22.706145Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
db_1   | 2017-11-24T09:13:22.706197Z 0 [Note] IPv6 is available.
db_1   | 2017-11-24T09:13:22.706205Z 0 [Note]   - '::' resolves to '::';
db_1   | 2017-11-24T09:13:22.706223Z 0 [Note] Server socket created on IP: '::'.
db_1   | 2017-11-24T09:13:22.712616Z 0 [Warning] 'user' entry 'root@localhost.localdomain' ignored in --skip-name-resolve mode.
db_1   | 2017-11-24T09:13:22.712727Z 0 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
db_1   | 2017-11-24T09:13:22.712734Z 0 [Warning] 'proxies_priv' entry '@ root@localhost.localdomain' ignored in --skip-name-resolve mode.
db_1   | 2017-11-24T09:13:22.713521Z 0 [Warning] System table 'time_zone_leap_second' is expected to be transactional.
db_1   | 2017-11-24T09:13:22.713534Z 0 [Warning] System table 'time_zone_name' is expected to be transactional.
db_1   | 2017-11-24T09:13:22.713537Z 0 [Warning] System table 'time_zone' is expected to be transactional.
db_1   | 2017-11-24T09:13:22.713539Z 0 [Warning] System table 'time_zone_transition_type' is expected to be transactional.
db_1   | 2017-11-24T09:13:22.713541Z 0 [Warning] System table 'time_zone_transition' is expected to be transactional.
db_1   | 2017-11-24T09:13:22.714203Z 0 [Warning] System table 'servers' is expected to be transactional.
db_1   | 2017-11-24T09:13:22.717923Z 0 [ERROR] Incorrect definition of table performance_schema.accounts: expected column 'USER' at position 0 to have type char(32), found type char(16).
db_1   | 2017-11-24T09:13:22.720764Z 0 [ERROR] Incorrect definition of table mysql.db: expected column 'User' at position 2 to have type char(32), found type char(16).
db_1   | 2017-11-24T09:13:22.720833Z 0 [ERROR] mysql.user has no `Event_priv` column at position 28
db_1   | 2017-11-24T09:13:22.721077Z 0 [ERROR] Event Scheduler: An error occurred when initializing system tables. Disabling the Event Scheduler.
db_1   | 2017-11-24T09:13:22.721422Z 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-11-24T09:13:22.721454Z 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-11-24T09:13:22.721458Z 0 [Note] Beginning of list of non-natively partitioned tables
db_1   | 2017-11-24T09:13:22.737852Z 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  | November 24, 2017 - 09:13:24
web_1  | Django version 1.11.7, using settings 'mysite.settings'
web_1  | Starting development server at http://0.0.0.0:8000/
web_1  | Quit the server with CONTROL-C.

6.确认db和web所在的网络:

[root@localhost docker_django]# docker network ls
NETWORK ID          NAME                   DRIVER              SCOPE
5b536f8598ac        bridge                 bridge              local
78fc4e0cf9c2        docker_gwbridge        bridge              local
eb70db39d84b        dockerdjango_default   bridge              local      #发现这是一个docker-compose up命令自动创建的bridge网络
nk6tbkc614n4        getstartedlab_webnet   overlay             swarm
128ace262fbf        host                   host                local
qit7w65ffyvx        ingress                overlay             swarm
5a5ce5df8d33        none                   null                local

7.查看该网络, 可以看到这两个服务(db和web都在这个网络内,web的ip为172.20.0.3)

[root@localhost docker_django]# docker network inspect dockerdjango_default 
[
   ...
        "Containers": {
            "5c33c49f358c637d448030f43965a0cedd67b9cdb85d1de351b60859242fb8e6": {
                "Name": "dockerdjango_web_1",
                "EndpointID": "a3b779b8ca3a5acdeafc257975d5b2123c836b0f6c00dbc3228be74c54c3e758",
                "MacAddress": "02:42:ac:14:00:03",
                "IPv4Address": "172.20.0.3/16",
                "IPv6Address": ""
            },
            "a2be6a52eed18018a8466ffa042f7662b425f3b5c09f77d2d5fdc1bf670b5dbf": {
                "Name": "dockerdjango_db_1",
                "EndpointID": "c6dd5f8547bef257737343e5d7ff08b0d5ecb4c3bc8ed1e8bb111c32ff61cacd",
                "MacAddress": "02:42:ac:14:00:02",
                "IPv4Address": "172.20.0.2/16",
                "IPv6Address": ""
            }
        },
   ...
]

8.在host的浏览器上访问172.20.0.3:8000,即可成功访问网站



展开阅读全文

没有更多推荐了,返回首页