最近使用Docker封装Django应用发布,遇到了一些问题。接下来是所使用的DockerFile和注意事项:
一:数据库PSQL所需要的文件:
init-user-db.sql:x请修改为自己的值
CREATE USER project_dbuser WITH PASSWORD 'xxxxxxx';
CREATE DATABASE project_db;
GRANT ALL ON DATABASE project_db TO project_dbuser;
DockerFile:x请修改为自己的值
FROM postgres:latest
LABEL maintainer="xxx<xxx@gmail.com>"
LABEL version = "1.0"
LABEL description="my psql images"
ENV POSTGRES_PASSWORD=xxxxxxx
ADD init-user-db.sql /docker-entrypoint-initdb.d/
二、Django应用所需要的文件
DockerFile – Centos8 base:
FROM centos:8
LABEL maintainer="xxxx<xxxx@gmail.com>"
RUN sed -i 's/mirrorlist/
RUN yum groupinstall "Development tools" -y && yum install openldap-devel python3-devel openssl-devel libffi-devel bzip2-devel crontabs wget vim -y
RUN wget https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tgz && tar xvf Python-3.8.5.tgz && cd Python-3.8*/ && ./configure --enable-optimizations && make altinstall && /usr/local/bin/python3.8 -m pip install --upgrade pip
WORKDIR /project_web
ENV RUN_PORT=xxxx
ENV DATABASE_HOST='x.x.x.x'
ENV DATABASE_PORT='xxxx'
EXPOSE $RUN_PORT
ADD . /project_web
RUN echo "Europe/London" > /etc/timezone
RUN pip3.8 install -r requirement.txt
RUN mkdir -p /project_web/projectdb/configurationDir && chmod 777 /project_web/projectdb/configurationDir
VOLUME ['/project_web/projectdb/configurationDir']
RUN python3.8 project_insert_db.py && python3.8 manage.py crontab add && crond restart
RUN python3.8 manage.py makemigrations && python3.8 manage.py migrate
CMD python3.8 manage.py runserver 0.0.0.0:xxxx
DockerFile – Ubuntu 20.04 base:
FROM ubuntu:20.04
LABEL maintainer="xxxx<xxxx@gmail.com>"
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y python3.9 python3.9-dev python3-pip
RUN apt-get install build-essential \
libldap2-dev libsasl2-dev slapd ldap-utils tox \
lcov valgrind cron curl vim -y
RUN curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && python3.9 get-pip.py
WORKDIR /project_web
ENV RUN_PORT=xxxx
ENV DATABASE_HOST='x.x.x.x'
ENV DATABASE_PORT='xxxx'
EXPOSE $RUN_PORT
ADD . /project_web
RUN pip3.9 install -r requirement.txt
RUN echo "Europe/London" > /etc/timezone
RUN mkdir -p /project_web/projectdb/configurationDir && chmod 777 /project_web/projectdb/configurationDir
RUN python3.9 project_insert_db.py && python3.9 manage.py crontab add && service cron restart
RUN python3.9 manage.py makemigrations && python3.9 manage.py migrate
CMD python3.9 manage.py runserver 0.0.0.0:xxxx
如下是笔者所用的python包的requirement.txt,可以按照自己的django应用自行修改:
asgiref==3.5.2
attrs==21.4.0
backports.zoneinfo==0.2.1
bcrypt==3.2.2
cffi==1.15.0
cryptography==37.0.2
Django==4.0.5
django-auth-ldap==4.1.0
django-crontab==0.7.1
future==0.18.2
importlib-resources==5.8.0
jsonschema==4.6.0
ldap3==2.9.1
netmiko==4.1.0
ntc-templates==3.0.0
paramiko==2.11.0
pip==22.1.2
ply==3.11
psycopg2-binary==2.9.3
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycparser==2.21
pycryptodomex==3.14.1
PyNaCl==1.5.0
pyproject-toml==0.0.10
pyrsistent==0.18.1
pyserial==3.5
pysmi==0.3.4
pysnmp==4.4.12
python-ldap==3.4.0
PyYAML==6.0
scp==0.14.4
setuptools==47.1.0
six==1.16.0
sqlparse==0.4.2
tenacity==8.0.1
textfsm==1.1.2
toml==0.10.2
wheel==0.37.1
zipp==3.8.0
注意事项:
- 如果遇到crontab无法正常运行的情况,请删除django中的环境变量;
- PSQL数据库请自己先调试完成,再启动django应用的容器;
- 遇到文件操作无法写入容器的情况,保证容器中对应存储目录有权限后,还需要进行卷挂载;
- 如果想要在django容器没有启动的情况下使用python向psql数据库写入数据,请直接使用psql的语法而不是django的ORM。
文章如有误,请指正。
|