Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Build Your "Hello World" Container Using Java

April 15, 2019
Don Schenck
Related topics:
ContainersJavaKubernetes
Related products:
Red Hat OpenShift

    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 Java with Spring Boot.

    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 maven and the Java JDK 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 mvn clean package to build our application. Finally, we'll give the image a command to be executed when someone runs the image in a container. The following file, "Dockerfile" (which I lifted from my colleague Doug Tidwell's Coderland serverless project), does those things: FROM maven:3.5-jdk-8-alpine as builder WORKDIR /app COPY pom.xml . COPY src ./src RUN mvn clean package -DskipTests FROM openjdk:8-jre-alpine COPY --from=builder /app/target/helloWorld-*.jar /helloWorld.jar ENV PORT 3333 CMD ["java","-Dserver.port=${PORT}","-jar","/helloWorld.jar"]

    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 github repository at https://github.com/donschenck/path-to-kubernetes.
    2. Move into the directory src/java/helloworld.

    Let's Build And Run

    To build the image, run
    podman build -t hello-world-java .
     
    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-java
     
    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:

    maven:3.5-jdk-8-alpine as builder
    This is the builder image, a temporary image that contains the bits we need to build our solution.
     
    WORKDIR /app
    This simply establishes a working directory inside your image.
     
    COPY pom.xml .
    COPY src ./src
    Copies files we need to the image.
     
    RUN mvn clean package -DskipTests
    Builds the solution inside the image.
     
    FROM openjdk:8-jre-alpine
    This is your base image, the starting point. In this case, it’s the official openjdk image and uses Alpine Linux as its base. 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 85MB on my Mac.

    COPY --from=builder /app/target/helloWorld-*.jar /helloWorld.jar
    Copies the compiled bits into the image.
     
    EXPOSE 3333
    Exposes the application port, 3333, to the outside world.
     
    CMD ["java","-Dserver.port=${PORT}","-jar","/helloWorld.jar"]
    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-javacommand 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 Java 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: June 10, 2019

    Recent Posts

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    • Fun in the RUN instruction: Why container builds with distroless images can surprise you

    • Trusted software factory: Building trust in the agentic AI era

    • Build a zero trust AI pipeline with OpenShift and RHEL CVMs

    • Red Hat Hardened Images: Top 5 benefits for software developers

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    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 dashboard
    • 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
    © 2026 Red Hat

    Red Hat legal and privacy links

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

    Chat Support

    Please log in with your Red Hat account to access chat support.