本文介紹如何用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 '[email protected]' 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 '@ [email protected]' 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,即可成功訪問網站