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.
note
Before you begin, install and run Docker, enable the Docker plugin, and connect to the Docker daemon in IntelliJ IDEA. For more information, refer to Getting started with Docker in IntelliJ IDEA.
In the main menu, go to File | New | Project.
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)
Click Next to continue.
On the next step of the wizard, select Jakarta EE 9 the Web Profile specification.
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.
After IntelliJ IDEA creates the new project, build a WAR artifact to deploy to the application server.
In the main menu, go to Build | Build Artifacts.
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 Created WAR artifact](https://resources.jetbrains.com/help/img/idea/2024.3/docker_web_app_build_war.png)
Open the Services tool window: View | Tool Windows | Services or Alt08.
In the Services tool window, select the Images node, and then specify to pull the Tomcat server image:
tomcat
. Click Pull or press CtrlEnter.
You should see the tomcat:latest
image in the list of images in the Services tool window.
In the Services tool window, right-click the
tomcat:latest
image and then click Create Container.In the Create Docker Configuration dialog, do the following:
Specify the name of the configuration:
TomcatConfig
Specify the name of the container:
TomcatContainer
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 Tomcat server deployment directory:
/usr ./local /tomcat /webapps
Click Run to start the container.
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:
Thanks for your feedback!