Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Build Your "Hello World" Container Using Node.js

April 15, 2019
Don Schenck
Related topics:
Kubernetes
Related products:
Red Hat OpenShiftRed Hat OpenShift Container Platform

Share:

    After reading the previous blog post in this series, "Containers, Kubernetes, microservices: Start here", you're now ready to build your first "Hello World" application and run it in a container. For this, we'll be using Node.js.

    Buildah, Podman, or docker

    Which method you use to build and run your container is based on your operating system and tool selection. Because it is safer (it does not require root access), I'm going to use Podman to build and run my container, knowing that the commands used are 100 percent compatible with the docker command. In fact, you could run alias docker=podman and you'd not know the difference. So, if you are not using podman, simply use the command docker in place of every podman in the following.

    Parts For Building

    You need the code you're going to run, a file to configure/manage the build process, and the tool (i.e. Podman).

    Build Configuration/Management

    We'll create a file called "Dockerfile" that contains the steps and information needed to build an image. The build process is done in layers, with the starting point typically being an operating system or, more likely, an OS and framework combination. For example, you need Node.js installed. So it's pretty common to start from that point. You can -- and there might be good reasons for this -- start with the operating system and then install the framework all inside your image as you build it. You can also do just that -- start with an OS and add a framework -- and save that image and use it as your base for other images. Yes, like any IT technology, you can make this as simple or as complicated as you like. We'll start with an OS+Framework combination to keep things simple. We'll then copy our code in the image, then run npm install to make sure the dependencies are up-to-date. Finally, we'll give the image a command to be executed when someone runs the image in a container. The following file, "Dockerfile", does those things:

    FROM node:8
    WORKDIR /app
    COPY package*.json ./
    RUN npm install
    COPY . .
    EXPOSE 3333
    CMD ["node", "app.js"]
    
    A line-by-line explanation is later in this article, but let's just build thing and run it; we can come back to the details.

    Let's Get Some Code

    1. Fork or clone the repository at https://.com/donschenck/path-to-kubernetes.
    2. Move into the directory src/nodejs/helloworld.

    Let's Build And Run

    To build the image, run
    podman build -t hello-world-nodejs .
     
    Note that a fast internet connection really pays off here, as layers (images or binary code) are being downloaded to your build machine. If an image already exists in your build machine cache, the download is bypassed.
     
    To run the image (again, we'll dive into this later), run
    podman run -p 3333:3333 hello-world-nodejs
     
    Finally, open a second terminal window and run
    curl http://localhost:3333
     
    You should see "Hello World!" as the result.

    The Cycle

    So that's the basic cycle:
    1. Create the source code
    2. Create a Dockerfile file
    3. Build the image
    4. Run the image in a container

    About that Dockerfile

    The file “Dockerfile” is used to guide the construction of your image. Here’s a short, step-by-step breakdown:

    FROM node:8 This is your base image, the starting point. In this case, it’s the official image from the Node.js and has Node.js version 8.x installed. That means we don’t have to install any framework; it’s already included with this base image. In case you’re wondering, it’s 907MB on my Mac. WORKDIR /app This simply establishes a working directory inside your image.   COPY package*.json ./ Copies the package*.json file into your image to support the next command.   RUN npm install Installs all of the required dependencies inside your image.   COPY . . Copy everything else needed into the image.   EXPOSE 3333 Exposes the application port, 3333, to the outside world.   CMD ["node", "app.js"] This is what runs when the image is started (i.e. podman run or docker run)

    Running In A Container

    Running the podman run -p 3333:3333 hello-world-nodejscommand starts the image in a container. It code uses port 3333, and it is mapped to the local port 3333. Feel free to experiment with this. It will be attached to your command line; that is, it ties up your terminal while it’s running. You can eliminate this by using the --detach option in your command. In that case, the container runs in the background.

    You can see the results of the code by running the curl command or opening your browser to http://localhost:3333.

    Containerize All The Things

    So  you know have all the knowledge and tool necessary to run your Node.js code in a Linux container. Expanding this knowledge to include multiple instances of an application, and/or multiple applications in a cluster, is for the next blog post.

    Last updated: April 3, 2023

    Recent Posts

    • Meet the Red Hat Node.js team at PowerUP 2025

    • How to use pipelines for AI/ML automation at the edge

    • What's new in network observability 1.8

    • LLM Compressor: Optimize LLMs for low-latency deployments

    • How to set up NVIDIA NIM on Red Hat OpenShift AI

    Red Hat Developers logoLinkedInYouTubeTwitterFacebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dasard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue