docker部署nginx+django项目

一、前期准备

1、docker-compose.yml文件:

version: '2'
services:

  db:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=mysql  # MySQL密码
    expose:
      - "3306"
    volumes:
      - /data/database/api_ts:/var/lib/mysql  # 挂载的文件在服务器上必须存在

    hostname:
      db
    ports:
      - "3308:3306"


  redis:
    image: redis
    hostname: redis
    ports:
      - "6380:6379"

  web:
    build: .  # 路径要与Dockerfile文件对应上
    restart: always
    volumes:
      - .:/home/docker/code/narwel
      - /var/log/:/var/log/
    ports:
      - "81:80"
    links:
      - db
      - redis
    depends_on:
      - db
    entrypoint: supervisord -n  # 默认启动/etc/supervisor/conf.d/supervisor-app.conf

2、Dockerfile文件

FROM python:3.6

RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y \
	nginx \
	supervisor &&\
    rm -rf /var/lib/apt/lists/*

RUN echo "daemon off;" >> /etc/nginx/nginx.conf
COPY ./requirements/base.txt /home/docker/code/
COPY ./fastdfs /home/docker/code/fastdfs
WORKDIR /home/docker/code/
RUN pip install  --default-timeout=500 -r base.txt  --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple && \
    pip install uwsgi --default-timeout=500

COPY deploy/testing/nginx-app.conf /etc/nginx/sites-available/default
COPY deploy/testing/supervisor-app.conf /etc/supervisor/conf.d/

3、nginx-app.conf文件:用于镜像和项目通信

# nginx-app.conf

# the upstream component nginx needs to connect to
upstream django {
    server unix:/home/docker/code/app.sock; # for a file socket
}

# configuration of the server
server {
    # the port your site will be served on, default_server indicates that this server block
    # is the block to use if no blocks match the server_name
    listen      80 default_server;

    # the domain name it will serve for
    server_name 0.0.0.0; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/docker/code/narwel/deploy/testing/uwsgi_params; # the uwsgi_params file you installed
    }
}

# 注意路径

4、supervisor-app.conf文件:启动uwsgi和nginx

[program:app-uwsgi]
command = /usr/local/bin/uwsgi --ini /home/docker/code/narwel/deploy/testing/uwsgi.ini

[program:nginx-app]
command = /usr/sbin/nginx

5、uwsgi.ini文件

[uwsgi]
# this config will be loaded if nothing specific is specified
# load base config from below
ini = :base  # 指定使用哪种配置文件
socket = /home/docker/code/app.sock
master = true
processes = %k
py-autoreload = 1
# background the process & log
logto = /var/log/uwsgi/ms.log   # 文件一定要存在

[dev]
ini = :base
# socket (uwsgi) is not the same as http, nor http-socket
socket = :8001


[local]
ini = :base
http = :8000
# set the virtual env to use
home=/Users/you/envs/env


[base]

# chdir to the folder of this config file, plus app/website
chdir = /home/docker/code/narwel
# load the module from wsgi.py, it is a python path from 
# the directory above.
env = DJANGO_SETTINGS_MODULE=narwel_backstage_backend.settings.testing
module=narwel_backstage_backend.wsgi
# allow anyone to connect to the socket. This is very permissive
chmod-socket=666

6、默认文件

uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;

二、迁移生成镜像

1、进入到docker-compose.yml所在目录执行指令

docker-compose up -d

2、查看docker镜像情况

docker ps

3、查看镜像日志

docker logs 镜像id

4、进入镜像内部

docker exec -it 镜像id bash 

5、如果镜像顺利生成会在对应目录下产生app.sock文件

三、nginx配置

1、后端

server {
    listen 80;
    server_name 后端域名;

    client_max_body_size 1000M;
    location / {
        proxy_pass  http://0.0.0.0:81;
    }
}

2、前端

server {
    listen 80;
    server_name 前端域名;


    location / {
         
         root /home/ubuntu/dist; # 前端文件路径
         index index.html index.htm;
    }
}

四、注意事项

  • MySQL和redis最好不要使用默认端口,避免服务器也装应用,导致和docker生成的混淆;
  • 挂载的文件必须在服务器上已经存在;
  • 路径要对上
发布了61 篇原创文章 · 获赞 11 · 访问量 6459

猜你喜欢

转载自blog.csdn.net/weixin_41449756/article/details/104000105