From 6c5016003ae6ab879437eb8377ea65623c1862e8 Mon Sep 17 00:00:00 2001 From: Florent Chehab Date: Sun, 19 Apr 2020 17:18:38 +0200 Subject: [PATCH] refacto(Dockerfile): optimized with new setup --- .dockerignore | 2 ++ Dockerfile | 35 +++++++++++++++++++++++++++------- scripts/server-frontend-dev.js | 9 +++++---- 3 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..27792a5 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +**/node_modules +**/git diff --git a/Dockerfile b/Dockerfile index 7b4b95b..1a1a84a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,26 @@ -FROM node:11 +FROM node:11 as base + +# Create app directory +RUN mkdir -p /opt/app +WORKDIR /opt/app + +# Install app dependencies +COPY ./package.json package-lock.json ./ +RUN npm ci + +# Bundle frontend +COPY src ./src +COPY assets ./assets +COPY config ./config +RUN npm run build + + +##################### +# Final image +##################### + +FROM node:11-alpine +ENV NODE_ENV=prod MAINTAINER cracker0dks @@ -6,12 +28,11 @@ MAINTAINER cracker0dks RUN mkdir -p /opt/app WORKDIR /opt/app -# Install app dependencies -COPY ./package.json /opt/app -RUN npm install +COPY ./package.json ./package-lock.json ./ +RUN npm ci --only=prod -# Bundle app source -COPY . /opt/app +COPY scripts ./scripts +COPY --from=base /opt/app/public ./public EXPOSE 8080 -CMD [ "npm", "start" ] +ENTRYPOINT [ "npm", "run", "start:prod-no-build" ] diff --git a/scripts/server-frontend-dev.js b/scripts/server-frontend-dev.js index a60319f..5e3d762 100644 --- a/scripts/server-frontend-dev.js +++ b/scripts/server-frontend-dev.js @@ -1,7 +1,3 @@ -const webpack = require("webpack"); -const WebpackDevServer = require("webpack-dev-server"); -const config = require("../config/webpack.dev"); - const devServerConfig = { hot: true, inline: true, @@ -19,6 +15,11 @@ const devServerConfig = { } function startFrontendDevServer(port) { + // require here to prevent prod dependency to webpack + const webpack = require("webpack"); + const WebpackDevServer = require("webpack-dev-server"); + const config = require("../config/webpack.dev"); + new WebpackDevServer(webpack(config), devServerConfig) .listen(port, (err) => { if (err) {