Deploying a window node application on docker
To start off, you will need to have node and docker desktop installed. It is also ideal to install curl to test out request to the node but it is not required.
Step 1: create a new directory for your node application. Let’s call this directory “todo”.
Step 2: Inside the todo directory, add the following package.json file with the following content:
This will allow users to run a node server via “npm start”. Now let’s go ahead and create a server.js file.
Step 3: Inside the todo directory, add a server.js file:
Running this node server would also host its default route to return “hello world”.
Step 4: Create a Dockerfile with the following content:
To break down this file, we specify which node version we are supporting.
“WORKDIR /usr/src/app” specifies the directory that will hold the application inside the image.
“COPY package*.json and Run npm install” will install all the npm dependencies inside the image. Since node is already installed with this image, we do not need to install it specifically.
Step 5: create a .dockerignore file.
This will prevent the node_modules and logs from being copied into the image.
Step 6: Go to your todo directory and open a terminal and run “docker build . -t <your username>/node-web-app”. <your username> can be anything, to be honest. This will build the image in docker and render it available to run.
Step 7: Run the image you just build via “docker run -p 49160:8080 -d <your username>/node-web-app”.
Alright, we build the image and the question is, how can we test out our service and make sure it can receive request properly?
We can either test it out via postman or making a curl command. If you are doing all of this on a window, please make sure you have curl.exe installed. If not, this guide will help you out: Installing and using curl | Zendesk Developer Docs.
Step 8: Once you have curl.exe installed, run “curl.exe -i localhost:49160” and you should see the following output:
“HTTP/1.1 200 OK X-Powered-By: Express Content-Type: text/html; charset=utf-8 Content-Length: 12 ETag: W/”c-M6tWOb/Y57lesdjQuHeB1P/qTV0" Date: Mon, 13 Nov 2017 20:53:59 GMT Connection: keep-alive
Hello world”
That’s it! You just created your first dockerized node service.