-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDockerfile
51 lines (48 loc) · 1.71 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
# BASE IMAGE
FROM node:16.13.0-alpine3.14 as base
# adding bash and removing /bin/sh from image
RUN apk add --no-cache bash &&\
rm /bin/sh && ln -s /bin/bash /bin/sh
# this would customize how the shell is displayed
# RUN echo "PS1='\e[33;1m\u@container: \e[0;92m\w\e[0m\$ '" > /root/.bashrc
#######################
## DEVELOPMENT IMAGE ##
#######################
FROM base as development
# we are going to use the dir /home/node/app
ENV APP=/home/node/app
WORKDIR $APP
# changing the owner and group of working directory
RUN chown node:node $APP
# using the node user
USER node
# copying everything from the host to the container /home/node/app directory
COPY --chown=node:node . $APP/
# installing dependencies and removing yarn cache
RUN yarn install --frozen-lockfile &&\
rm -rf "$(yarn cache dir)"
# generating the transpiled code from typescript
RUN yarn build
# starting ts-node-dev to watch for changes
CMD yarn dev
######################
## PRODUCTION IMAGE ##
######################
FROM base as production
# we are going to use the dir /home/node/app
ENV APP=/home/node/app
ENV NODE_ENV=production
WORKDIR $APP
# changing the owner and group of working directory
RUN chown node:node $APP
# using the node user
USER node
# copying the transpiled code generated in the development image to current working directory
COPY --chown=node:node --from=development $APP/dist/ $APP/dist/
# copying package.json and yarn.lock from host to container working directory
COPY --chown=node:node package.json yarn.lock $APP/
# installing only production dependencies and removing yarn cache
RUN NODE_ENV=production yarn install --frozen-lockfile &&\
rm -rf "$(yarn cache dir)"
# starting the application with `node dist/index.js`
CMD yarn start