From bef8aa1d6ab8121b2c4bf78ef4d198350c309a6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= Date: Fri, 14 Dec 2018 22:41:13 +0100 Subject: [PATCH 1/4] Reduce docker image size with multistage build We don't need development dependencies in production. I also added the BUILD_COMMIT environment variable to the image for convenience. --- .travis.yml | 2 +- Dockerfile | 13 +++++++++++-- docker-compose.override.yml | 3 +++ docker-compose.yml | 4 +++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1e832a04d..2de21f27b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,7 +18,7 @@ before_install: - sudo mv docker-compose /usr/local/bin install: - - docker build --build-arg BUILD_COMMIT=$TRAVIS_COMMIT -t humanconnection/nitro-backend:latest . + - docker build --build-arg BUILD_COMMIT=$TRAVIS_COMMIT --target production -t humanconnection/nitro-backend:latest . - docker-compose -f docker-compose.yml up -d script: diff --git a/Dockerfile b/Dockerfile index ad0ce1094..d0080e3c4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,11 @@ -FROM node:10-alpine +FROM node:10-alpine as builder LABEL Description="Backend of the Social Network Human-Connection.org" Vendor="Human Connection gGmbH" Version="0.0.1" Maintainer="Human Connection gGmbH (developer@human-connection.org)" # Expose the app port EXPOSE 4000 +ARG BUILD_COMMIT +ENV BUILD_COMMIT=$BUILD_COMMIT ARG WORKDIR=/nitro-backend RUN mkdir -p $WORKDIR WORKDIR $WORKDIR @@ -11,10 +13,17 @@ WORKDIR $WORKDIR # Install the Application Dependencies COPY package.json . COPY yarn.lock . -RUN yarn install --production=false --frozen-lockfile --non-interactive +RUN yarn install --frozen-lockfile --non-interactive COPY . . COPY .env.template .env RUN yarn run build CMD ["yarn", "run", "start"] + +# reduce image size with a multistage build +FROM node:10-alpine as production +ENV NODE_ENV=production +COPY --from=builder /nitro-backend/dist ./dist +COPY package.json yarn.lock ./ +RUN yarn install --frozen-lockfile --non-interactive diff --git a/docker-compose.override.yml b/docker-compose.override.yml index 3be950735..2a5deb170 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -2,6 +2,9 @@ version: "3.7" services: backend: + build: + context: . + target: builder volumes: - .:/nitro-backend - /nitro-backend/node_modules diff --git a/docker-compose.yml b/docker-compose.yml index 0b070c98e..459496173 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,7 +3,9 @@ version: "3.7" services: backend: image: humanconnection/nitro-backend:latest - build: . + build: + context: . + target: production networks: - hc-network depends_on: From 4e816d902050421bd83064e05f4c62b0d4913a0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= Date: Fri, 14 Dec 2018 23:10:48 +0100 Subject: [PATCH 2/4] Fix (?) the build with an image tag for builder Also I added a base stage so we can have a common working directory. --- Dockerfile | 22 ++++++++-------------- docker-compose.override.yml | 1 + 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/Dockerfile b/Dockerfile index d0080e3c4..ed6c274ce 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,29 +1,23 @@ -FROM node:10-alpine as builder +FROM node:10-alpine as base LABEL Description="Backend of the Social Network Human-Connection.org" Vendor="Human Connection gGmbH" Version="0.0.1" Maintainer="Human Connection gGmbH (developer@human-connection.org)" -# Expose the app port EXPOSE 4000 - ARG BUILD_COMMIT ENV BUILD_COMMIT=$BUILD_COMMIT ARG WORKDIR=/nitro-backend RUN mkdir -p $WORKDIR WORKDIR $WORKDIR - -# Install the Application Dependencies -COPY package.json . -COPY yarn.lock . -RUN yarn install --frozen-lockfile --non-interactive - -COPY . . +COPY package.json yarn.lock ./ COPY .env.template .env - -RUN yarn run build CMD ["yarn", "run", "start"] +FROM base as builder +RUN yarn install --frozen-lockfile --non-interactive +COPY . . +RUN yarn run build + # reduce image size with a multistage build -FROM node:10-alpine as production +FROM base as production ENV NODE_ENV=production COPY --from=builder /nitro-backend/dist ./dist -COPY package.json yarn.lock ./ RUN yarn install --frozen-lockfile --non-interactive diff --git a/docker-compose.override.yml b/docker-compose.override.yml index 2a5deb170..bed30c3eb 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -2,6 +2,7 @@ version: "3.7" services: backend: + image: humanconnection/nitro-backend:builder build: context: . target: builder From 94eea091de8c0fc4f97a4fbd51a1e939fe37285c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= Date: Fri, 14 Dec 2018 23:20:44 +0100 Subject: [PATCH 3/4] Ensure stage `builder` is used for testing But avoid synced volumes, otherwise the deployment will crash because Travis cannot clean up files just before entering the deployment. --- .travis.yml | 2 +- docker-compose.travis.yml | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 docker-compose.travis.yml diff --git a/.travis.yml b/.travis.yml index 2de21f27b..eaba8dba3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,7 +19,7 @@ before_install: install: - docker build --build-arg BUILD_COMMIT=$TRAVIS_COMMIT --target production -t humanconnection/nitro-backend:latest . - - docker-compose -f docker-compose.yml up -d + - docker-compose -f docker-compose.yml -f docker-compose.travis.yml up -d script: - docker-compose exec backend yarn run lint diff --git a/docker-compose.travis.yml b/docker-compose.travis.yml new file mode 100644 index 000000000..761a2aa64 --- /dev/null +++ b/docker-compose.travis.yml @@ -0,0 +1,8 @@ +version: "3.7" + +services: + backend: + image: humanconnection/nitro-backend:builder + build: + context: . + target: builder From 779190f711a28885cefc30d954e6b217e3e8dc4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= Date: Fri, 14 Dec 2018 23:41:11 +0100 Subject: [PATCH 4/4] Run `yarn run dev` in development --- docker-compose.override.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/docker-compose.override.yml b/docker-compose.override.yml index bed30c3eb..ef7d52c7e 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -9,6 +9,7 @@ services: volumes: - .:/nitro-backend - /nitro-backend/node_modules + command: yarn run dev neo4j: ports: - 7687:7687