feat(docker): added docker multi stage build
This commit is contained in:
+26
-5
@@ -1,13 +1,29 @@
|
|||||||
FROM node:18-alpine
|
# build stage for building .ts files
|
||||||
|
FROM node:18-alpine as build
|
||||||
|
|
||||||
|
RUN mkdir /home/app
|
||||||
|
|
||||||
|
WORKDIR /home/app
|
||||||
|
|
||||||
|
COPY package.json .
|
||||||
|
|
||||||
|
RUN npm install --ignore-scripts
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
RUN npm run build
|
||||||
|
|
||||||
|
# prod stage for including only necessary files
|
||||||
|
FROM node:18-alpine as prod
|
||||||
|
|
||||||
LABEL description="Aniwatch API docker image"
|
|
||||||
LABEL org.opencontainers.image.source https://github.com/ghoshRitesh12/aniwatch-api
|
LABEL org.opencontainers.image.source https://github.com/ghoshRitesh12/aniwatch-api
|
||||||
|
LABEL org.opencontainers.image.description "Aniwatch API docker image"
|
||||||
|
|
||||||
# create a non-privileged user
|
# create a non-privileged user
|
||||||
RUN addgroup -S aniwatch && adduser -S zoro -G aniwatch
|
RUN addgroup -S aniwatch && adduser -S zoro -G aniwatch
|
||||||
|
|
||||||
# set secure folder permissions
|
# set secure folder permissions
|
||||||
RUN mkdir /app && chown -R zoro:aniwatch /app
|
RUN mkdir -p /app/public /app/dist && chown -R zoro:aniwatch /app
|
||||||
|
|
||||||
# set non-privileged user
|
# set non-privileged user
|
||||||
USER zoro
|
USER zoro
|
||||||
@@ -21,8 +37,11 @@ COPY --chown=zoro:aniwatch package.json .
|
|||||||
# install dependencies
|
# install dependencies
|
||||||
RUN npm install --omit=dev --ignore-scripts
|
RUN npm install --omit=dev --ignore-scripts
|
||||||
|
|
||||||
# copy all necessary files in the container (except ones in .dockerignore)
|
# copy public folder from build stage to prod
|
||||||
COPY --chown=zoro:aniwatch . .
|
COPY --from=build --chown=zoro:aniwatch /home/app/public /app/public
|
||||||
|
|
||||||
|
# copy dist folder from build stage to prod
|
||||||
|
COPY --from=build --chown=zoro:aniwatch /home/app/dist /app/dist
|
||||||
|
|
||||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s CMD [ "npm", "run", "healthcheck" ]
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s CMD [ "npm", "run", "healthcheck" ]
|
||||||
|
|
||||||
@@ -33,3 +52,5 @@ ENV PORT=4000
|
|||||||
EXPOSE 4000
|
EXPOSE 4000
|
||||||
|
|
||||||
CMD [ "node", "dist/server.js" ]
|
CMD [ "node", "dist/server.js" ]
|
||||||
|
|
||||||
|
# exit
|
||||||
Reference in New Issue
Block a user