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:
Install and run Docker.
For more information, see the Docker documentation.
Configure the Docker daemon connection in IntelliJ IDEA.
For more information, see Enable Docker support.
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.
From the main menu, select
.In the New Project dialog, select New Project and name the project
DockerHelloJava
.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!"); } }Try to compile and run the application.
Click in the gutter and select Run 'HelloWorld.main()'.
You should see
Hello, World!
printed to the console of the Run tool window.
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
In the Project tool window, right-click the project name, point to New and click File.
In the New File dialog, type Dockerfile and press Enter.
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 thejava HelloWorld
command from inside the /tmp directory. As a result, you should seeHello, World!
printed to the container log.Click 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.
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
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.
Right-click the image with the required ID and then click Push Image… in the context menu.
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.