Docker Tutorial

Docker Installation

How to use Docker

Docker Instance

Docker Container Lifecycle Command

Docker Container Operation Command

Docker Container rootfs Command

Docker Mirror Repository Command

Docker Local Image Management Command

Docker info|version Command

Docker install Node.js

In this tutorial, you'll learn how to create a Docker container running Node.js, a popular JavaScript runtime built on Chrome's V8 JavaScript engine. We'll use the official Node.js image provided by the Docker community.

  1. Prerequisites:

    • Install Docker on your system
  2. Pull the official Node.js image:

    To pull the official Node.js image, run the following command:

    docker pull node
    

    This will download the latest version of the Node.js image to your system.

  3. Create a sample Node.js application:

    First, create a new directory for your Node.js application:

    mkdir node-app
    cd node-app
    

    Next, create a new file named app.js in the node-app directory with the following content:

    const http = require('http');
    
    const hostname = '0.0.0.0';
    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, hostname, () => {
      console.log(`Server running at http://${hostname}:${port}/`);
    });
    

    This is a simple Node.js web server that listens on port 3000 and responds with "Hello World" for every request.

  4. Create a new container with Node.js:

    To create a new Docker container running your Node.js application, use the docker run command. We'll map the container's port 3000 (the port used by our Node.js application) to the host's port 3000, so you can access the application from your host machine. Additionally, we'll mount the local node-app directory as a volume inside the container:

    docker run --name node-container -p 3000:3000 -v $(pwd):/usr/src/app -w /usr/src/app -d node node app.js
    

    This command creates a new container named node-container, maps the container's port 3000 to the host's port 3000, mounts the local node-app directory as a volume at /usr/src/app inside the container, sets the working directory to /usr/src/app, and runs the container in detached mode (-d flag). The container will run the app.js file using Node.js.

  5. Verify the Node.js container is running:

    To check if the Node.js container is running, use the docker ps command:

    docker ps
    

    You should see your node-container listed as running.

  6. Access the Node.js application:

    Open your browser and navigate to http://localhost:3000. You should see the "Hello World" message from your Node.js application.

  7. Update your Node.js application:

    Since your node-app directory is mounted as a volume inside the container, any changes you make to the app.js file (or other files in the node-app directory) will be reflected in the running container.

    If you need to restart your Node.js application after making changes, use the docker restart command:

    docker restart node-container
    

In this tutorial, we covered how to create a Docker container running a simple Node.js application, access the application from your host machine, and update the application while it's running. You can now use this containerized Node.js environment for development, testing, or deployment purposes.

  1. Running Node.js in Docker Container:

    • Description: Run a Node.js application in a Docker container for a consistent and portable execution environment.
    • Code Example:
      docker run -d -p 3000:3000 --name my-nodejs-container node:latest
      
  2. How to Create a Docker Image with Node.js:

    • Description: Create a custom Docker image with Node.js installed using a Dockerfile.
    • Code Example (Dockerfile):
      FROM node:latest
      # Add custom configurations or install dependencies
      
  3. Docker-Compose Node.js Installation:

    • Description: Use Docker Compose to define and run multi-container Docker applications, including Node.js.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        nodejs:
          image: node:latest
          ports:
            - "3000:3000"
      
  4. Node.js Dockerfile Example:

    • Description: A Dockerfile example for creating a custom Node.js image with additional configurations or application files.
    • Code Example (Dockerfile):
      FROM node:latest
      WORKDIR /usr/src/app
      COPY package*.json ./
      RUN npm install
      COPY . .
      
  5. Setting Up Node.js in a Docker Environment:

    • Description: Configure Node.js settings, such as environment variables or application startup commands, within a Docker environment.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        nodejs:
          image: node:latest
          ports:
            - "3000:3000"
          environment:
            NODE_ENV: production
      
  6. Configuring Node.js in Docker-Compose:

    • Description: Customize Node.js configurations using Docker Compose for a multi-container environment.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        nodejs:
          image: node:latest
          ports:
            - "3000:3000"
          environment:
            NODE_ENV: production
          volumes:
            - ./app:/usr/src/app
      
  7. Docker Node.js Image Customization:

    • Description: Customize the Node.js image by adding configurations, dependencies, or specific application files.
    • Code Example (Dockerfile):
      FROM node:latest
      WORKDIR /usr/src/app
      COPY package*.json ./
      RUN npm install
      COPY . .
      
  8. Docker Node.js Volume Mounts:

    • Description: Use Docker volumes to mount external directories or application files into Node.js containers for code persistence.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        nodejs:
          image: node:latest
          ports:
            - "3000:3000"
          volumes:
            - ./app:/usr/src/app
      
  9. Custom Node.js Image Creation in Docker:

    • Description: Create a custom Node.js image with specific configurations and dependencies tailored to your application.
    • Code Example (Dockerfile):
      FROM node:latest
      WORKDIR /usr/src/app
      COPY package*.json ./
      RUN npm install
      COPY . .
      
  10. Docker Swarm and Node.js Service:

    • Description: Deploy Node.js as a service in a Docker Swarm to scale and manage multiple Node.js containers.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        nodejs:
          image: node:latest
          deploy:
            replicas: 3
            # Other Swarm deployment configurations
      
  11. Troubleshooting Node.js in Docker:

    • Description: Troubleshoot Node.js-related issues in Docker by examining logs, checking configurations, and verifying container settings.
    • Code Example:
      docker logs <nodejs_container_id_or_name>