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.
This commit is contained in:
Robert Schäfer 2018-12-14 22:41:13 +01:00
parent f9fec054d6
commit bef8aa1d6a
4 changed files with 18 additions and 4 deletions

View File

@ -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:

View File

@ -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

View File

@ -2,6 +2,9 @@ version: "3.7"
services:
backend:
build:
context: .
target: builder
volumes:
- .:/nitro-backend
- /nitro-backend/node_modules

View File

@ -3,7 +3,9 @@ version: "3.7"
services:
backend:
image: humanconnection/nitro-backend:latest
build: .
build:
context: .
target: production
networks:
- hc-network
depends_on: