Reduce docker image size

This commit is contained in:
Stefan Forstenlechner 2022-05-28 13:24:43 +02:00
parent 42799389b5
commit 2ecdb07d65
2 changed files with 27 additions and 24 deletions

View File

@ -1,44 +1,36 @@
FROM node:16.14.2 as client-builder
FROM node:16.14.2-alpine as client-builder
WORKDIR /usr/src/app
COPY picture-gallery-client/package*.json picture-gallery-client/
COPY picture-gallery-client/package*.json /usr/src/app/picture-gallery-client/
WORKDIR /usr/src/app/picture-gallery-client
RUN npm ci --only=production
COPY picture-gallery-client .
RUN npm run build-client
FROM node:16.14.2 as server-builder
RUN mkdir built && \
mv build built && \
mv package.minimize.docker.json built/package.json && \
cp package-lock.json built && \
npm --prefix built/ ci --only=production
WORKDIR /usr/src/app
COPY picture-gallery-server/package*.json picture-gallery-server/
FROM node:16.14.2-alpine as server-builder
COPY picture-gallery-server/package*.json /usr/src/app/picture-gallery-server/
WORKDIR /usr/src/app/picture-gallery-server
RUN npm ci
COPY picture-gallery-server .
RUN npm run build-server
FROM node:16.14.2
RUN mkdir built && \
mv dist node_modules built
WORKDIR /usr/src/app
FROM node:16.14.2-alpine
COPY picture-gallery-client/package*.json picture-gallery-client/
COPY picture-gallery-server/package*.json picture-gallery-server/
COPY --from=client-builder /usr/src/app/picture-gallery-client/build /usr/src/app/picture-gallery-client/build
COPY --from=server-builder /usr/src/app/picture-gallery-server/dist /usr/src/app/picture-gallery-server/dist
RUN chown node:node -R /usr/src/app/picture-gallery-client/build && \
chown node:node -R /usr/src/app/picture-gallery-server/dist
# only needed for "react-inject-env"
WORKDIR /usr/src/app/picture-gallery-client
RUN npm ci --only=production
WORKDIR /usr/src/app/picture-gallery-server
RUN npm ci --only=production
COPY --from=client-builder --chown=node:node /usr/src/app/picture-gallery-client/built /usr/src/app/picture-gallery-client/
COPY --from=server-builder --chown=node:node /usr/src/app/picture-gallery-server/built /usr/src/app/picture-gallery-server/
VOLUME /usr/src/app/public
EXPOSE 3001
USER node
# we are still in the server directory
WORKDIR /usr/src/app/picture-gallery-server
CMD npm --prefix ../picture-gallery-client/ run set-environment && node dist/app.js

View File

@ -0,0 +1,11 @@
{
"name": "picture-gallery-client",
"version": "0.1.0",
"private": true,
"dependencies": {
"react-inject-env": "2.1.0"
},
"scripts": {
"set-environment": "npx react-inject-env set"
}
}