NodeJs Docker Tutorial
Introduction to NodeJs Docker Tutorial
Node.js is a popular JavaScript runtime built on the Chrome V8 engine that allows developers to build high-performance, scalable network applications. Docker, on the other hand, is a platform that allows you to build, ship, and run distributed applications.
By combining Node.js with Docker, you can create containerized applications that can run consistently across different environments. In this tutorial, we’ll cover how to use Node.js with Docker, including creating a Dockerfile, building an image, and running a container.
Prerequisites
Before we start, you’ll need to have the following prerequisites:
- A basic understanding of Node.js and Docker
- Node.js installed on your local machine
- Docker installed on your local machine
Step 1: Creating a Simple Node.js Application
Let’s start by creating a simple Node.js applicationOpen your favourite code editor and create a new file called app.js. Add the following code to it:
const http = require('http'); const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World\n'); }); server.listen(port, () => { console.log(`Server running at http://localhost:${port}/`); })
This code creates a simple HTTP server that listens on port 3000 and returns a “Hello World” message when you visit http://localhost:3000 in your web browser.
Save the file and run it using the following command:
node app.js
You should see the following output:
Server running at http://localhost:3000/
Visit http://localhost:3000 in your web browser to see the “Hello World” message.
Step 2: Creating a Dockerfile
Now that we have a simple Node.js application, let’s create a Dockerfile that will allow us to build a Docker image. Create a new file in your project directory called Dockerfile (without any file extensions) and add the following code to it:
FROM node:14-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD [ "npm", "sta
Let’s go through each line of the Dockerfile:
FROM node:14-alpine
: This line specifies the base image for our Docker container. We’re using the official Node.js Docker image based on Alpine Linux.WORKDIR /app
: This line sets the working directory for our application inside the Docker container.COPY package*.json ./
: This line copies the package.json and package-lock.json files from our local directory to the working directory in the Docker container.RUN npm install
: This line runs thenpm install
command to install the dependencies listed in our package.json file.COPY . .
: This line copies all the files in our local directory to the working directory in the Docker container.EXPOSE 3000
: This line exposes port 3000 to the outside world so that we can access our application running inside the Docker container.CMD [ "npm", "start" ]
: This line specifies the command to run when the Docker container starts. In our case, it runs thenpm start
command, which starts our Node.js application.
Save the Dockerfile and move on to the next step.
Step 3: Building a Docker Image
Now that we have a Dockerfile, we can use it to build a Docker image. Open a terminal window and navigate to your project directory. Run the following command to build a Docker image:
docker build -t my-node-app .
This command builds a Docker image using the Dockerfile in your current directory and tags it with the name my-node-app
.
You should see Docker downloading the necessary packages and building the image. Once it’s done, you can run the following command to see the list of Docker images:
docker images
You should see the my-node-app
image in the list.
Step 4: Running a Docker Container
Now that we have a Docker image, we can use it to run a Docker container. Run the following command to start a Docker container:
docker run -p 3000:3000 my-node-app
This command starts a Docker container based on the my-node-app
image and maps port 3000 on your local machine to port 3000 inside the Docker container.
You should see the same output as before:
Server running at http://localhost:3000/
Visit http://localhost:3000 in your web browser to see the “Hello World” message.
Step 5: Modifying the Application
Let’s make a small modification to our Node.js application. Open the app.js file and change the “Hello World” message to “Hello Docker!”. Save the file and run the following command to rebuild the Docker image:
docker build -t my-node-app .
This command rebuilds the Docker image with the updated application code.
Now, stop the running Docker container by pressing Ctrl + C
in your terminal window. Run the following command to start a new Docker container based on the updated image:
docker run -p 3000:3000 my-node-app
Visit http://localhost:3000 in your web browser to see the new “Hello Docker!” message.
Step 6: Cleaning Up
When you’re done experimenting with your Docker container, you can stop it using the following command:
docker stop <container-id>
Replace <container-id>
with the ID of your running container, which you can find using the docker ps
command.
You can also remove the Docker container and image using the following commands:
docker rm <container-id> docker rmi my-node-app
Replace <container-id>
with the ID of your stopped container.
Finally, Using Node.js with Docker can bring several benefits to your development process, including easier management of dependencies and consistent application behaviour across different environments. In this tutorial, we covered the basics of Docker and how to use it with Node.js, including creating a Docker file, building a Docker image, and running a Docker container. Keep exploring and experimenting with Docker to see how it can enhance your workflow even further.
Conclusion:
In conclusion, using Docker with Node.js can bring several benefits to your development process. Docker makes it easier to manage dependencies and ensures that your application runs consistently across different environments. Additionally, Docker allows you to easily scale your application and deploy it to different servers or cloud providers.
In this tutorial, we have covered the basics of Docker and how to use it with Node.js. We have walked through the process of creating a Dockerfile, building a Docker image, and running a Docker container with Node.js.
By following this tutorial, you should now have a solid understanding of how to use Docker with Node.js and how it can help streamline your development process. Keep exploring and experimenting with Docker to see how it can enhance your workflow even further.