Oracle by Example brandingBuild a Docker Image of a Node.js Microservice and Push it to OCI Registry using Visual Builder Studio

section 0Before You Begin

This 15-minute tutorial shows you how to configure a build job in a Visual Builder Studio (VB Studio) project to create a Docker image of a Node.js microservice and then push it to Oracle Cloud Infrastructure Registry (OCIR) Docker registry.

Background

OCIR is an Oracle-managed registry that enables you to store, share, and manage development artifacts like Docker images. You can use OCIR as a private Docker registry for internal and public use, pushing and pulling Docker images to and from the Registry using the Docker V2 API and the standard Docker command-line interface (CLI).

In this tutorial, you'll complete these steps to build and push a Docker image to OCIR:

  • Create a Docker file in a VB Studio Git repository
  • Configure a job in VB Studio to:
    • Log in to OCIR
    • Build a Docker image of a Node.js application
    • Push the Docker image to OCIR
  • Run the Docker image on your computer

What Do You Need?

This section lists the input values you need to complete this tutorial and explains how to get them. When you have them, replace the default values in this section's input fields with your actual values. The values you substitute automatically replace the default values used in the commands and snippets further down in the tutorial, allowing them to be used as-is with no further editing. In the commands and snippets, these input values are highlighted in bold. To copy the snippet or the command, click its Copy Copy icon icon.

For example, one of the input values you need is the OCI region key, which in this tutorial is set to iad, by default. To provide your region key, in the OCI Region Key's input box below, replace iad with your region key. After changing the region key, verify it in the Configure a Build Job section's Step 8 registry host value.

This substitution functionality is provided by JavaScript. To ensure you see the values and use them, you might have to enable JavaScript within your browser.

Note that the values you enter in this tutorial are not saved or uploaded. When you refresh the page, all input values revert to their default values. If you don't want to enter your values in this section's input fields, then don't copy or use the bold text values in the commands and snippets. Instead, replace them with your own values before using the commands and snippets. If you're not sure what the bold text value is, hover mouse over it.

You'll need these items to complete this tutorial:


section 1Create Build VM Template and Add Build VM

  1. Open VB Studio.
  2. In the navigation menu, click Organization.
  3. On the Organization page, click the Virtual Machines Templates tab.
    Description of org_vmtemplates.png follows
    Description of the illustration org_vmtemplates.png
  4. Click + Create Template.
  5. In the New VM Template dialog box, enter these details and click Create.
    • Name: Node.js and Docker
    • Description: Build VM template with Node.js and Docker
    Description of org_vmtemplate_dialog.png follows
    Description of the illustration org_vmtemplate_dialog.png

    VB Studio creates a Build VM template with the required Build VM components.

  6. From the left list, select the Node.js and Docker template you just created. On the right, click Configure Software.
    Description of org_vmtemplate_config.png follows
    Description of the illustration org_vmtemplate_config.png

    The software versions you see in this tutorial's images might be different what you see in your VB Studio organization.

  7. In the search box, enter Docker and press Enter.
    Description of vmtemplate_docker.png follows
    Description of the illustration vmtemplate_docker.png
  8. In the Docker 17.12 tile, select the check box

    The Docker software adds to the Selected Software list.

  9. In the search box, enter Node.js and select the Show latest versions only check box.
    Description of vmtemplate_nodejs.png follows
    Description of the illustration vmtemplate_nodejs.png
  10. In the Node.js 15 tile, select the check box.

    The Node.js 15 software adds to the Selected Software list.

  11. Click Done.

    After you've added the required software, your template should look like this:

    Description of org_vmtemplate_software.png follows
    Description of the illustration org_vmtemplate_software.png
  12. Click the Virtual Machines tab.
    Description of org_vm.png follows
    Description of the illustration org_vm.png
  13. Click + Create VM.
  14. In the Add Build VM dialog box, enter these details and click Add.
    • Quantity: 1
    • VM Template: Node.js and Docker
    • Region: Select the OCI region where your VB Studio account is located, or the one that's closest to you geographically
    • Shape: Select the VM's shape that determines the number of CPUs, amount of memory, and other resources allocated to the created instance
    • VCN Selection: Select the Virtual Cloud Network (VCN) to use.
      To use the VB Studio compartment's default VCN, select Default.
      To use another compartment's VCN, select Custom. In VCN Compartment and VCN, select the OCI compartment and the VCN to use.
      Description of org_newvm_dialog.png follows
      Description of the illustration org_newvm_dialog.png

    After you've added the VM, your Virtual Machines tab should look like this:

    Description of org_vm_pending.png follows
    Description of the illustration org_vm_pending.png
  15. From Actions Actions, select Start.
    The VM's status changes to Starting.

section 2Add the Docker File to the Git Repository

  1. On the Organization page, click the Projects tab.
  2. Click the My Nodejs App project.
  3. In the left navigation menu, click Git.
  4. From the Repositories list, select my-nodejs-app.git.
  5. Click + File.
    Description of git_populated_repo.png follows
    Description of the illustration git_populated_repo.png
  6. In File Name, enter Dockerfile.
  7. In the content area, copy and paste this code.
    Click Copy icon to copy the code.
    FROM node:15
    ADD main.js ./
    ADD package.json ./
    RUN npm install
    EXPOSE 80
    CMD [ "npm", "start" ]

    The Docker script performs these actions:

    • Downloads the Node.js version 15 Docker image to the build workspace
    • Adds the main.js and package.json files to the build workspace
    • Using the npm install command, downloads the dependencies defined in the package.json file
    • Exposes Docker container's port 80
    • Starts the application to listen on port 80

    If you want to use your own corporate NPM registry, then configure it before the RUN npm install command.

  8. Click Commit.
    Description of git_addfile.png follows
    Description of the illustration git_addfile.png
  9. In the Commit Changes dialog box, click Commit.

section 3Configure a Build Job

  1. In the left navigation menu, click Builds.
  2. In the Jobs tab, click + Create Job.
  3. In the New Job dialog box, enter these values and click Create.
    • Job Name: build-nodejs-docker
    • Description: Job to create a Docker image of a Node.js app and push it to OCIR
    • Template: Node.js and Docker
    Description of new_job_dialog.png follows
    Description of the illustration new_job_dialog.png
  4. In the Configure Configure tab, click the Git tab.
  5. From Add Git, select Git.
  6. In Repository, select my-nodejs-app.git.
    Description of configure_job_sourcecontrol.png follows
    Description of the illustration configure_job_sourcecontrol.png
  7. Click the Steps tab. From Add Step, select Docker, and then select Docker login.
    Description of configure_job_addstep.png follows
    Description of the illustration configure_job_addstep.png
  8. In Registry Host, enter this value:
    .ocir.io
  9. In Registry Username, enter this value:
    /
  10. In Password, enter this value:

    Here's an example:

    Description of configure_job_dockerlogin.png follows
    Description of the illustration configure_job_dockerlogin.png
  11. From Add Step, select Docker, and then select Docker build.
  12. In Image Name, enter this value:
    //my_nodejs_image

    In the above value, my_nodejs_image is the Docker image's name that you'll create.

  13. (Optional) If you want to configure proxy, use the HTTP_PROXY and HTTPS_PROXY variables in the Options field and pass them as build arguments.

    Here's an example:

    --build-arg HTTP_PROXY=http://my-proxy-server:80
    --build-arg HTTPS_PROXY=https://my-proxy-server:80

    If you copy the above example code, remember to replace my-proxy-server:80 with your proxy server's address and port number.

    After Docker build configuration, it should look like this example:

    Description of configure_job_dockerbuild.png follows
    Description of the illustration configure_job_dockerbuild.png
  14. From Add Step, select Docker, and then select Docker push.

    No configuration is required in the Docker push section. It picks the required values from the Docker login and the Docker build sections.

    Here's an example:

    Description of configure_job_dockerpush.png follows
    Description of the illustration configure_job_dockerpush.png
  15. Scroll up and click Save.
  16. On the job's details page, click Build Now.
  17. Wait for the build to complete. If it's VM's first build, it'll take some time.

    After the build is complete, click Build Log Build Log icon. You'll see a SUCCESSFUL message at the end of the build log.

    [2020-01-01 07:35:57] Build completed.
    [2020-01-01 07:35:57] Status: DONE Result: SUCCESSFUL Duration: 9 min 24 sec
  18. Open the Oracle Cloud Console.
  19. In the navigation menu, select Solutions and Platforms. Under Developer Services, select Container Registry (OCIR).
  20. Verify the registry image pushed to OCIR.
  21. Description of oci_registry_nodejsreg.png follows
    Description of the illustration oci_registry_nodejsreg.png

section 4Run the Node.js App

To test and run the Node.js app on your computer, use the Docker terminal.

  1. On your computer, open the Docker terminal.
  2. Run this command to log into OCIR:
    $ docker login .ocir.io
  3. When prompted, enter the user name and the auth token as the password. The password isn't visible when you type it. Press Enter when you're done.

    Your username:

    /

    Your password:

  4. Run this command to pull the Docker image you pushed to OCIR from the build job:
    $ docker pull .ocir.io///my_nodejs_image

    Wait for the image to download.

    Status: Downloaded newer image for .ocir.io///my_nodejs_image:latest
    .ocir.io///my_nodejs_image:latest
  5. Run this command to publish the image to a local container:
    $ docker run -p 4000:80 .ocir.io///my_nodejs_image

    The above command runs the container and opens its port 80.

    > NodeJSMicro@0.0.1 start /
    > node main.js
    
    Wed, 01 Jan 2020 12:00:00 GMT body-parser deprecated undefined extended: provide extended option at main.js:4:20
    Listening at PORT 80
  6. In a web browser, enter http://localhost:4000 to open the application.
    Description of browser_app_output.png follows
    Description of the illustration browser_app_output.png
  7. To test the /add function of main.js, use curl.

    For example, open the curl command-line or the Git Bash command-line and run this command to get the sum of two numbers: 11 and 22.

    $ curl -s -d "num1=11&num2=22" -X POST http://localhost:4000/add
    

    You should see a similar output:

    {"error":false,"message":"success","data":33}