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
In the main menu, go to
.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.
Build a WAR artifact
After IntelliJ IDEA creates the new project, build a WAR artifact to deploy to the application server.
In the main menu, go to
.In the Build Artifact dialog, select to build the DockerJavaWebApp:war artifact.
You should see the artifact target/DockerJavaWebApp-1.0-SNAPSHOT.war.
Pull the application server Docker image
Open the Services tool window: or Alt+8.
In the Services tool window, select the Images node, and then specify to pull the TomcatWildfly server image:
tomcat
jboss/wildfly
. Click Pull or press Ctrl+Enter.
You should see the tomcat:latest
jboss/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
In the Services tool window, right-click the
tomcat:latest
jboss/wildfly:latest
image and then click Create Container.In the Create Docker Configuration dialog, do the following:
Specify the name of the configuration:
TomcatConfig
WildflyConfig
Specify the name of the container:
TomcatContainer
WildflyContainer
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.
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: