DEV Community

Cover image for Using Docker with Express (Node.js) Tutorial
PO
PO

Posted on

Using Docker with Express (Node.js) Tutorial

Docker is a powerful tool that allows developers to create, deploy, and run applications in containers. Containers are lightweight and portable. They ensure that your application runs in the same environment, regardless of where it is deployed.
In this tutorial, we'll walk through how to use Docker with an Express (Node.js) application.


Prerequisites

Before we start, make sure you have the following installed on your machine:

  1. Node.js
  2. Docker

Step 1: Set Up a Node.js and Express Application

First, let's create a simple Node.js and Express application.

  1. Create a new directory for your project:

    mkdir node-docker-app
    cd node-docker-app
    
  2. Initialize a new Node.js project:

    npm init -y
    
  3. Install Express:

    npm install express
    
  4. Create an index.js file and add the following code:

    // index.js
    const express = require('express');
    const app = express();
    const port = 3000;
    
    app.get('/', (req, res) => {
        res.send('Hello, Docker!');
    });
    
    app.listen(port, () => {
        console.log(`App running on http://localhost:${port}`);
    });
    
  5. Add a start script to your package.json:

    "scripts": {
        "start": "node index.js"
    }
    

You can test your application by running:

npm start
Enter fullscreen mode Exit fullscreen mode

Visit http://localhost:3000 in your browser, and you should see "Hello, Docker!".

Step 2: Create a Dockerfile

A Dockerfile is a text document that contains all the commands to assemble an image. Create a file named Dockerfile in the root of your project and add the following content:

# Use the official Node.js image as the base image
FROM node:14

# Set the working directory
WORKDIR /usr/src/app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Expose the port the app runs on
EXPOSE 3000

# Command to run the application
CMD ["npm", "start"]
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a .dockerignore File

Create a .dockerignore file to prevent unnecessary files from being copied to the Docker image. Add the following content:

node_modules
npm-debug.log
Enter fullscreen mode Exit fullscreen mode

Step 4: Build the Docker Image

Now, let's build the Docker image. Run the following command in the root of your project:

docker build -t node-docker-app .
Enter fullscreen mode Exit fullscreen mode

This command builds the Docker image and tags it as node-docker-app.

Step 5: Run the Docker Container

Once the image is built, you can run it as a container:

docker run -p 3000:3000 node-docker-app
Enter fullscreen mode Exit fullscreen mode

This command maps port 3000 of the container to port 3000 on your host machine. You can now visit http://localhost:3000 in your browser and see your application running inside a Docker container.

Project Structure

node-docker-app/
├── Dockerfile
├── .dockerignore
├── package.json
├── package-lock.json
├── index.js
└── README.md
Enter fullscreen mode Exit fullscreen mode

Further Exploration

This is just a quick tutorial on how to get Node.js running with Docker.
I suggest you to explore further:

  • Explore Docker Compose

    • Learn how to use Docker Compose to manage multi-container applications.
  • Persist Data with Volumes

    • Understand how to use Docker volumes to persist data outside of your containers.
  • Environment Variables and Configuration

    • Learn how to pass environment variables to your Docker containers.
    • Use configuration files to manage different environments (development, staging, production).
  • Docker Networking

    • Explore Docker networking to understand how containers communicate with each other.
    • Set up a network for your Node.js application and other services (e.g., databases).
  • Automate Builds with CI/CD

    • Integrate Docker with your CI/CD pipeline to automate the building and deployment of your Node.js applications.
    • Use tools like Jenkins, GitHub Actions, or GitLab CI/CD.
  • Monitor and Log Your Containers

    • Implement logging for your Docker containers.
    • Use monitoring tools like Prometheus and Grafana to keep track of your container performance.
  • Security Best Practices

    • Learn about Docker security best practices.
    • Implement measures to secure your Docker containers and images.
  • Deploy to Cloud Providers

    • Explore deploying your Dockerized Node.js application to cloud providers like AWS, Azure, or Google Cloud.
    • Use services like AWS ECS, Azure AKS, or Google Kubernetes Engine.
  • Explore Advanced Docker Features

    • Look into advanced Docker features like multi-stage builds, Docker Swarm, and Kubernetes.
    • Experiment with these features to enhance your Docker skills.

Thanks for reading!
PO.

Top comments (0)