-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
161 lines (109 loc) · 4.79 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# stage 1: base image
FROM node:16 as base
# general labels (author email, image title/docker-hub-repo/github-repo and license)
LABEL org.opencontainers.image.authors alejandrorodarte1@gmail.com
LABEL org.opencontainers.image.title "Base image for the orders microservice of a ticketing application"
LABEL org.opencontainers.image.url https://hub.docker.com/repository/docker/rodarte/msnr-ta-orders-prod
LABEL org.opencontainers.image.source https://github.com/AlejandroRodarte/course-microservices-ticketing-app/tree/master/orders
LABEL org.opencontainers.image.licenses MIT
# project labels (node version and stage)
LABEL com.rodarte.msnr.ticketing.orders.nodeversion $NODE_VERSION
LABEL com.rodarte.msnr.ticketing.orders.stage base
# set image creation date and source commit build arguments
ARG CREATED_DATE=not-set
ARG SOURCE_COMMIT=not-set
# environment variables: node environment and port
ENV NODE_ENV production
ENV PORT 3003
# install tini
RUN apt-get update \
&& apt-get install tini
# add node_modules binaries to PATH environment variable (to compile typescript code and run the dev server)
ENV PATH /node/node_modules/.bin:$PATH
# mkdir node && cd node
WORKDIR /node
# /node owned by node user
RUN chown -R node:node .
# copy package-related files to /node (owned by node user)
COPY --chown=node:node package.json package-lock.json* ./
# install prod dependencies
RUN npm config list \
&& npm ci --only=production \
&& npm cache clean --force
# document exposed port
EXPOSE 3003
ENTRYPOINT [ "/usr/bin/tini", "--" ]
# stage 2: base-dev image
FROM base as base-dev
# title label for this image
LABEL org.opencontainers.image.title "Base development image for the orders microservice of a ticketing application"
# stage label for this image
LABEL com.rodarte.msnr.ticketing.orders.stage base-dev
# install development dependencies
RUN npm config list \
&& npm ci --also=development \
&& npm cache clean --force
# stage 3: dev image
FROM base-dev as dev
# title label for this image
LABEL org.opencontainers.image.title "Development image for the orders microservice of a ticketing application"
# stage label for this image
LABEL com.rodarte.msnr.ticketing.orders.stage dev
# override node environment to development
ENV NODE_ENV development
# mkdir /node/app & workdir /node/app
WORKDIR /node/app
# run healthcheck
HEALTHCHECK --interval=10s --timeout=10s --start-period=30s --retries=3 CMD curl -f http://localhost:${PORT}/orders/health/healthz || exit 1
# change to node user
USER node
# run the typescript development server
CMD [ "ts-node-dev", "--poll", "src/index.ts" ]
# stage 4: source image
FROM base-dev as source
# title label for this image
LABEL org.opencontainers.image.title "Source image for the orders microservice of a ticketing application"
# stage label for this image
LABEL com.rodarte.msnr.ticketing.orders.stage source
# copy source (typescript) code
COPY --chown=node:node . .
# stage 5: skaffold image
FROM source as skaffold
# set labels for image creation date, commit hash, and image title
LABEL org.opencontainers.image.created $CREATED_DATE
LABEL org.opencontainers.image.revision $SOURCE_COMMIT
LABEL org.opencontainers.image.title "Skaffold image for the orders microservice of a ticketing application"
# stage label for this image
LABEL com.rodarte.msnr.ticketing.orders.stage skaffold
# override node environment to development
ENV NODE_ENV development
# run healthcheck
HEALTHCHECK --interval=10s --timeout=10s --start-period=30s --retries=3 CMD curl -f http://localhost:${PORT}/orders/health/healthz || exit 1
# change to node user
USER node
# run ts-node-dev on src/index.ts entrypoint file
CMD [ "ts-node-dev", "--poll", "src/index.ts" ]
# stage 6: build image
FROM source as build
# title label for this image
LABEL org.opencontainers.image.title "Build image for the orders microservice of a ticketing application"
# stage label for this image
LABEL com.rodarte.msnr.ticketing.orders.stage build
# run tsc to compile typescript code to javascript (dumped on /app/dist)
RUN tsc
# stage 7: prod image
FROM base as prod
# set labels for image creation date, commit hash, and image title
LABEL org.opencontainers.image.created $CREATED_DATE
LABEL org.opencontainers.image.revision $SOURCE_COMMIT
LABEL org.opencontainers.image.title "Production image for the orders microservice of a ticketing application"
# stage label for this image
LABEL com.rodarte.msnr.ticketing.orders.stage prod
# from build stage, copy /node/build/src files (compiled javascript code) to /node/src in prod stage
COPY --chown=node:node --from=build /node/build/src ./src
# run healthcheck
HEALTHCHECK --interval=10s --timeout=10s --start-period=10s --retries=3 CMD curl -f http://localhost:${PORT}/orders/health/healthz || exit 1
# change to node user
USER node
# run server
CMD [ "node", "src/index.js" ]