被拉过去帮忙给项目部署网站,之前从来没有接触过这些,现学了一些知识,记录一下过程和过程中遇到的一些问题
前置环境要求
- 安装
vim
、curl
、git
、pip
、nginx
、net-tools
curl
是为了安装docker
,但也可以通过pip
安装,就不需要安装curl
了
net-tools
用来使用ifconfig
查ip
地址
安装指令也很简单,就是sudo apt install xxx
,安装之前可以先更新一下sudo apt update
个人不习惯进su
(因为我的zsh
主题好看!),但是经常需要输入密码,嫌麻烦可以sudo su
安装
docker
和docker-compose
docker
sudo curl -fsSL get.docker.com -o get-docker.sh sudo sh get-docker.sh rm get-docker.sh
docker
安装方法网上还有很多,比如sudo pip install docker
,但我看这个顺眼(docker-compose
sudo pip install docker-compose
直接安装会遇到报错:
ERROR: docker 7.0.0 has requirement requests>=2.26.0, but you'll have requests 2.22.0 which is incompatible. Installing collected packages: docopt, dockerpty, distro, attrs, pyrsistent, jsonschema, python-dotenv, websocket-client, urllib3, packaging, bcrypt, pycparser, cffi, pynacl, cryptography, paramiko, docker, texttable, docker-compose Attempting uninstall: distro Found existing installation: distro 1.4.0 Not uninstalling distro at /usr/lib/python3/dist-packages, outside environment /usr Can't uninstall 'distro'. No files were found to uninstall. Attempting uninstall: urllib3 Found existing installation: urllib3 1.25.8 Not uninstalling urllib3 at /usr/lib/python3/dist-packages, outside environment /usr Can't uninstall 'urllib3'. No files were found to uninstall. Attempting uninstall: pynacl Found existing installation: PyNaCl 1.3.0 Not uninstalling pynacl at /usr/lib/python3/dist-packages, outside environment /usr Can't uninstall 'PyNaCl'. No files were found to uninstall. Attempting uninstall: cryptography Found existing installation: cryptography 2.8 Not uninstalling cryptography at /usr/lib/python3/dist-packages, outside environment /usr Can't uninstall 'cryptography'. No files were found to uninstall. Successfully installed attrs-23.2.0 bcrypt-4.1.2 cffi-1.16.0 cryptography-42.0.1 distro-1.9.0 docker-7.0.0 docker-compose-1.29.2 dockerpty-0.4.1 docopt-0.6.2 jsons
嗯其实看第一句就好了:
has requirement requests>=2.26.0
所以要先升级requests
pip install --upgrade requests
部署网站
先把项目
git clone
到本地,根据dockerfile
中的内容在根目录指定路径创建数据库文件夹ifconfig
查本地ip
之后把项目文件中settings
和nginx.conf
中用到ip
的地方改成本地ip
接下来在有
dockerfile
的文件夹下生成容器docker-compose up -d
有可能遇到报错
Traceback (most recent call last): File "/usr/local/bin/docker-compose", line 8, in <module> sys.exit(main()) File "/usr/local/lib/python3.8/dist-packages/compose/cli/main.py", line 81, in main command_func() File "/usr/local/lib/python3.8/dist-packages/compose/cli/main.py", line 200, in perform_command project = project_from_options('.', options) File "/usr/local/lib/python3.8/dist-packages/compose/cli/command.py", line 60, in project_from_options return get_project( File "/usr/local/lib/python3.8/dist-packages/compose/cli/command.py", line 152, in get_project client = get_client( File "/usr/local/lib/python3.8/dist-packages/compose/cli/docker_client.py", line 41, in get_client client = docker_client( File "/usr/local/lib/python3.8/dist-packages/compose/cli/docker_client.py", line 124, in docker_client kwargs = kwargs_from_env(environment=environment, ssl_version=tls_version) TypeError: kwargs_from_env() got an unexpected keyword argument 'ssl_version'
把
-
去掉就好了docker compose up -d
容器里运行
makemigrations
和migrate
来生成相应文件夹docker ps
查容器名,然后执行指令docker exec name python manage.py makemigrations
docker exec name python manage.py migrate
如果
settings
里的ip
改错了就可能遇到报错Traceback (most recent call last): File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection self.connect() File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 270, in connect self.connection = self.get_new_connection(conn_params) File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) File "/usr/local/lib/python3.9/site-packages/django/db/backends/mysql/base.py", line 247, in get_new_connection connection = Database.connect(**conn_params) File "/usr/local/lib/python3.9/site-packages/MySQLdb/init.py", line 121, in Connect return Connection(*args, **kwargs) File "/usr/local/lib/python3.9/site-packages/MySQLdb/connections.py", line 193, in init super().init(*args, **kwargs2) MySQLdb.OperationalError: (2002, "Can't connect to server on 'wrong ip' (115)")
此时需要删除容器和镜像,将
ip
修改正确之后再次运行docker rmi <your-image-id>
docker rm <your-docker-id>
其中
id
可以通过docker ps
查看
将项目中的
nginx.conf
替换/etc/nginx/sites-available
中的default
(要把名字也改成default
),再加载nginx
nginx -s reload
nginx -t
最后将项目文件中的
dist
文件复制到根目录创建的文件夹下
以上只针对这次项目的部署,很多环境在给的dockerfile
中配好了,我也没怎么去研究,仅记录一下中间遇到的一些问题,虽然是一些小问题但是耗了我两三个小时…嗯仅供参考吧