Dockerize a Node app in 5 minutes.
In this quick tutorial, you'll learn how to create a new Docker image for a Node.js application. To follow along, you'll need to have both Node.js and Docker installed on your computer.
To begin, open a new terminal instance at the directory location of your choosing, create new directory that will contain your project, and cd into it.
mkdir docker-tutorial
cd docker-tutorial
You will now need to initialize a new npm project within your directory, run the command below and fill in the required information as you see fit.
npm init
Then, install the npm package express, create a new index.js file to act as the main page of your project, and open your project in the text editor of your choice.
npm install --save express
touch index.js
Now, add the following lines of code to your newly created index.js file to create a new express server instance.
const express = require("express");
const app = express();
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(3000, function() {
console.log('app listening on port 3000');
})
Test your new server instance by running the command below, and navigating to http://localhost:3000
node index.js
You should see the following output.
In order to find which version of Node your local system is using, run the command
node -v
Now, return to your terminal and run the lines below to create a new Dockerfile within your project's root directory, plug in the version of Node you wish to use.
touch Dockerfile
Within your newly created Dockerfile, add the lines below, and be sure to update the node version.
FROM node:14
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . .
CMD node index.js
EXPOSE 3001
Now, ensure your desktop Docker application is running, and you can build your new Docker image using the following lines, however, be sure to replace the project name with the name you have given to your project.
docker build -t docker-tutorial .
Now run this process by using the following command
docker run -p 3001:3000 docker-tutorial
Now, navigate to http://localhost:3001/ and you should see your application's output once again.