about
本篇主要為了練習Django在docker環境的基本部署,熟悉相關部署流程。
宿主的一些環境要提前配置好:
- Python環境,https://www.cnblogs.com/Neeo/articles/12293143.html
- django環境,
pip3 install django==1.11.1
- django環境,
- docker環境,https://www.cnblogs.com/Neeo/articles/11945963.html
- docker-compose,https://www.cnblogs.com/Neeo/articles/12721710.html
[root@r ~]# python3 -V
Python 3.6.8
[root@r ~]# pip3 -V
pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)
[root@r ~]# python3 -m django --version
1.11.1
[root@r ~]# docker --version
Docker version 18.06.3-ce, build d7080c1
[root@r ~]# docker-compose -v
docker-compose version 1.18.0, build 8dd22a9
創建Django項目
創建/opt/DjangoDemo
目錄,並且后續的配置工作都在此目錄上展開。
創建Django項目
現在,我們在/opt/DjangoDemo
目錄下創建一個Django項目AT
,並且執行數據庫遷移命令。
[root@r ~]# mkdir /opt/DjangoDemo && cd /opt/DjangoDemo
[root@r DjangoDemo]# django-admin startproject AT
[root@r DjangoDemo]# ls
AT
[root@r DjangoDemo]# cd AT/
[root@r AT]# python3 manage.py makemigrations
No changes detected
[root@r AT]# python3 manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying sessions.0001_initial... OK
創建requirements.txt文件
requirements.txt
文件不用說了吧!我們項目的依賴包都管理在該文件中。
在項目的根目錄創建該文件,並填寫一個django的版本。
[root@r AT]# echo 'django==1.11.1' >> requirements.txt
修改settings中的ALLOWED_HOSTS
修改ALLOWED_HOSTS
以支持遠程訪問。
ALLOWED_HOSTS = ["www.neeo.cc", "127.0.0.1"]
www.neeo.cc
是我的服務器的域名;127.0.0.1
是容器中的ip。
使用Dockerfile來構建鏡像文件
首先要拉取一個Python3.6的鏡像:
[root@r AT]# docker pull python:3.6-alpine
3.6-alpine: Pulling from library/python
cbdbe7a5bc2a: Pull complete
26ebcd19a4e3: Pull complete
ebb7c97b2c7d: Download complete
c30b8a8127ec: Download complete
a82086273aba: Download complete
3.6-alpine: Pulling from library/python
cbdbe7a5bc2a: Pull complete
26ebcd19a4e3: Pull complete
ebb7c97b2c7d: Pull complete
c30b8a8127ec: Pull complete
a82086273aba: Pull complete
Digest: sha256:5592efbc8cd685cb40d1ab5821438522b393480da18d11863717ceea4582c89d
Status: Downloaded newer image for python:3.6-alpine
[root@r AT]# docker images | grep python
python 3.6-alpine 92a6cc6f3ecc 35 hours ago 92.8MB
[root@r AT]# vim dockerfile
編輯Dockerfile
文件:
# 基於本地倉庫 python:3.6-alpine 鏡像
FROM python:3.6-alpine
# 設置 python 環境變量
ENV PYTHONUNBUFFERED 1
# 創建 code 文件夾並將其設置為工作目錄
RUN mkdir /code
WORKDIR /code
# 更新 pip
RUN pip install pip -U
# 將 requirements.txt 復制到容器的 code 目錄
ADD requirements.txt /code/
# 安裝庫
RUN pip install -r requirements.txt
# 將當前目錄復制到容器的 code 目錄, 點 表示當前目錄中的所有文件復制到容器的code目錄。
ADD . /code/
此時Django項目目錄文件結構如下:
[root@r AT]# pwd
/opt/DjangoDemo/AT
[root@r AT]# ls
AT db.sqlite3 dockerfile manage.py requirements.txt
編輯docker-compose
在項目根目錄編輯[root@r AT]# vim docker-compose.yaml
version: "3.3"
services:
at:
restart: always
build: . # 點表示當前目錄
command: "python3 manage.py runserver 0.0.0.0:8000"
volumes:
- .:/code
ports:
- "8823:8000"
command
是啟動django服務監聽(容器環境)本地8000端口,然后下面的ports
是將容器的8000
端口映射到宿主機的8823
端口。
此時Django項目目錄文件結構如下:
[root@r AT]# ls
AT db.sqlite3 docker-compose.yaml dockerfile manage.py requirements.txt
測試
使用docker-compose
命令來啟動容器服務:
[root@r AT]# docker-compose up
Creating network "at_default" with the default driver
Building at
Step 1/8 : FROM python:3.6-alpine
---> 92a6cc6f3ecc
Step 2/8 : ENV PYTHONUNBUFFERED 1
---> Running in f9d6711cc31c
Removing intermediate container f9d6711cc31c
---> 5c46570a82d9
Step 3/8 : RUN mkdir /code
---> Running in 57fc971ecfe5
Removing intermediate container 57fc971ecfe5
---> 9743aab02b3f
Step 4/8 : WORKDIR /code
---> Running in fe629f84a1c2
Removing intermediate container fe629f84a1c2
---> 7e910fdc2da3
Step 5/8 : RUN pip install pip -U
---> Running in 0d022709942c
Requirement already up-to-date: pip in /usr/local/lib/python3.6/site-packages (20.1)
Removing intermediate container 0d022709942c
---> ad31936ddeac
Step 6/8 : ADD requirements.txt /code/
---> f8fb28bc5fc4
Step 7/8 : RUN pip install -r requirements.txt
---> Running in 6e8dfeb7a01a
Collecting django==1.11.1
Downloading Django-1.11.1-py2.py3-none-any.whl (6.9 MB)
Collecting pytz
Downloading pytz-2020.1-py2.py3-none-any.whl (510 kB)
Installing collected packages: pytz, django
Successfully installed django-1.11.1 pytz-2020.1
Removing intermediate container 6e8dfeb7a01a
---> 5200a0425ea6
Step 8/8 : ADD . /code/
---> e4f426c3cee9
Successfully built e4f426c3cee9
Successfully tagged at_at:latest
WARNING: Image for service at was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating at_at_1 ... done
Attaching to at_at_1
at_1 | Performing system checks...
at_1 |
at_1 | System check identified no issues (0 silenced).
at_1 | May 01, 2020 - 05:35:06
at_1 | Django version 1.11.1, using settings 'AT.settings'
at_1 | Starting development server at http://0.0.0.0:8000/
at_1 | Quit the server with CONTROL-C.
此時你可以瀏覽器訪問了:
補充一些其他命令:
# 后台啟動並且查看log文件
[root@r AT]# docker-compose up -d && docker-compose logs -ft --tail=20 at
see also: