Skip to content

Commit 082037f

Browse files
add node images
Signed-off-by: ivan katliarchuk <[email protected]>
1 parent f834278 commit 082037f

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ docker run --rm test "kubectl version --short --client=true" | awk '{print $3}'
187187
1. [X] Renovate configuration
188188
1. [ ] Use `requirementes.txt` file with python packages
189189
1. [X] Review missing images
190+
1. [ ] Build node images `node`
190191

191192
<!-- resources -->
192193
[opa.badge]: https://github.com/cloudkats/docker-tools/actions/workflows/policy.opa.yaml/badge.svg
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#image from https://snyk.io/blog/choosing-the-best-node-js-docker-image/
2+
# notes from https://snyk.io/blog/10-best-practices-to-containerize-nodejs-web-applications-with-docker/
3+
4+
# --------------> The build image
5+
FROM node:20.12.1-bookworm-slim AS build
6+
RUN apt-get update && apt-get install -y --no-install-recommends dumb-init
7+
8+
ARG APP_VERSION
9+
ENV APP_VERSION=$APP_VERSION
10+
ENV APP_USER 999
11+
ENV APP_GROUP node
12+
ENV WORKDIR /usr/src/app
13+
#ENV NODE_ENV production #not needed in this stage
14+
15+
# difficult to change /.npm to a different non-root path
16+
#ENV NPM_CONFIG_PREFIX=/home/node/.npm-global
17+
## optionally if you want to run npm global bin without specifying path
18+
#ENV PATH=$PATH:/home/node/.npm-global/bin
19+
20+
RUN echo "APP_VERSION: $APP_VERSION"
21+
RUN echo "USER: $APP_USER"
22+
RUN echo "GROUP: $APP_GROUP"
23+
RUN echo "WORKDIR: $WORKDIR"
24+
25+
# wont work without changing /.npm to non-root
26+
#ARG USER=$APP_USER:$APP_GROUP
27+
#USER ${APP_USER}
28+
29+
WORKDIR $WORKDIR
30+
31+
COPY package*.json ./
32+
33+
# copy npmrc securely
34+
# and run npm ci to install node deps
35+
RUN --mount=type=secret,mode=0644,id=npmrc,target=/usr/src/app/.npmrc npm ci --only=production
36+
37+
# --------------> The production image__
38+
FROM node:20.12.1-bookworm-slim AS production
39+
40+
ENV APP_VERSION=$APP_VERSION
41+
ENV APP_USER 999
42+
ENV APP_GROUP node
43+
ENV WORKDIR /usr/src/app
44+
ENV NODE_ENV production
45+
46+
WORKDIR $WORKDIR
47+
48+
RUN echo "NODE_ENV: $NODE_ENV"
49+
RUN echo "WORKDIR: $WORKDIR"
50+
51+
COPY --from=build /usr/bin/dumb-init /usr/bin/dumb-init
52+
COPY --chown=$APP_USER:$APP_GROUP --from=build /usr/src/app/node_modules /usr/src/app/node_modules
53+
54+
COPY --chown=$APP_USER:$APP_GROUP . /usr/src/app
55+
56+
ENV PORT 5000
57+
EXPOSE $PORT
58+
59+
ARG USER=$APP_USER:$APP_GROUP
60+
USER ${APP_USER}
61+
62+
CMD ["dumb-init", "node", "src/index.js"]

node/bookworm/Dockerfile

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# docker build -t node-local .
2+
# docker run --rm -it --entrypoint /bin/sh node-local
3+
FROM node:20-bookworm-slim
4+
5+
# Setting working directory. All the path will be relative to WORKDIR
6+
WORKDIR /usr/src/app
7+
8+
# ARG CI_JOB_TOKEN
9+
ENV HUSKY 0
10+
# ENV CI_JOB_TOKEN=$CI_JOB_TOKEN
11+
12+
ENV APP_USER 999
13+
ENV APP_GROUP appuser
14+
15+
COPY --chown=$APP_USER:$APP_GROUP ./build ./build
16+
COPY --chown=$APP_USER:$APP_GROUP ./public ./public
17+
18+
WORKDIR /usr/src/app
19+
20+
# Copying source files
21+
COPY --chown=$APP_USER:$APP_GROUP ./server ./server
22+
23+
WORKDIR /usr/src/app/server
24+
25+
# Remove files not needed
26+
RUN rm -f package-lock.json \
27+
&& rm -f nodemon.json \
28+
&& rm -f tsconfig.json \
29+
&& rm -rf src \
30+
&& ls
31+
32+
EXPOSE 3003
33+
34+
RUN echo "USER: $APP_USER" && echo "GROUP: $APP_GROUP"
35+
36+
# create user and add to node group
37+
RUN useradd -l --system -s /bin/false -g node -u $APP_USER --badname $APP_USER
38+
39+
USER $APP_USER
40+
41+
#CMD ["node", "server/index.js"]
42+
CMD ["npm","run", "start"]

0 commit comments

Comments
 (0)