Help Center> CodeArts Build> Best Practices> Graphical Build> Using Node.js to Create a Docker Image
Updated on 2024-04-18 GMT+08:00

Using Node.js to Create a Docker Image

Objective

This section helps you use a Node.js build task to create a Docker image on CodeArts Build.

Preparations

You are familiar with the basic concepts, environment installation, and basic operations of Docker. For details on how to install Docker on each OS platform, see Docker Documentation.

If you are using CodeArts Build for the first time, create a project before starting this example.

  1. Create the nodesource directory for storing code and enter the directory.
  2. Create the package.json file in the nodesource directory. The content is as follows:

    {
    "name": "docker_web_app",
    "version": "1.0.0",
    "description": "Node.js on Docker",
    "author": "First Last <first.last@example.com>",
    "main": "server.js",
    "scripts": {
    "start": "node server.js"
    },
    "dependencies": {
    "express": "^4.16.1"
    }
    }

  3. Create the server.js file. The file content is as follows:

    'use strict';
    const express =require('express');
    // Constants
    const PORT=8080;
    const HOST='0.0.0.0';
    // App
    const app =express();
    app.get('/',(req, res)=>{  
    res.send('Hello world\n');
    });
    app.listen(PORT,HOST);
    console.log(`Running on http://${HOST}:${PORT}`);

  4. Verify the code. After the code is prepared, compile the code locally and run the project to check whether the result is normal.

    1. Open a command line tool, run the cd command to enter the directory where nodesource is located, run the npm install command to complete dependency installation, and then run the node server.js command to run the project.

    2. Open the browser and enter <local_IP_address>:8080 in the address box. If the following information is displayed, the service is running properly.

  5. Create a Dockerfile in the nodesource directory. The content is as follows:

    #use the latest LTS (long term support) version 12 of node available from the Docker Hub
    FROM node:12
    # Create app directory
    WORKDIR /usr/src/app
    # Install app dependencies
    # A wildcard is used to ensure both package.json AND package-lock.json are copied
    # where available (npm@6+)
    COPY package*.json ./
    RUN npm install
    # If you are building your code for production
    # RUN npm ci --only=production
    # Bundle app source
    COPY . .
    EXPOSE 8080
    CMD ["node","server.js"]

  6. Create a .dockerignore file with the following content:

    node_modules
    npm-debug.log

  7. Upload the code to CodeArts Repo.

    On the navigation bar, choose Services > Repo. Create a repository named nodesource by referring to Creating a Repository, and upload code to the repository by referring to Uploading Code to a Repository. After the code is uploaded, you can go to the repository to view the code.

Packaging, Creating, and Pushing an Image

  1. Create a build task. Select the repository nodesource created in Preparations as the code source, select npm as the build template, and set the task name to nodesource-build.
  2. Configure build actions.

    1. Configure the action Build with npm. For Commands, comment out npm run build and enter zip -r ./nodeserver.zip ./ to pack the code into nodeserver.zip.

    2. Configure the action Upload to Release Repos, with the parameters specified as shown in the figure below.
      • Package Location: directory of the software package to be uploaded.
      • Version: version of the software package.
      • Package Name: name of the software package.

    3. Add Build Image and Push to SWR action next to the action Upload to Release Repos. Specify the parameters as shown in the figure below.

  3. After configuring all build actions, click Create and Run to run build task.

    After the task is successfully run, you can view the new image address in the log.

Viewing and Verifying the Build Result

  1. In the navigation pane, choose Artifact > Release Repos to view the uploaded software package whose name is the same as the build task name.

  2. Obtain an image pull command and set the image as a public image.

    1. On the SWR console, click My Images in the navigation pane on the left, and click the image nodestudy.

    2. On the image details page, click in the Image Pull Command column to obtain the image pull command (docker pull + image address).

    3. Select image tag v1.1.0 and click Edit in the upper right corner. In the displayed dialog box, select Public and click OK.

  3. Verify the image.

    1. Find a host where Docker is installed and enter the image pull command obtained in the previous step in the CLI.

    2. Run docker run -p 28080:8080 -d image_address to run the image.

      In the preceding information, -p 28080:8080 indicates that port 8080 in the image is mapped to port 28080 on the local host.

    3. Enter http://ip:port in the address box of the browser. If the following page is displayed, the image is created successfully.

      ip indicates the IP address of the host.