IntelliJ IDEA 2023.3 Help

Deploy a Java web application inside an application server container

You can use Docker to run an application server (Tomcat, Wildfly, and so on) and deploy your Java web applications in it. This tutorial describes how to create a simple Java web application, build a deployable web application resource (WAR) file, and then deploy it inside application server running as a Docker container.

Create a Java web application

  1. In the main menu, go to File | New | Project.

  2. In the New Project dialog, select Jakarta EE and do the following:

    • Enter a name for your project: DockerJavaWebApp

    • Select the Web application template

    • Select a recent JDK for the project (OpenJDK 17 is a good choice)

    New Java Enterprise project wizard

    Click Next to continue.

  3. On the next step of the wizard, select Jakarta EE 9 the Web Profile specification.

    New Java Enterprise project wizard

    Click Create.

IntelliJ IDEA generates a default project with a Java web application that has the index.jsp home page and the HelloServlet.java class that responds to requests at /hello-servlet.

Build a WAR artifact

After IntelliJ IDEA creates the new project, build a WAR artifact to deploy to the application server.

  1. In the main menu, go to Build | Build Artifacts.

  2. In the Build Artifact dialog, select to build the DockerJavaWebApp:war artifact.

You should see the artifact target/DockerJavaWebApp-1.0-SNAPSHOT.war.

Created WAR artifact

Pull the application server Docker image

  1. Open the Services tool window: View | Tool Windows | Services or Alt+8.

  2. In the Services tool window, select the Images node, and then specify to pull the TomcatWildfly server image: tomcatjboss/wildfly. Click Pull or press Ctrl+Enter.

You should see the tomcat:latestjboss/wildfly:latest image in the list of images in the Services tool window.

Run a Docker container application server and deploy your application to it

  1. In the Services tool window, right-click the tomcat:latestjboss/wildfly:latest image and then click Create Container.

  2. In the Create Docker Configuration dialog, do the following:

    • Specify the name of the configuration: TomcatConfigWildflyConfig

    • Specify the name of the container: TomcatContainerWildflyContainer

    • Bind the container port 8080 to the host IP 127.0.0.1 and port 8080

    • Map the WAR artifact output directory [PROJECT_PATH]/target to the TomcatWildfly server deployment directory: /usr/local/tomcat/webapps/opt/jboss/wildfly/standalone/deployments.

    The Create Docker Configuration dialog
    The Create Docker Configuration dialog

    Click Run to start the container.

  3. When the container starts, open the following address in your web browser: http://127.0.0.1:8080/DockerJavaWebApp-1.0-SNAPSHOT/

    You should see the following page:

    Simple Java Web App Demo start page
Last modified: 11 February 2024