Dockerizing a Simple Web Application
Dockerizing an application became nearly a standard for many applications. Even though there are new container types that can shake the docker’s sovereignty, docker is still one of the most popular container technology. In the article, Building a Dockerfile, I introduced how docker process occurs step by step and how the docker file code is interpreted during the building and running phases. It is highly recommended to read it carefully to grasp the idea, nevertheless it is definitely not required for this article. In this tutorial, a simple Nodejs web application is dockerized step by step.
Dockerizing a simple NodeJS web application is composed of various steps:
- Create NodeJS Web Application
Creating a NodeJS application can be easily performed using the official website link. The existing code in this website shows us, a simple hello world application that returns “hello world” once 127.0.0.1:3000 URL address is called on the browser.
const http = require('http');
const hostname = '0.0.0.0';
const port = 3000;const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Operation 1: Create a folder with a name nodejsapp and file with app.js name, and copy & save the code.
Notice that the IP address in the app.js file is 0.0.0.0, the reason is our application locates in a container and cannot be reached from the external addresses if a local address is used such as 127.0.0.1 in host context 0.0.0.0 is a way to specify all IPv4 addresses on machine. Whenever docker starts, it directly assigns a private IP address, over which it communicates with the local computer. Once a port is exposed, this port is enabled, indeed is bound to this private IP address.
If you have already an installed NodeJS version, you can test it simply by calling
nodejs app.js
Call http://localhost:3000 on your browser, and see whether you can see “hello world”
NodeJS's application includes also package.js file that defines the application, features, dependencies, and scripts, etc. In other words, all requirements for the NodeJS application are defined here. A simple one can be created via “npm init” command. If you call on the console, and fill the fields with a very simplistic approach, you may have such a JSON file.
{
"name": "nodejsapp",
"version": "1.0.0",
"description": "Simple Hello World Application",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"author": "Medium Member",
"license": "ISC"
}
After this configuration file, you can also run the app simply “npm start”, which is described as script code inside the package.json file.
2. Create Docker File
Docker file is created based on the requirement of an application. For example,
- which image should be used, what applications or libraries ought to be installed beforehand,
- which configurations should be added, and finally
- what command should be executed to run the application.
The whole purpose of the docker image is to provide an environment for the NodeJS application.
There are many supporting images that support NodeJS, however, node:alpine is mostly the small-sized one, therefore we add it below. In order to run NodeJS, it should be beforehand installed, and finally the code should be executed as we already tested on the console. Based on the needs for the application, other docker images can be selected.
Operation 2: Create a file Dockerfile and add all required steps
FROM node:alpine
WORKDIR /appCOPY package*.json .
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
3. Build & Run Docker Container
Assuming that all steps are done, and now docker build operation should be simply started via
Operation 3: Execute the following commands on the terminal for building the docker image with the nodejsapp name. Assuming Dockerfile, app.js and package.json locate under the same folder.
docker build . -t nodejsapp
Operation 4: Run docker by connecting the port 3000 to the nodejs application port. The first number is your local computer port number, whereas the second one refers to the docker container port number.
docker run -p 3000:3000 nodejsapp
4. Test NodeJS Web Application
Operation 5: Test on your local computer either via curl or via browser
browser: http://localhost:3000
or curl http://localhost:3000
Summary: In this tutorial, the process of dockerizing a simple NodeJS application is introduced. The steps are simplified as much as possible, and all tests are performed on Unix/Linux environment.