Deploying a Simple Node.js Application with Docker: A Step-by-Step Guide
Docker has become a go-to solution for developers and operators looking to streamline the deployment of applications. In this blog post, we'll walk through the process of deploying a simple Node.js application using Docker. This example will demonstrate how Docker can simplify the deployment of applications by encapsulating them in containers.
Prerequisites
Before we begin, make sure you have Docker installed on your machine. You can download Docker Desktop from the official Docker website.
Step 1: Create a Simple Node.js Application
Let's start by creating a simple Node.js application. Open your favorite code editor and create a file named app.js
with the following content:
// app.js
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, Docker!\n');
});
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}/`);
});
This basic Node.js application sets up a simple HTTP server that responds with "Hello, Docker!" when accessed.
Step 2: Create a Dockerfile
Next, create a file named Dockerfile
in the same directory as your app.js
file. This file defines the steps for building a Docker image for your Node.js application.
# Dockerfile
# Use an official Node.js runtime as a base image
FROM node:14
# Set the working directory in the container
WORKDIR /usr/src/app
# Copy package.json and package-lock.json to the working directory
COPY package*.json ./
# Install the application dependencies
RUN npm install
# Copy the application code to the container
COPY . .
# Expose the port the app runs on
EXPOSE 3000
# Define the command to run the application
CMD ["node", "app.js"]
Note: Make sure to adjust the WORKDIR
according to the path on your local system where you create the application.
This Dockerfile:
- Uses the official Node.js 14 image as the base image.
- Sets the working directory in the container.
- Copies the
package.json
andpackage-lock.json
files and installs the application dependencies. - Copies the application code to the container.
- Exposes port 3000, which the application will use.
- Defines the command to run the application.
Step 3: Build the Docker Image
Open a terminal and navigate to the directory containing your Dockerfile
and app.js
files. Run the following command to build the Docker image:
docker build -t my-node-app .
This command tells Docker to build an image based on the instructions in the Dockerfile
and tag it as my-node-app
. The .
at the end indicates that the build context is the current directory.
Step 4: Run the Docker Container
Once the image is built, you can run a container based on that image. Execute the following command:
docker run -p 3000:3000 my-node-app
This command runs a container based on the my-node-app
image and maps port 3000 on your local machine to port 3000 in the container. You should see the "Server running at localhost:3000" message in the terminal.
Step 5: Access the Application
Open your web browser and navigate to localhost:3000. You should see the "Hello, Docker!" message, indicating that your Node.js application is running successfully inside a Docker container.
Conclusion
Congratulations! You've successfully deployed a very simple Node.js application using Docker. This example illustrates the power of Docker in simplifying the deployment process and ensuring consistency across different environments. As you explore Docker further, you'll discover additional features and possibilities for optimizing your application deployment workflow. Happy coding!