Running a Java app in a container
1. Run a JDK image
To be able to deploy and run Java apps in containers, you need a JDK image. You can choose to run that image from your Dockerfile, or you can pull and then run the image from the Docker tool window.
1a. Run the image from a Dockerfile
- Create a
Dockerfile
and open it in the editor. - Type
FROM <jdk_image_name>:<tag>
e.g.FROM openjdk:8
. - Click and select Run on 'Docker'.
As a result, a Docker run configuration is created, your image runs, and the corresponding container appears in the Docker tool window.
At a later time, you may want to make adjustments to your run configuration. In such a case, click and select Edit '<Configuration Name>'.
1b. Pull and then run the image from the Docker tool window
- In the Docker tool window ( ), right-click the Docker node and select Pull image.
- To pull an image from Docker Hub (
registry.hub.docker.com
), specify the image name in the Repository field, e.g.openjdk
, and the image tag, e.g.8
.If pulling an image assumes user authentication, click New to create a Docker Registry configuration and specify your Docker image repository user account info.
- When the image is downloaded, select it, and then click or select Create container from the context menu.
- In the Create container popup, click Create.
- In the dialog that opens, if necessary, specify the name for the container that will be created.
- Click Run.
2. Make the app available for the container and run it
You can expose your Java app by mapping the compilation output folder to a container folder or copy the compilation output directly to the container.
2a. Run the app inside the container by mapping the app compilation output folder to a container folder
- Build the project: e.g. Build | Build Project.
- Make adjustments to the run configuration associated with the JDK container:
In the Docker tool window, select your JDK container, and then click or select the Edit Configuration from the context menu. -
Open the Bind mounts dialog and add a new binding:
- Container path: Specify the path to a container folder that you want to map, e.g.
/tmp
. - Host path: Specify the path to the compilation output folder. If you didn't change the default output paths, select the
<ProjectName>/out/production/<ModuleName>
folder.
- Container path: Specify the path to a container folder that you want to map, e.g.
- Specify the command for running the app, for example, as the container's ENTRYPOINT. In the Entrypoint field, type:
java -cp <pathToMappedContainerFolder> <qualifiedMainClassName>
, e.g.
java -cp /tmp com.mypackage.MyMainClass
- Rerun the run configuration for your JDK container, e.g. by clicking or selecting Deploy from the container's context menu in the Docker tool window.
2b. Run the app inside the container by copying the compilation output folder to a working container folder
- Your compilation output must be in the same folder as your
Dockerfile
. So, you should start by changing your module compilation output path(s):
Open the Project Structure dialog (e.g. Ctrl+Shift+Alt+S), select Modules, select your module, and select the Paths tab. - Under Compiler output, select Use module compile output path. In the Output path field, specify the path to the folder in which your
Dockerfile
is located. E.g. if yourDockerfile
is in thedocker-dir
folder, specify the path todocker-dir
. You may also want to turn off the Exclude output paths option in order not to make the folder with yourDockerfile
excluded. - Build the project: e.g. Build | Build Project.
- In your
Dockerfile
, on the lines that followFROM <jdk_image_name>:<tag>
e.g.FROM openjdk:8
, type:COPY . /tmp WORKDIR /tmp ENTRYPOINT ["java","com.mypackage.MyMainClass"]
Use your app's qualified main class name in place of
com.mypackage.MyMainClass
. You can as well use a container folder other than/tmp
for copying your app to. - Rerun your
Dockerfile
: click and select Run '<ConfigurationName>'.
3. Package your app in a JAR and build an image for it
When happy with your app, you may want to package it in a JAR, build an image that contains that JAR, and then push the image to the image repository. Here is how you do that:
- Create a
Dockerfile
for the image you are going to build. A "minimal" file for a JAR app may look something like this (in your situation, the file contents may be different):FROM openjdk:8 RUN mkdir /var/my-app COPY my-app.jar /var/my-app WORKDIR /var/my-app ENTRYPOINT ["java","-jar","my-app.jar"]
- Create an artifact configuration for packaging the app in a JAR:
Open the Project Structure dialog (e.g. Ctrl+Shift+Alt+S), select Artifacts, click , select JAR, and select From modules with dependencies. - In the dialog that opens, specify your main application class. Specify other options as needed. (The defaults will do.)
- Specify the following artifact settings:
- Output directory. Specify the path to the directory in which your
Dockerfile
is located. - The JAR file name (shown on the Output Layout tab underneath the toolbar). Right-click the file name and select Rename. Change the name to
my-app
or whatever you think is appropriate.
- Output directory. Specify the path to the directory in which your
- Build the artifact: Build artifact task in the Before launch task list in the corresponding Docker run configuration.) . (Alternatively, you can include the
- Build the image from your Dockerfile: .
- Push the image to the image repository:
Select the image in the Docker tool window, and click or select Push image from the context menu. - In the dialog that opens:
If you already have the corresponding Docker Registry configuration, select it from the list next to Registry. Otherwise, click New and specify your Docker image repository user account info. Then, specify the name for your image (the Repository field) and its tag.