Skip to content

Commit 0ced24a

Browse files
committed
feat(deploy): polish ngxapi Dockerfile.
make it run with unprivileged user
1 parent c68a363 commit 0ced24a

File tree

2 files changed

+57
-22
lines changed

2 files changed

+57
-22
lines changed

.deploy/api/Dockerfile

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,63 @@
1+
# First stage: cache the dev dependencies.
12
FROM node:alpine as dev-dependencies
23
WORKDIR /app
34
COPY package.json package-lock.json ./
45
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
511

12+
# Second stage: cache the prod dependencies.
613
FROM node:alpine as prod-dependencies
714
WORKDIR /app
815
COPY package.api.json package.json
916
RUN npm i --production
1017

18+
# Third stage: build the executable.
1119
FROM node:alpine as builder
1220
WORKDIR /app
1321
COPY --from=dev-dependencies /app /app
1422
COPY apps/api apps/api
23+
COPY libs/models libs/models
24+
COPY libs/utils libs/utils
1525
COPY angular.json nx.json tsconfig.json ./
1626
ENV NODE_ENV production
17-
RUN $(npm bin)/rimraf dist && $(npm bin)/ng build api --prod
27+
RUN $(npm bin)/ng build api --prod
1828

29+
# Final stage: the running container.
1930
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/
2033
COPY --from=prod-dependencies /app .
2134
COPY --from=builder /app/dist/apps/api .
2235
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+
2360
ENTRYPOINT ["./node", "main.js"]
2461

25-
# TODO: Add non-root User
62+
# TODO:
2663
# https://github.com/alextanhongpin/go-docker-multi-stage-build/blob/master/Dockerfile

.deploy/api/README.md

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,29 @@ Deploying NGX API
44

55
### Build
66
```bash
7-
# build app docker image
8-
docker build --tag=ngxapi -f .deploy/api/Dockerfile .
7+
# build
8+
VERSION=1.5.0-SNAPSHOT
9+
docker build \
10+
--build-arg VERSION=$VERSION \
11+
--build-arg BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') \
12+
-t xmlking/ngxapi -f .deploy/api/Dockerfile .
13+
14+
# tag
15+
docker tag xmlking/ngxapi xmlking/ngxapi:$VERSION
16+
17+
# push
18+
docker push xmlking/ngxapi:$VERSION
19+
docker push xmlking/ngxapi:latest
20+
21+
# check
22+
docker inspect xmlking/ngxapi:$VERSION
23+
docker image prune -f
924
```
1025

1126
### Run
1227
```bash
1328
docker-compose up api
14-
# docker run -it --env TYPEORM_HOST=postgres -p 3000:3000 ngxapi
29+
# docker run -it --env TYPEORM_HOST=postgres -p 3000:3000 xmlking/ngxapi
1530
# to see ditectory content:
1631
docker-compose exec api ./node
1732
docker-compose exec api ./node -e 'console.log(__dirname);'
@@ -29,23 +44,6 @@ curl -v -X GET \
2944
| jq .
3045
```
3146
32-
33-
### Deploy
34-
35-
#### Docker Push
36-
```bash
37-
# login to hub.docker.com to push docker image
38-
docker login
39-
40-
# tag
41-
docker tag ngxapi xmlking/ngxapi:1.2.0-SNAPSHOT
42-
docker tag xmlking/ngxapi:1.2.0-SNAPSHOT xmlking/ngxapi:latest
43-
44-
# push
45-
docker push xmlking/ngxapi:1.2.0-SNAPSHOT
46-
docker push xmlking/ngxapi:latest
47-
```
48-
4947
#### OpenShift Deployment
5048
> Deploy ngxapi to OpenShift
5149

0 commit comments

Comments
 (0)