From 7b0c3d55795d8778d533f31162c287ad29104fe6 Mon Sep 17 00:00:00 2001 From: custertian Date: Fri, 15 Mar 2019 17:10:45 +0800 Subject: [PATCH] add docker-compose start --- .dockerignore | 8 ++++++++ .env | 1 + Dockerfile-dev | 15 +++++++++++++++ Dockerfile-prod | 41 ++++++++++++++++++++++++++++++++++++++++ README.md | 14 ++++++++++++++ conf/conf.d/default.conf | 12 ++++++++++++ docker-compose-dev.yml | 25 ++++++++++++++++++++++++ docker-compose-prod.yml | 23 ++++++++++++++++++++++ nginx/Dockerfile-dev | 4 ++++ nginx/Dockerfile-prod | 4 ++++ nginx/dev.conf | 16 ++++++++++++++++ nginx/prod.conf | 16 ++++++++++++++++ 12 files changed, 179 insertions(+) create mode 100644 .dockerignore create mode 100644 .env create mode 100644 Dockerfile-dev create mode 100644 Dockerfile-prod create mode 100644 conf/conf.d/default.conf create mode 100644 docker-compose-dev.yml create mode 100644 docker-compose-prod.yml create mode 100644 nginx/Dockerfile-dev create mode 100644 nginx/Dockerfile-prod create mode 100644 nginx/dev.conf create mode 100644 nginx/prod.conf diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..bc9fc14e --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +node_modules +coverage +build +env +htmlcov +.dockerignore +Dockerfile-dev +Dockerfile-prod \ No newline at end of file diff --git a/.env b/.env new file mode 100644 index 00000000..b885521b --- /dev/null +++ b/.env @@ -0,0 +1 @@ +NODE_PATH=./src diff --git a/Dockerfile-dev b/Dockerfile-dev new file mode 100644 index 00000000..34c22e13 --- /dev/null +++ b/Dockerfile-dev @@ -0,0 +1,15 @@ +# base image +FROM node:11.6.0-alpine + +# set working directory +WORKDIR /usr/src/app + +# add `/usr/src/app/node_modules/.bin` to $PATH +ENV PATH /usr/src/app/node_modules/.bin:$PATH + +# install and cache app dependencies +COPY package.json /usr/src/app/package.json +RUN npm install --silent + +# start app +CMD ["npm", "run", "serve"] diff --git a/Dockerfile-prod b/Dockerfile-prod new file mode 100644 index 00000000..f417f516 --- /dev/null +++ b/Dockerfile-prod @@ -0,0 +1,41 @@ + +########### +# BUILDER # +########### + +# base image +FROM node:11.6.0-alpine as builder + +# set working directory +WORKDIR /usr/src/app + +# install app dependencies +ENV PATH /usr/src/app/node_modules/.bin:$PATH +COPY package.json /usr/src/app/package.json +RUN npm install --silent + + +# create build +COPY . /usr/src/app +RUN npm run build + + +######### +# FINAL # +######### + +# base image +FROM nginx:1.15.8-alpine + +# update nginx conf +RUN rm -rf /etc/nginx/conf.d +COPY conf /etc/nginx + +# copy static files +COPY --from=builder /usr/src/app/dist /usr/share/nginx/html + +# expose port +EXPOSE 80 + +# run nginx +CMD ["nginx", "-g", "daemon off;"] diff --git a/README.md b/README.md index 8b5551e5..f4fea356 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,20 @@ npm install or yarn npm run serve or yarn (run) serve ``` +## Docker 快速安装 + +开发环境: + +```sh +docker-compose -f docker-compose-dev.yml up -d --build +``` + +生产环境: + +```sh +docker-compose -f docker-compose-prod.yml up -d --build +``` + ## 下个版本开发计划 - [ ] 完成插件机制 diff --git a/conf/conf.d/default.conf b/conf/conf.d/default.conf new file mode 100644 index 00000000..c6a436aa --- /dev/null +++ b/conf/conf.d/default.conf @@ -0,0 +1,12 @@ +server { + listen 80; + location / { + root /usr/share/nginx/html; + index index.html index.htm; + try_files $uri $uri/ /index.html; + } + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } +} \ No newline at end of file diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml new file mode 100644 index 00000000..94865a71 --- /dev/null +++ b/docker-compose-dev.yml @@ -0,0 +1,25 @@ +version: '3.7' + +services: + + client: + build: + context: . + dockerfile: Dockerfile-dev + volumes: + - '../lin-cms-vue:/usr/src/app' + - '/usr/src/app/node_modules' + ports: + - 8080:8080 + environment: + - NODE_ENV=development + + nginx: + build: + context: ./nginx + dockerfile: Dockerfile-dev + restart: always + ports: + - 80:80 + depends_on: + - client \ No newline at end of file diff --git a/docker-compose-prod.yml b/docker-compose-prod.yml new file mode 100644 index 00000000..2dac6491 --- /dev/null +++ b/docker-compose-prod.yml @@ -0,0 +1,23 @@ +version: '3.7' + +services: + + client: + container_name: client + build: + context: . + dockerfile: Dockerfile-prod + args: + - NODE_ENV=production + expose: + - 80 + + nginx: + build: + context: ./nginx + dockerfile: Dockerfile-prod + restart: always + ports: + - 80:80 + depends_on: + - client \ No newline at end of file diff --git a/nginx/Dockerfile-dev b/nginx/Dockerfile-dev new file mode 100644 index 00000000..48656393 --- /dev/null +++ b/nginx/Dockerfile-dev @@ -0,0 +1,4 @@ +FROM nginx:1.15.8-alpine + +RUN rm /etc/nginx/conf.d/default.conf +COPY /dev.conf /etc/nginx/conf.d \ No newline at end of file diff --git a/nginx/Dockerfile-prod b/nginx/Dockerfile-prod new file mode 100644 index 00000000..cfdbf624 --- /dev/null +++ b/nginx/Dockerfile-prod @@ -0,0 +1,4 @@ +FROM nginx:1.15.8-alpine + +RUN rm /etc/nginx/conf.d/default.conf +COPY /prod.conf /etc/nginx/conf.d \ No newline at end of file diff --git a/nginx/dev.conf b/nginx/dev.conf new file mode 100644 index 00000000..6407b6ae --- /dev/null +++ b/nginx/dev.conf @@ -0,0 +1,16 @@ +server { + + listen 80; + + location / { + proxy_pass http://client:8080; + proxy_http_version 1.1; + proxy_redirect default; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Host $server_name; + } +} \ No newline at end of file diff --git a/nginx/prod.conf b/nginx/prod.conf new file mode 100644 index 00000000..548ed297 --- /dev/null +++ b/nginx/prod.conf @@ -0,0 +1,16 @@ +server { + + listen 80; + + location / { + proxy_pass http://client:80; + proxy_http_version 1.1; + proxy_redirect default; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Host $server_name; + } +} \ No newline at end of file