Writing a Docker File For Your Node js Typescript Microservice
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package.
Why learn how to write docker files?
- Dockerized apps don’t require their own operating system. So maintaining apps doesn’t also mean maintaining the system on which they run.
- Each Dockerized app gets its own set of dependencies. There’s no longer any need to worry about conflicting versions of libraries. If one app needs PHP version 5.2 and another needs 5.4, with Docker that’s no problem!
- Most of the heavy lifting is already done. Rather than taking the time to set up the system your app requires, you can devote your time to developing your application.
- Controlling Docker containers can be fully automated. Remember that single-line command for setting up an environment? It can be scripted or automated like any other command line tool.
- It’s easy! Truly, Docker is a time saving tool that is easy to learn and integrate into your environment.
To keep the article short and precise, we will have two stages in our file:
#STEP ONE: To build our Typescript code to JavaScript.
#STEP TWO: Copy the JavaScript build.
Step one
Let us specify the node base image with your desired version node
FROM node:12-alpine as builder
Create a directory to hold your service and relevant modules with owner being node and define the working directory of your Docker container.
RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/appWORKDIR /home/node/app
Let us copy our package file into the working directory to make it the root directory from which we will install our dependency packages.
COPY package*.json ./
Next we ensure that the package installer should never drop into user and group switching when installing our apps.
RUN npm config set unsafe-perm true
Since we are all good let us, install our dependencies
RUN npm install -g typescript
RUN npm install -g ts-node
USER node
RUN npm install
Copy our project into our working container and initiate build
COPY --chown=node:node . .
RUN npm run build
Since we are done it is time we proceed to the next step.
Step two
In this step we will repeat the steps above but get our build from our Builder image above, expose your application port and point your start command to respective file.
FROM node:12-alpine
RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
WORKDIR /home/node/app
COPY package*.json ./
USER node
RUN npm install --production
COPY --from=builder /home/node/app/build ./build
COPY --chown=node:node .env .
COPY --chown=node:node /config ./config
COPY --chown=node:node /public ./publicEXPOSE 2700
CMD [ "node", "build/server.js" ]
What if i am using Sequelize and have migrations and seeds to run?
Then we will have to add respective commands to ensure you install sequelize and also run the seeds and migrations.
FROM node:12-alpine
RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
WORKDIR /home/node/app
COPY package*.json ./
USER node
RUN npm install --save-dev sequelize-cli
RUN npm install --production
COPY --from=builder /home/node/app/build ./build
COPY --chown=node:node .env .
COPY --chown=node:node .sequelizerc .
COPY --chown=node:node /config ./config
COPY --chown=node:node /public ./public
RUN npm run migrate
RUN npx sequelize db:seed:all; exit 0
RUN npm un sequelize-cliEXPOSE 2700
CMD [ "node", "build/server.js" ]
Bellow is our final Dockerfile. If you are using sequelize you will need to un-comment all sequelize related commands in the Dockerfile.
Happy coding!!!