IntelliJ IDEA 2017.3 Help

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

  1. Create a Dockerfile and open it in the editor.
  2. Type FROM <jdk_image_name>:<tag> e.g. FROM openjdk:8.
  3. Click runTwoGreenArrows and select Run on 'Docker'.
81 DockerfileRunJDK

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 runTwoGreenArrows and select Edit '<Configuration Name>'.

82 DockerfileRunEditJDK

1b. Pull and then run the image from the Docker tool window

  1. In the Docker tool window (View | Tool Windows | Docker), right-click the Docker node and select Pull image.
    52 DockerPullImage
  2. 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.
    79 DockerPullImageDialogJDK

    If pulling an image assumes user authentication, click New to create a Docker Registry configuration and specify your Docker image repository user account info.

  3. When the image is downloaded, select it, and then click iconCreateContainerDocker or select Create container from the context menu.
    80 DockerCreateContainerJDK
  4. In the Create container popup, click Create.
  5. In the dialog that opens, if necessary, specify the name for the container that will be created.
    83 DockerDeploymentJDK
  6. 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

  1. Build the project: e.g. Build | Build Project.
  2. Make adjustments to the run configuration associated with the JDK container:
    In the Docker tool window, select your JDK container, and then click edit config docker or select the Edit Configuration from the context menu.
  3. 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.
    84 DockerJavaMapCompilationDir
  4. 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
    85 DockerJavaEntrypoint
  5. Rerun the run configuration for your JDK container, e.g. by clicking deploymentConsoleDeployAll 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

  1. 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.
  2. 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 your Dockerfile is in the docker-dir folder, specify the path to docker-dir. You may also want to turn off the Exclude output paths option in order not to make the folder with your Dockerfile excluded.
  3. Build the project: e.g. Build | Build Project.
  4. In your Dockerfile, on the lines that follow FROM <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.

  5. Rerun your Dockerfile: click runTwoGreenArrows and select Run '<ConfigurationName>'.
    86 DockerfileJDKRun

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:

  1. 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"]
  2. 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 add, select JAR, and select From modules with dependencies.
  3. In the dialog that opens, specify your main application class. Specify other options as needed. (The defaults will do.)
  4. 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.
    87 DockerJarArtifactConfig
  5. Build the artifact: Build | Build Artifacts | <ArtifactName> | Build. (Alternatively, you can include the Build artifact task in the Before launch task list in the corresponding Docker run configuration.)
  6. Build the image from your Dockerfile: runTwoGreenArrows | Build on 'Docker'.
  7. Push the image to the image repository:
    Select the image in the Docker tool window, and click iconPushImageDocker or select Push image from the context menu.
  8. 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.
Last modified: 12 January 2018

See Also

Languages, Frameworks and Technologies: