From 48dbb07698cdf614fc0f833f6a6a244810907153 Mon Sep 17 00:00:00 2001 From: marvzhang Date: Thu, 19 Sep 2019 14:24:27 +0800 Subject: [PATCH] updated Dockerfile --- .gitignore | 1 + Dockerfile | 6 ++++-- Dockerfile-local | 18 ++++++++++++++++++ backend/package.json | 3 ++- docker-compose.yaml | 13 +++++++++++-- docker_init.sh | 17 +++++++++++++++++ nginx/artipub.conf | 7 +++++++ server.js | 1 + src/pages/document.ejs | 2 +- 9 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 Dockerfile-local create mode 100755 docker_init.sh create mode 100644 nginx/artipub.conf diff --git a/.gitignore b/.gitignore index 2eb482b..11fcf0c 100755 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ yarn.lock .DS_Store package-lock.json .umi/ +dist/ diff --git a/Dockerfile b/Dockerfile index 9a60e34..0cac8f4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM jelastic/nodejs:8.16.1-npm AS frontend WORKDIR /app ADD . /app -RUN npm install --registry=https://registry.npm.taobao.org +RUN npm install RUN npm run build FROM jelastic/nodejs:8.16.1-npm @@ -12,5 +12,7 @@ ADD . /app COPY --from=frontend /app/dist /frontend RUN cp ./backend/package.json . RUN npm install --registry=https://registry.npm.taobao.org +RUN yum install -y nginx +RUN cp /app/nginx/artipub.conf /etc/nginx/conf.d -CMD npm run start:backend +CMD /app/docker_init.sh diff --git a/Dockerfile-local b/Dockerfile-local new file mode 100644 index 0000000..207506f --- /dev/null +++ b/Dockerfile-local @@ -0,0 +1,18 @@ +FROM jelastic/nodejs:8.16.1-npm AS frontend + +WORKDIR /app +ADD . /app +RUN npm install --registry=https://registry.npm.taobao.org +RUN npm run build + +FROM jelastic/nodejs:8.16.1-npm + +WORKDIR /app +ADD . /app +COPY --from=frontend /app/dist /frontend +RUN cp ./backend/package.json . +RUN npm install --registry=https://registry.npm.taobao.org +RUN yum install -y nginx +RUN cp /app/nginx/artipub.conf /etc/nginx/conf.d + +CMD /app/docker_init.sh diff --git a/backend/package.json b/backend/package.json index 5dec7c5..1798d96 100644 --- a/backend/package.json +++ b/backend/package.json @@ -4,7 +4,8 @@ "description": "", "main": "server.js", "scripts": { - "start": "node server.js" + "start": "node server.js", + "start:backend": "node server.js" }, "author": "", "license": "ISC", diff --git a/docker-compose.yaml b/docker-compose.yaml index 35a66cf..2a55d0f 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,7 +1,16 @@ version: '3.3' services: - master: + app: image: "tikazyq/artipub:latest" + environment: + MONGO_HOST: "mongo" ports: - - "8080:8080" # frontend + - "8000:8000" # frontend - "3000:3000" # backend + depends_on: + - mongo + mongo: + image: mongo:latest + restart: always + ports: + - "27017:27017" diff --git a/docker_init.sh b/docker_init.sh new file mode 100755 index 0000000..2f5a5a8 --- /dev/null +++ b/docker_init.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +# replace original url to new one +if [ "${ARTIPUB_API_ADDRESS}" = "" ]; +then + : +else + jspath=`ls /frontend/umi.*.js` + sed -i "s?localhost:3000?${ARTIPUB_API_ADDRESS}?g" ${jspath} +fi + +# start frontend +nginx -s stop | true +nginx + +# start backend +npm run start:backend diff --git a/nginx/artipub.conf b/nginx/artipub.conf new file mode 100644 index 0000000..dc78478 --- /dev/null +++ b/nginx/artipub.conf @@ -0,0 +1,7 @@ +server { + listen 8000; + location / { + root /frontend; + index index.html; + } +} diff --git a/server.js b/server.js index 163f22e..a78558f 100644 --- a/server.js +++ b/server.js @@ -13,6 +13,7 @@ const logger = require('./logger') const app = express() // 环境变量覆盖 +console.log(process.env) if (process.env.MONGO_HOST) config.MONGO_HOST = process.env.MONGO_HOST if (process.env.MONGO_PORT) config.MONGO_PORT = process.env.MONGO_PORT if (process.env.MONGO_DB) config.MONGO_DB = process.env.MONGO_DB diff --git a/src/pages/document.ejs b/src/pages/document.ejs index 7710be6..5d1b48c 100644 --- a/src/pages/document.ejs +++ b/src/pages/document.ejs @@ -9,7 +9,7 @@ /> ArtiPub - +