dockerfile编写前端部署的代码

一般来说前后端分离的话部署就是用nginx做转发

也可以把前端的代码做成静态的文件

server {
    listen       80;
    server_name  0.0.0.0;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

}
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  300;
    client_body_timeout 300;
    client_max_body_size 1000m;
    send_timeout 300;
    gzip  on;

    include /etc/nginx/conf.d/*.conf;
}


# build stage
# ===========
FROM node:lts-alpine as build-stage

# make the 'app' folder the current working directory
WORKDIR /app

# copy both 'package.json' and 'package-lock.json' (if available)
COPY package*.json ./

# install project dependencies
RUN npm install

COPY . /app/
COPY 3rd-party/* /app/node_modules/react-scripts/config/
RUN npm run build

# ================
# production stage
# ================
FROM nginx:1.13 as production-stage
# FROM nginx:stable-alpine as production-stage
COPY nginx.conf /etc/nginx/nginx.conf
COPY default.conf /etc/nginx/conf.d/default.conf
COPY --from=build-stage /app/build /usr/share/nginx/html
EXPOSE 80
FROM python:3.6

# 定义编码
ENV LC_ALL="C.UTF-8" \
  LANG="C.UTF-8"

WORKDIR /opt/app/

COPY . /opt/app/

WORKDIR frontend

RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

RUN apt-get install -y nodejs

RUN npm install -gd express --registry=http://registry.npm.taobao.org /opt/app/frontend/

RUN npm --prefix /opt/app/frontend/ rebuild node-sass

RUN npm --prefix /opt/app/frontend/ run build


WORKDIR /opt/app/backend

RUN pip install  -r requirements.txt

ENV PYTHONPATH=/opt/app/backend:${PYTHONPATH}

ENV project_config=prod

# 定义启动指令
CMD ["python","manage.py"]

你可能感兴趣的:(docker)