使用docker-compose+nginx+uwsgi+django部署项目
(1)centos上下载docker + docker-compose
(2)基础目录
(3)首先创建一个纯净的python+django+uwsgi的镜像,便于后期使用(也可不用创建,后期docker-compose的时候再创建python镜像,这里我们先创建,后期直接把项目放进去,不用每次都下载环境)
创建python+django+uwsgi的纯净镜像,命名镜像名为django:
#Dockerfile 这个dockrfile不是基础目录中的Dockerfile,需要在其他目录中创建 FROM python RUN mkdir /code ADD ./requirements.txt /code
WORKDIR /code RUN pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt RUN rm -f requirements.txt
#requirements.txt
django==2.2.2 psycopg2 uwsgi
(4)基础目录中的Dockerfile
#使用刚刚创建的基础镜像django FROM django WORKDIR /code RUN mkdir hello #创建项目目录 ADD . /code/hello
(5)创建uwsgi配置文件
创建conf/uwsgi.ini配置文件
#conf/uwsgi.ini [uwsgi] socket = 0.0.0.0:8000 chdir = /code/hello #注意:在这里的路径是容器内的项目所在的路径,而不是宿主机放项目的路径 module = hello.wsgi #daemonize = uwsgi.log master = True processes = 4
(6)创建nginx配置文件
创建nginx/nginc.conf
#nginx/nginx.conf
events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; server { listen 80; charset utf-8; location / { include uwsgi_params; uwsgi_pass 10.127.2.3:8000; #端口要和uwsgi里配置的一样 #uwsgi_param UWSGI_SCRIPT hello.wsgi; #wsgi.py所在的目录名+.wsgi #uwsgi_param UWSGI_CHDIR /opt/deploy/hello; #项目路径 } location /static/ { alias /code/hello/static/; #静态资源路径 #注意:在这里的路径是容器内的项目所在的路径,而不是宿主机放项目的路径 } }
(7)创建nginx的Dockerfile
创建nginx/Dockerfile
#nginx/Dockerfile FROM nginx WORKDIR /etc/nginx/ RUN cp nginx.conf ./nginx.conf.bak COPY nginx.conf ./ CMD ["nginx", "-g", "daemon off;"]
(8)创建docker-compose.yml
version: '3' services: db: image: postgres restart: always environment: POSTGRES_PASSWORD: 12345 networks: net-django: ipv4_address: 10.127.2.4 web: build: . #command: python3 manage.py runserver 0.0.0.0:8000 privileged: true #ports: # - 8000:8000 depends_on: - db networks: net-django: ipv4_address: 10.127.2.3 #command: uwsgi --chdir=/code/hello --module=hello.wsgi --master --socket 0.0.0.0:8000 command: uwsgi --ini /code/hello/conf/uwsgi.ini #注意:在这里的路径是容器内的项目所在的路径,而不是宿主机放项目的路径 nginx: container_name: nginx-container restart: always depends_on: - web links: - "web:web" build: ./nginx ports: - 8080:80 networks: net-django: ipv4_address: 10.127.2.2 networks: net-django: ipam: config: - subnet: 10.127.2.0/24
(9)最后创建容器
docker-compose build docker-compose up -d
(10)容器创建启动后,登录x.x.x.x:8080即可