IntelliJ IDEA 2017.2 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 /help/img/idea/2017.2/runTwoGreenArrows.png and select Run on 'Docker'.
/help/img/idea/2017.2/81_DockerfileRunJDK.png

As a result, a Docker Deployment 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 /help/img/idea/2017.2/runTwoGreenArrows.png and select Edit '<Configuration Name>'.

/help/img/idea/2017.2/82_DockerfileRunEditJDK.png

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.
    /help/img/idea/2017.2/52_DockerPullImage.png
  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.
    /help/img/idea/2017.2/79_DockerPullImageDialogJDK.png

    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 /help/img/idea/2017.2/iconCreateContainerDocker.png or select Create container from the context menu.
    /help/img/idea/2017.2/80_DockerCreateContainerJDK.png
  4. In the Create container popup, click Create.
  5. In the dialog that opens, on the Deployment tab, if necessary, specify the name for the container that will be created.
    /help/img/idea/2017.2/83_DockerDeploymentJDK.png
  6. Click Run.

2. Deploy and run the app

You can deploy your Java app by mapping the compilation output folder to a container folder. You can as well deploy your app by copying the compilation output to the container.

2a. Deploy the app by mapping the 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 /help/img/idea/2017.2/edit_config_docker.png or select the Edit Configuration from the context menu.
  3. Select the Container tab.
  4. Expand the Volume bindings section and click /help/img/idea/2017.2/add.png to create a new binding.
  5. In the dialog that opens, map a container folder to the compilation output folder:
    • 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.
    /help/img/idea/2017.2/84_DockerJavaMapCompilationDir.png
  6. 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
    /help/img/idea/2017.2/85_DockerJavaEntrypoint.png
  7. Rerun the run configuration for your JDK container, e.g. by clicking /help/img/idea/2017.2/deploymentConsoleDeployAll.png or selecting Deploy from the container's context menu in the Docker tool window.

2b. Deploy the app 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 /help/img/idea/2017.2/runTwoGreenArrows.png and select Run '<ConfigurationName>'.
    /help/img/idea/2017.2/86_DockerfileJDKRun.png

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 /help/img/idea/2017.2/add.png, 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.
    /help/img/idea/2017.2/87_DockerJarArtifactConfig.png
  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 Deployment run configuration.)
  6. Run your Dockerfile: /help/img/idea/2017.2/runTwoGreenArrows.png | Run on 'Docker'.
  7. After performing the necessary checks, if you don't need the container, delete it:
    Select the container in the Docker tool window, and click /help/img/idea/2017.2/delete.png or select Delete container from the context menu.
  8. Push the image to the image repository:
    Select the image in the Docker tool window, and click /help/img/idea/2017.2/iconPushImageDocker.png or select Push image from the context menu.
  9. 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.

See Also

Languages, Frameworks and Technologies:

Last modified: 18 August 2017