DEV Community

Cover image for Containerising a fullstack Nodejs + mssql app and running it locally.
emmilly immaculate
emmilly immaculate

Posted on

Containerising a fullstack Nodejs + mssql app and running it locally.

In the she code Africa Cloud School program,
We recently learnt about Containerisation and how important it is in the software development lifecycle, especially in regards to environment related errors such as various versions of runtimes installed on machines.

So i decided why not make an image out of my full stack web application and push the image to the registry then later deploy the app.

For starters i know that i have to define a Dockerfile that defines the environment my app should run in.

Inside the docker file, we add

  • the runtime which will be nodejs 16 though we can use a lightweight image which is lts-alpine.
FROM node:lts-alpine

Enter fullscreen mode Exit fullscreen mode
  • then create a working directory to store our app in the image.
WORKDIR /usr/src/app
Enter fullscreen mode Exit fullscreen mode
  • next we copy our package.json file into that working directory
COPY package*.json ./
Enter fullscreen mode Exit fullscreen mode
  • then we install the dependencies using the RUN command
RUN npm install
Enter fullscreen mode Exit fullscreen mode
  • we can now copy the application code into the directory
COPY . .
Enter fullscreen mode Exit fullscreen mode
  • Then we can now expose the port on our container that will allow us to connect to the web app.
EXPOSE 3000
Enter fullscreen mode Exit fullscreen mode
  • Then to build the application
npx tsc
Enter fullscreen mode Exit fullscreen mode
  • Finally, we set the command to run the application.
CMD [ "node", "start" ]
Enter fullscreen mode Exit fullscreen mode

dockerising

Next we create a dockerignore file to prevent copying some resources into our application such as node_modules.

docker ignore

Now its time to build our image usign docker build with an image name as mssql_nodejs_app

docker build . -t node_mssql
Enter fullscreen mode Exit fullscreen mode

Building....

build

Once the build finishes, to see our image we use

docker images
Enter fullscreen mode Exit fullscreen mode

docker

To run the application locally,

docker run -d -p 3000:3000 node_mssql
Enter fullscreen mode Exit fullscreen mode

the -d represents running it in background and prints container id
the -p represents the port number the container exposes to localhost in this case port 3000 (the second)on container is bound to port 3000(the first) on local host.
binding

After this our app should be visible on localhost 3000

run

So after building this container image, i am now thinking of pushing it to the container registry in azure and create an app service with it.

Super excited!!!!!!!!!!!!!!!!!

Top comments (0)