docker 部署django项目
一:实现原理
- 在虚拟机(宿主机)内开启Docker服务
- 拉起1个MySQL容器,这个容器就可以当做一台主机,通过veth设备对 一对一 连接到docker0,映射3306端口
- 拉起1个CentOS容器,也可以当做一台主机,通过veth设备对 一对一 连接到docker0,映射8000端口
- docker0是一个网桥,连通了MySQL容器和CentOS容器
- CentOS容器中的Django项目的数据库用到了MySQL容器,HOST应该是MySQL容器的IP地址,而不是宿主机的IP地址
- CentOS容器中的Django项目启动的时候,是:
0.0.0.0:8000
,外界都可以访问 - 对于虚拟机来说,docker0:
172.17.0.1
就是内网的网关,IP地址:192.168.50.101
就是公网地址 - 所以,在本机可以直接通过访问:
192.168.50.101:8000
来访问Django项目 - 本机可以通过:
192.168.50.101
的3306
端口来连接和管理数据库
二:安装所需的镜像
注意:
以下所有的命令,如果有2个bash代码块窗口,第1个就是
纯命令
,第2个是实际运行的效果
,所以只需参考第一个即可
1.安装 CentOS7.9
这里用的是CentOS7.9
,具体版本是centos:7.9.2009
之后会把这个当做Django项目启动的容器,再安装Python
docker pull centos:7.9.2009
[root@localhost ~]# docker pull centos:7.9.2009
7.9.2009: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7.9.2009
docker.io/library/centos:7.9.2009
2.安装 MySQL5.7
docker pull mysql:5.7.32
[root@localhost ~]# docker pull mysql:5.7.32
5.7.32: Pulling from library/mysql
852e50cd189d: Pull complete
29969ddb0ffb: Pull complete
a43f41a44c48: Pull complete
5cdd802543a3: Pull complete
b79b040de953: Pull complete
938c64119969: Pull complete
7689ec51a0d9: Pull complete
36bd6224d58f: Pull complete
cab9d3fa4c8c: Pull complete
1b741e1c47de: Pull complete
aac9d11987ac: Pull complete
Digest: sha256:8e2004f9fe43df06c3030090f593021a5f283d028b5ed5765cc24236c2c4d88e
Status: Downloaded newer image for mysql:5.7.32
docker.io/library/mysql:5.7.32
3.查看本地已有镜像
docker images
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7.32 ae0658fdbad5 2 weeks ago 449MB
centos 7.9.2009 8652b9f0cb4c 3 weeks ago 204MB
三:启动容器
1.启动 MySQL容器
docker run -dit -p 3306:3306 --name mysql01 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.32
[root@localhost ~]# docker run -dit -p 3306:3306 --name mysql01 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.32
e432e4f92efae4f060042b6e9911f1e6d7202df89a3784e8046b77d4879bf5d9
2.启动 CentOS容器
docker run -dit -p 8000:8000 --name centos01 centos:7.9.2009
[root@localhost ~]# docker run -dit -p 8000:8000 --name centos01 centos:7.9.2009
7c65c2fc3ddc8db998144b41ecdcf80d251f07b2722468490b4d55052b1c39f3
3.查看正在运行的容器
- CentOS容器的端口状况应该是:
0.0.0.0:8000->8000/tcp
- MySQL容器的端口状况应该是:
0.0.0.0:3306->3306/tcp
docker ps
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7c65c2fc3ddc centos:7.9.2009 "/bin/bash" 1 minutes ago Up 1 minutes 0.0.0.0:8000->8000/tcp centos01
e432e4f92efa mysql:5.7.32 "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql01
四:查看 宿主机IP 及 容器IP
1.查看宿主机的IP
- eth0:宿主机的IP地址
192.168.50.101
- docker0:docker中 默认网桥的网关
172.17.0.1
- eth1:vlan的IP
172.16.0.11
- lo:本地回环地址
127.0.0.1
- veth41a90d9:Veth设备对41a90d9 - 联通docker0 和 MySQL容器
- vetha304da9:Veth设备对a304da9 - 联通docker0 和 CentOS容器
ifconfig
[root@localhost ~]# ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
inet6 fe80::42:3fff:fe26:9cc8 prefixlen 64 scopeid 0x20<link>
ether 02:42:3f:26:9c:c8 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5 bytes 446 (446.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.50.101 netmask 255.255.255.0 broadcast 192.168.50.255
inet6 fe80::c725:1123:43a1:c9e4 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:70:89:d7 txqueuelen 1000 (Ethernet)
RX packets 280309 bytes 413770749 (394.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 32395 bytes 2500982 (2.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.0.11 netmask 255.255.0.0 broadcast 172.16.255.255
inet6 fe80::9f4b:56ea:6f4d:d64a prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:70:89:e1 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 16 bytes 1202 (1.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
veth41a90d9: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::f854:d4ff:fe82:2510 prefixlen 64 scopeid 0x20<link>
ether fa:54:d4:82:25:10 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 13 bytes 1102 (1.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
vetha304da9: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::f889:68ff:fef6:33ba prefixlen 64 scopeid 0x20<link>
ether fa:89:68:f6:33:ba txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 656 (656.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
如果使用不了上述的命令,就安装网络工具:yum install net-tools -y
2.查看容器
① 查看 MySQL容器IP
- 重点在
NetworkSettings
中的IPAddress
,这个才是该容器的真实IP - 容器的IP:
172.17.0.2
docker inspect mysql01
...
"NetworkSettings": {
"Bridge": "",
"SandboxID": "ef9a9185ffd8181e5e846d0cce4033713be2e5565f802234423b81ee9c37e1dc",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"3306/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "3306"
}
],
"33060/tcp": null
},
"SandboxKey": "/var/run/docker/netns/ef9a9185ffd8",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "43f07cc4be1b6aba7b2e2cab8001835228a0226040f187efd6f19ea614b20ae3",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2", # 看这里!
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "76f07c0fbd40e71188c3bdf6a221b2cfa2563d75c90e08a8f6a8fa3bd2872ae2",
"EndpointID": "43f07cc4be1b6aba7b2e2cab8001835228a0226040f187efd6f19ea614b20ae3",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
...
② 查看 CentOS容器IP
- 重点在
NetworkSettings
中的IPAddress
,这个才是该容器的真实IP - 容器的IP:
172.17.0.3
docker inspect centos01
...
"NetworkSettings": {
"Bridge": "",
"SandboxID": "1c63adbf165bd66708a0970aec1f8bec5a298f93d0a8b9e3f87d5eb51e39cb28",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"8000/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "8000"
}
]
},
"SandboxKey": "/var/run/docker/netns/1c63adbf165b",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "2346c360a8fb707f5730d38db96c5425e32089a7a234d2cd5f792703da9a8dd2",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3", # 重点在这里!
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "76f07c0fbd40e71188c3bdf6a221b2cfa2563d75c90e08a8f6a8fa3bd2872ae2",
"EndpointID": "2346c360a8fb707f5730d38db96c5425e32089a7a234d2cd5f792703da9a8dd2",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
...
五:设置 MySQL容器
1.进入 MySQL容器
docker exec -it mysql01 bash
[root@localhost ~]# docker exec -it mysql01 bash
root@e432e4f92efa:/#
2.登录、创建数据库blog01
mysql -u root -p123456
create database blog01 default charset utf8;
root@e432e4f92efa:/# mysql -u root -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.32 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database blog01 default charset utf8;
Query OK, 1 row affected (0.00 sec)
此时,本机也是可以通过MySQL连接的工具,来直接连接和管理该数据库的
打开Navicat
- 连接 - MySQL
填写信息(常规)
- 连接名:自定义
- 主机:
localhost
(不用修改) - 端口:
3306
(不用修改,除非映射的端口不是3306) - 用户名:数据库的用户名,默认是
root
- 密码:数据库的密码,刚才设置的是
123456
填写信息(SSH)
- 使用SSH通道:
√
- 主机:虚拟机(宿主机)的IP
- 端口:
22
(固定的ssh端口) - 用户名:虚拟机(宿主机)的用户名,默认是
root
- 密码:虚拟机(宿主机)的密码
点击左下角可以测试连接
点击确定,然后双击打开,就可以看到刚刚创建的数据库:blog01
3.退出MySQL、退出MySQL容器
exit
exit
mysql> exit
Bye
root@e432e4f92efa:/# exit
exit
[root@localhost ~]#
六:设置 CentOS容器
1.进入 CentOS容器
docker exec -it centos01 bash
[root@localhost ~]# docker exec -it centos01 bash
[root@7c65c2fc3ddc /]#
2.安装一些需要的工具
- vim:文本编辑器 - 用于编辑文件
- net-tools:网络工具 - 用于查看网络 和 ping其他网络
- python36:Python解释器 - 用于安装Django项目的依赖
- git:版本控制工具 - 用于拉取项目
yum install vim net-tools python36 git -y
[root@7c65c2fc3ddc /]# yum install vim net-tools python36 git -y
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.163.com
* extras: mirrors.163.com
* updates: mirrors.163.com
...
Complete!
3.测试网络
ping www.baidu.com # 测试是否能访问外网
ping 192.168.50.101 # 测试是否能访问宿主机
ping 172.17.0.2 # 测试是否能访问MySQL容器
[root@7c65c2fc3ddc /]# ping www.baidu.com
PING www.a.shifen.com (110.242.68.4) 56(84) bytes of data.
64 bytes from 110.242.68.4 (110.242.68.4): icmp_seq=1 ttl=127 time=27.4 ms
64 bytes from 110.242.68.4 (110.242.68.4): icmp_seq=2 ttl=127 time=26.4 ms
64 bytes from 110.242.68.4 (110.242.68.4): icmp_seq=3 ttl=127 time=28.6 ms
^C
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2008ms
rtt min/avg/max/mdev = 26.410/27.501/28.669/0.923 ms
[root@7c65c2fc3ddc /]# ping 192.168.50.101
PING 192.168.50.101 (192.168.50.101) 56(84) bytes of data.
64 bytes from 192.168.50.101: icmp_seq=1 ttl=64 time=0.033 ms
64 bytes from 192.168.50.101: icmp_seq=2 ttl=64 time=0.042 ms
64 bytes from 192.168.50.101: icmp_seq=3 ttl=64 time=0.042 ms
^C
--- 192.168.50.101 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2005ms
rtt min/avg/max/mdev = 0.033/0.039/0.042/0.004 ms
[root@7c65c2fc3ddc /]# ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.056 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.102 ms
64 bytes from 172.17.0.2: icmp_seq=3 ttl=64 time=0.048 ms
^C
--- 172.17.0.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2005ms
rtt min/avg/max/mdev = 0.048/0.068/0.102/0.025 ms
4.用git
拉取项目
① 先创建并进入自定义的目录
mkdir /django_test
cd /django_test
[root@7c65c2fc3ddc /]# mkdir /django_test
[root@7c65c2fc3ddc /]# cd /django_test
[root@7c65c2fc3ddc django_test]#
② 拉取Django项目
git clone https://gitee.com/xuexianqi/django_blog.git
[root@7c65c2fc3ddc django_test]# git clone https://gitee.com/xuexianqi/django_blog.git
Cloning into 'django_blog'...
remote: Enumerating objects: 1118, done.
remote: Counting objects: 100% (1118/1118), done.
remote: Compressing objects: 100% (961/961), done.
remote: Total 1118 (delta 133), reused 1084 (delta 118), pack-reused 0
Receiving objects: 100% (1118/1118), 17.45 MiB | 5.33 MiB/s, done.
Resolving deltas: 100% (133/133), done.
③ 查看当前文件夹 是否有该Django项目
ls -l
[root@7c65c2fc3ddc django_test]# ls -l
total 0
drwxr-xr-x. 8 root root 151 Dec 5 09:12 django_blog
5.安装项目所需的依赖
① 切换到该项目的根目录 并查看文件
cd django_blog
ls -l
[root@7c65c2fc3ddc django_test]# cd django_blog
[root@7c65c2fc3ddc django_blog]# ls -l
total 20
drwxr-xr-x. 3 root root 93 Dec 5 09:12 Django_Blog
-rw-r--r--. 1 root root 10396 Dec 5 09:12 README.md
drwxr-xr-x. 5 root root 203 Dec 5 09:12 blog
-rw-r--r--. 1 root root 631 Dec 5 09:12 manage.py
drwxr-xr-x. 7 root root 91 Dec 5 09:12 media
-rw-r--r--. 1 root root 206 Dec 5 09:12 requirements.txt
drwxr-xr-x. 7 root root 63 Dec 5 09:12 static
drwxr-xr-x. 7 root root 181 Dec 5 09:12 templates
② 用pip3
安装依赖
项目所需的依赖都在requirements.txt
中,用pip3
可以一键安装(这里指定pip豆瓣源)
pip3 install -r requirements.txt -i https://pypi.douban.com/simple/
[root@7c65c2fc3ddc django_blog]# pip3 install -r requirements.txt -i https://pypi.douban.com/simple/
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting beautifulsoup4==4.9.3 (from -r requirements.txt (line 1))
Downloading https://pypi.doubanio.com/packages/d1/41/e6495bd7d3781cee623ce23ea6ac73282a373088fcd0ddc809a047b18eae/beautifulsoup4-4.9.3-py3-none-any.whl (115kB)
100% |################################| 122kB 616kB/s
Collecting Django==2.2.2 (from -r requirements.txt (line 2))
Downloading https://pypi.doubanio.com/packages/eb/4b/743d5008fc7432c714d753e1fc7ee56c6a776dc566cc6cfb4136d46cdcbb/Django-2.2.2-py3-none-any.whl (7.4MB)
100% |################################| 7.5MB 267kB/s
Collecting django-mdeditor==0.1.18 (from -r requirements.txt (line 3))
Downloading https://pypi.doubanio.com/packages/16/d6/392b8b2fe2646b0b8080aea68cd68a7c95ff574b90aebe77e5ec22f31872/django-mdeditor-0.1.18.tar.gz (40.1MB)
100% |################################| 40.1MB 46kB/s
Collecting importlib-metadata==2.0.0 (from -r requirements.txt (line 4))
Downloading https://pypi.doubanio.com/packages/6d/6d/f4bb28424bc677bce1210bc19f69a43efe823e294325606ead595211f93e/importlib_metadata-2.0.0-py2.py3-none-any.whl
Collecting Markdown==3.3.3 (from -r requirements.txt (line 5))
Downloading https://pypi.doubanio.com/packages/ac/ef/24a91ca96efa0d7802dffb83ccc7a3c677027bea19ec3c9ee80be740408e/Markdown-3.3.3-py3-none-any.whl (96kB)
100% |################################| 102kB 12.1MB/s
Collecting Pillow==8.0.1 (from -r requirements.txt (line 6))
Downloading https://pypi.doubanio.com/packages/5f/19/d4c25111d36163698396f93c363114cf1cddbacb24744f6612f25b6aa3d0/Pillow-8.0.1-cp36-cp36m-manylinux1_x86_64.whl (2.2MB)
100% |################################| 2.2MB 807kB/s
Collecting Pygments==2.7.2 (from -r requirements.txt (line 7))
Downloading https://pypi.doubanio.com/packages/d8/cf/d7e48846e10ac4f8b89d489cec3dc9c2804a825f812c851559cfbcc251cc/Pygments-2.7.2-py3-none-any.whl (948kB)
100% |################################| 952kB 2.0MB/s
Collecting PyMySQL==0.10.1 (from -r requirements.txt (line 8))
Downloading https://pypi.doubanio.com/packages/1a/ea/dd9c81e2d85efd03cfbf808736dd055bd9ea1a78aea9968888b1055c3263/PyMySQL-0.10.1-py2.py3-none-any.whl (47kB)
100% |################################| 51kB 10.9MB/s
Collecting pytz==2020.4 (from -r requirements.txt (line 9))
Downloading https://pypi.doubanio.com/packages/12/f8/ff09af6ff61a3efaad5f61ba5facdf17e7722c4393f7d8a66674d2dbd29f/pytz-2020.4-py2.py3-none-any.whl (509kB)
100% |################################| 512kB 3.8MB/s
Collecting soupsieve==2.0.1 (from -r requirements.txt (line 10))
Downloading https://pypi.doubanio.com/packages/6f/8f/457f4a5390eeae1cc3aeab89deb7724c965be841ffca6cfca9197482e470/soupsieve-2.0.1-py3-none-any.whl
Collecting sqlparse==0.4.1 (from -r requirements.txt (line 11))
Downloading https://pypi.doubanio.com/packages/14/05/6e8eb62ca685b10e34051a80d7ea94b7137369d8c0be5c3b9d9b6e3f5dae/sqlparse-0.4.1-py3-none-any.whl (42kB)
100% |################################| 51kB 15.3MB/s
Collecting zipp==3.4.0 (from -r requirements.txt (line 12))
Downloading https://pypi.doubanio.com/packages/41/ad/6a4f1a124b325618a7fb758b885b68ff7b058eec47d9220a12ab38d90b1f/zipp-3.4.0-py3-none-any.whl
Installing collected packages: soupsieve, beautifulsoup4, sqlparse, pytz, Django, django-mdeditor, zipp, importlib-metadata, Markdown, Pillow, Pygments, PyMySQL
Running setup.py install for django-mdeditor ... done
Successfully installed Django-2.2.2 Markdown-3.3.3 Pillow-8.0.1 PyMySQL-0.10.1 Pygments-2.7.2 beautifulsoup4-4.9.3 django-mdeditor-0.1.18 importlib-metadata-2.0.0 pytz-2020.4 soupsieve-2.0.1 sqlparse-0.4.1 zipp-3.4.0
③ 修改数据库配置
这个Django项目本身的数据库配置为本地,也就是127.0.0.1
但是现在的项目是跑在CentOS容器中的,但是MySQL却在另一个容器中
所以,要修改数据库配置,把127.0.0.1
替换成MySQL容器的IP:172.17.0.2
# 切换到 Django_Blog 目录
cd Django_Blog
# 用 vim 编辑 settings.py 文件
vim settings.py
# 按 i 进入插入模式
i
# 把 "HOST": "127.0.0.1", 改成 "HOST": "172.17.0.2",
# 把 "USER": os.getenv('user', 'root'), 改成 "USER": 'root',
# 把 "PASSWORD": os.getenv('pwd', '123456'), 改成 "PASSWORD": '123456',
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': "blog01",
"HOST": "172.17.0.2",
"PORT": 3306,
"USER": 'root',
"PASSWORD": '123456',
}
}
# 按 Esc 退出插入模式
Esc
# 按 :wq! 强制保存并退出
:wq!
[root@7c65c2fc3ddc django_blog]# cd Django_Blog
[root@7c65c2fc3ddc Django_Blog]# vim settings.py
④ 执行数据迁移命令
# 切换到上一层目录,因为需要的 manage.py 这个文件在上一层
cd ..
# 执行数据迁移命令(注意:这里会报错!)
python3 manage.py makemigrations
# 根据报错提示 修改文件:/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/operations.py 的第146行
vim /usr/local/lib/python3.6/site-packages/django/db/backends/mysql/operations.py
# 输入命令,显示行号
:set nu
# 用方向键↓找到146行,按 i 进入插入模式
i
# 把:query = query.decode(errors='replace') 的decode 换成 encode
query = query.encode(errors='replace')
# 按 Esc 退出
Esc
# 按 :wq! 强制保存 并退出
:wq!
# 再次执行数据迁移命令
python3 manage.py makemigrations
python3 manage.py migrate
[root@7c65c2fc3ddc Django_Blog]# cd ..
[root@7c65c2fc3ddc django_blog]# python3 manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
...
File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/operations.py", line 146, in last_executed_query
query = query.decode(errors='replace')
AttributeError: 'str' object has no attribute 'decode'
[root@7c65c2fc3ddc django_blog]# vim /usr/local/lib/python3.6/site-packages/django/db/backends/mysql/operations.py
[root@7c65c2fc3ddc django_blog]# python3 manage.py makemigrations
Migrations for 'blog':
blog/migrations/0001_initial.py
- Create model UserInfo
- Create model Article
- Create model Blog
- Create model Log
- Create model Swiper
- Create model Tag
- Create model UpAndDown
- Create model Tag2Article
- Create model Comment
- Create model Category
- Add field blog to article
- Add field category to article
- Add field tag to article
- Add field blog to userinfo
- Add field groups to userinfo
- Add field user_permissions to userinfo
[root@7c65c2fc3ddc django_blog]# python3 manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0001_initial... 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 auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying blog.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying sessions.0001_initial... OK
⑤ 启动项目
- IP和端口号是:
0.0.0.0:8000
,这样外部就可以直接访问了
python3 manage.py runserver 0.0.0.0:8000
[root@7c65c2fc3ddc django_blog]# python3 manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
December 05, 2020 - 17:36:07
Django version 2.2.2, using settings 'Django_Blog.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
⑥ 外界访问项目
在本机的浏览器中,输入虚拟机(宿主机)的IP和CentOS容器(运行的Django项目)的端口号:192.168.50.101:8000
192.168.50.101:8000
每天逼着自己写点东西,终有一天会为自己的变化感动的。这是一个潜移默化的过程,每天坚持编编故事,自己不知不觉就会拥有故事人物的特质的。 Explicit is better than implicit.(清楚优于含糊)