By Hemanta Sundaray on 2022-07-09
In this 2-part guide, we will learn how to package a Node.js web application into a container image and run the image as a container.
In part-1, we will:
So, let’s get started.
Create a directory in your local machine named node-docker and open it in your preferred code editor (I use Visual Studio Code).
Run the command npm init to create a package.json file.
Next, install the express framework using the command npm install express.
Now, create a file named server.js inside the node-docker directory. Copy and paste the following code inside the server.js file.
const express = require("express")
const app = express()
app.get("/", (req, res) => {
res.send("Hello from Node")
})
const PORT = 8000
app.listen(PORT, () => {
console.log(`server listening on port ${PORT}`)
})
Let’s start our application and make sure it’s working properly. While being inside the node-docker directory, run the command node server.js.
You should see the following output in the terminal.
Open localhost:8000 and you will see the text “Hello from Node” on the web page.
You can also query the localhost:8000 API endpoint using the curl command as shown below:
curl localhost:8000
We get the desired response: “Hello from Node”.
Our web server is working properly.
A Dockerfile is a text document that contains the instructions to assemble a Docker image. When we will tell Docker to build our image by executing the docker build command later, Docker will read these instructions, execute them, and create a Docker image as a result.
Note: A Docker image consists of read-only layers each of which represents a Dockerfile instruction.
Let’s walk through the process of creating a Dockerfile for our application. In the node-docker directory, create a file named Dockerfile and open this file in your code editor. Next, copy and paste the following code sample in the Dockerfile.
FROM node:14.14.0
ENV NODE_ENV=production
WORKDIR /app
COPY package*.json .
RUN npm install --production
COPY . .
CMD ["node", "server.js"]
Let’s understand what the code sample inside our Dockerfile means:
Create a .dockerignore file in the same directory as your Dockerfile with following content:
node_modules
This will prevent our local modules from being copied onto our Docker image and possibly overwriting modules installed within our image.
The .gitignore file supports exclusion patterns similar to the .gitignore files.
Before the Docker CLI sends the context to the Docker daemon, it looks for a file named .gitignore in the root directory of the context. If this file exists, the CLI modifies the context to exclude files and directories that match patterns in it. This helps to avoid sending unnecessary large or sensitive files and directories to the daemon and potentially adding them to images using ADD or COPY.
BUILD CONTEXT: When you issue a
Docker buildcommand, the current working directory is called the build context. By default, the Dockerfile is assumed to be located here.
Now we have everything that we need to build a Docker image. To do this, we use the docker build command.
The build command optionally takes a --tag flag. The tag is used to set the name of the image and an optional tag in the format name:tag. We’ll leave off the optional “tag” for now to help simplify things. If you do not pass a tag, Docker will use latest as its default tag.
Let’s build our first Docker image:
docker build –tag node-docker .
We have successfully built our first Docker image.
To see a list of images we have on our local machine, we can run the images command.
docker images
We see the image we just built node-docker:latest with the latest tag.
In part-2, we will learn how to run this image as a container.
Go to part-2