|
| 1 | +# First stage: cache the dev dependencies. |
1 | 2 | FROM node:alpine as dev-dependencies
|
2 | 3 | WORKDIR /app
|
3 | 4 | COPY package.json package-lock.json ./
|
4 | 5 | RUN npm ci
|
| 6 | +# Create the user and group files that will be used in the running container to |
| 7 | +# run the process as an unprivileged user. |
| 8 | +RUN mkdir /user && \ |
| 9 | + echo 'nobody:x:65534:65534:nobody:/:' > /user/passwd && \ |
| 10 | + echo 'nobody:x:65534:' > /user/group |
5 | 11 |
|
| 12 | +# Second stage: cache the prod dependencies. |
6 | 13 | FROM node:alpine as prod-dependencies
|
7 | 14 | WORKDIR /app
|
8 | 15 | COPY package.api.json package.json
|
9 | 16 | RUN npm i --production
|
10 | 17 |
|
| 18 | +# Third stage: build the executable. |
11 | 19 | FROM node:alpine as builder
|
12 | 20 | WORKDIR /app
|
13 | 21 | COPY --from=dev-dependencies /app /app
|
14 | 22 | COPY apps/api apps/api
|
| 23 | +COPY libs/models libs/models |
| 24 | +COPY libs/utils libs/utils |
15 | 25 | COPY angular.json nx.json tsconfig.json ./
|
16 | 26 | ENV NODE_ENV production
|
17 |
| -RUN $(npm bin)/rimraf dist && $(npm bin)/ng build api --prod |
| 27 | +RUN $(npm bin)/ng build api --prod |
18 | 28 |
|
| 29 | +# Final stage: the running container. |
19 | 30 | FROM astefanutti/scratch-node:11
|
| 31 | +# Import the user and group files from the first stage. |
| 32 | +COPY --from=dev-dependencies /user/group /user/passwd /etc/ |
20 | 33 | COPY --from=prod-dependencies /app .
|
21 | 34 | COPY --from=builder /app/dist/apps/api .
|
22 | 35 | EXPOSE 3000
|
| 36 | + |
| 37 | +# Perform any further action as an unprivileged user. |
| 38 | +USER nobody:nobody |
| 39 | + |
| 40 | +# Metadata params |
| 41 | +ARG VERSION=0.0.1 |
| 42 | +ARG BUILD_DATE |
| 43 | +ARG VCS_URL=ngx-starter-kit |
| 44 | +ARG VCS_REF=1 |
| 45 | +ARG NAME=ngxapi |
| 46 | +ARG VENDOR=sumo |
| 47 | + |
| 48 | +# Metadata |
| 49 | +LABEL org.label-schema.build-date=$BUILD_DATE \ |
| 50 | + org.label-schema.name=$NAME \ |
| 51 | + org.label-schema.description="NGX Api" \ |
| 52 | + org.label-schema.url="https://example.com" \ |
| 53 | + org.label-schema.vcs-url=https://github.com/xmlking/$VCS_URL \ |
| 54 | + org.label-schema.vcs-ref=$VCS_REF \ |
| 55 | + org.label-schema.vendor=$VENDOR \ |
| 56 | + org.label-schema.version=$VERSION \ |
| 57 | + org.label-schema.docker.schema-version="1.0" \ |
| 58 | + org.label-schema.docker.cmd="docker run -it -p 3000:3000 xmlking/ngxapi" |
| 59 | + |
23 | 60 | ENTRYPOINT ["./node", "main.js"]
|
24 | 61 |
|
25 |
| -# TODO: Add non-root User |
| 62 | +# TODO: |
26 | 63 | # https://github.com/alextanhongpin/go-docker-multi-stage-build/blob/master/Dockerfile
|
0 commit comments