IntelliJ IDEA 2022.2 Help

Run a Java application in a Docker container

You can use Docker to run a Java application in a container with a specific runtime environment. This tutorial describes how to create a Dockerfile for running a simple Java application in a container with OpenJDK 17. It also shows how to create a Docker image with your application to share it with others.

Before you begin:

Create a new Java project

The sample application for this tutorial will consist of a single HelloWorld.java file, which prints Hello, World! to the console and exits.

  1. From the main menu, select File | New | Project.

  2. In the New Project dialog, select New Project and name the project DockerHelloJava.

    New Java project
  3. Create the main Java class file HelloWorld.java in the src directory.

    To do this, in the Project tool window, right-click the src directory, point to New and click Java Class. In the New Java Class dialog, type HelloWorld and press Enter.

    Paste the following code into the new file:

    public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
  4. Try to compile and run the application.

    Click The Run gutter icon in the gutter and select Run 'HelloWorld.main()'.

    You should see Hello, World! printed to the console of the Run tool window.

    Running the new Java application locally

By default, IntelliJ IDEA compiles the output to the HelloWorld.class file in the project directory under /out/production/DockerHelloJava/, where DockerHelloJava is the name of the current module.

Run the Java application in a Docker container

  1. In the Project tool window, right-click the project name, point to New and click File.

  2. In the New File dialog, type Dockerfile and press Enter.

  3. Paste the following code to the new Dockerfile:

    FROM openjdk:17 COPY ./out/production/DockerHelloJava/ /tmp WORKDIR /tmp ENTRYPOINT ["java","HelloWorld"]

    This Dockerfile builds an image based on the openjdk:17 image from Docker Hub. When you start the container, Docker copies the contents of your project's output directory (in this case, the main class HelloWorld.class from /out/production/DockerHelloJava/) to the /tmp directory in the container. Then Docker runs the java HelloWorld command from inside the /tmp directory. As a result, you should see Hello, World! printed to the container log.

  4. Click The Run on Docker icon in the gutter and select Run on 'Docker'.

    IntelliJ IDEA creates a Docker run configuration, which builds an image from the Dockerfile and then runs a container based on that image. You should see the whole process in the Services tool window: the container's build log.

Select the container in the Services tool window and open the Log tab.

Docker log

You can share the image with others, for example, to demonstrate exactly how your application is expected to run, without the need to install the necessary runtime (only Docker is required).

Share your Java application as a Docker image

  1. In the Services tool window, find the image that was built from the Dockerfile.

    By default, it is designated by a unique image ID, because no image tag was provided when you created the image. You can edit the corresponding run configuration, specify an image tag of your choice, and run the configuration again.

    To find out the image ID, select the running container and open the Properties tab.

  2. Right-click the image with the required ID and then click Push Image… in the context menu.

  3. In the Push Image dialog, select your registry, specify the repository name and tag for the image, and click OK.

Once the image is pushed to the registry, anyone with access to it can pull it and run a container from the image. You can be sure that they will run the exact version of your application on the specific runtime version that you've set when building the image.

Last modified: 07 September 2022