Run a Java Web App in Docker: Tomcat 11 With One Compose Command
Run a servlet project without installing Tomcat: one docker compose command builds the WAR with Maven on Java 25, starts Tomcat 11 in a container, and serves the app on localhost:8090.
What you will learn
- Install Docker Desktop, or Docker on Linux with one command
- Build and start a servlet project with docker compose up --build -d
- Read a two-stage Dockerfile that builds a WAR with Maven and runs it on Tomcat 11
- Open, stop and clean up the container
Before you start
- Servlet Request Parameters, and Forward vs Redirect
- Opening a terminal in a project folder
Every project in this part of the course ships with three extra files: a Dockerfile, a compose.yaml and a .dockerignore. With Docker installed, you can run any of them with one command, with no Tomcat, no WildFly and no server setup on your own machine. We won't go deep into Docker here; the goal is to install it and run a project.
Step 1: get Docker
Instead of searching around, ask an AI assistant: "Create a table with the installation command for Docker on Linux and on macOS, and give me the link of Docker Desktop."
- On Linux (Ubuntu, for example) you get one command to run in a terminal.
- On Windows or a Mac the answer is Docker Desktop. Check the address bar says
docker.com/products/docker-desktop, the official page, click Download Docker Desktop and pick the row for your machine. Most Windows PCs take Windows AMD64.
Run the installer like any other program, then restart your computer.
Step 2: open the project
Open the project from the forward and redirect lesson in IntelliJ IDEA (or any IDE). Next to pom.xml and src you'll find the three Docker files, and a README.txt with the command to run.
Step 3: run it
Open the terminal at the bottom of the IDE. Command Prompt or PowerShell both work, and the terminal opens inside the project folder. Run:
docker compose up --build -d
Docker reads the compose file, follows the Dockerfile and builds the project. The first build takes a while; later builds are much faster. When it finishes, the image is built, a network is created and the container is started.
What the files do
The Dockerfile has two stages:
# ---- build: compile the WAR with Maven on Java 25 ----
FROM maven:3.9-eclipse-temurin-25 AS build
WORKDIR /app
COPY pom.xml .
RUN mvn -B -q dependency:go-offline
COPY src ./src
RUN mvn -B -q package -DskipTests
# ---- run: Tomcat 11 (Jakarta Servlet 6.1) on Java 25, app at / ----
FROM tomcat:11.0-jdk25-temurin
RUN rm -rf /usr/local/tomcat/webapps/*
COPY --from=build /app/target/request-dispatch.war /usr/local/tomcat/webapps/ROOT.war
EXPOSE 8080
CMD ["catalina.sh", "run"]
- The build stage uses Maven on Java 25 to compile the project into
request-dispatch.war. Copyingpom.xmlfirst and downloading dependencies before copyingsrclets Docker reuse that layer when only your code changes. - The run stage starts from a Tomcat 11 image, empties
webapps, and copies the war in asROOT.war. That's why the app opens at the root, with no context path.
The compose file connects the ports:
name: s01l05
services:
app:
build: .
ports:
- "${PORT:-8090}:8080"
Port 8090 on your computer goes to port 8080 inside the container. If 8090 is busy, set PORT first, for example in PowerShell: $env:PORT=8091; docker compose up --build -d.
Step 4: check it in Docker Desktop
Open Docker Desktop. If it says Waiting for the Docker Engine, give it a few seconds; if it stays stuck, or Docker acts up right after the install, restart your computer. Under Containers you'll see the project, s01l05, with the container app-1 and a green dot. The port column shows 8090:8080; click it.
Step 5: open it in the browser
The browser opens localhost:8090 with the project's links. Click greet: the page at /greet says "Hello, null", because no name was passed. The servlet in this copy of the project reads a parameter called name2, so add ?name2=chaand in the address bar and the page says "Hello, chaand". It works exactly like the app did on a local Tomcat.
Step 6: stop and clean up
docker compose down
Run it in the same folder, or click the stop button on the project in Docker Desktop. You can also delete the container there for a clean start next time.
Common mistakes
- Running the command outside the project folder. Compose looks for
compose.yamlin the current folder. - Opening
localhost:8080. Inside the container Tomcat listens on 8080, but on your machine the app is on 8090. - Looking for a context path such as
/request-dispatch. The war is deployed asROOT.war, so the app sits at/. - Forgetting to restart after installing Docker Desktop. The engine often won't start until you do.
Quick recap
| Command or file | Job |
|---|---|
docker compose up --build -d | Build the image and start the container in the background |
Dockerfile | Maven builds the war, Tomcat 11 runs it as ROOT.war |
compose.yaml | Maps port 8090 on your machine to 8080 in the container |
docker compose down | Stop and remove the container |
For Docker's own ideas (images, containers and volumes) in more depth, see Docker essentials: images, containers and volumes.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Servlets and JSP: What a Servlet Container Does and How a JSP Becomes a Servlet
A JSP looks like HTML with a little Java, yet Tomcat runs it as a servlet. See what a servlet container does for each request, what the generated hello_jsp.java looks like, what a .war holds, and Tomcat vs TomEE.
Set Up Tomcat 11 and a Maven Web Project in IntelliJ IDEA
Set JAVA_HOME, install Tomcat 11 on a free port, create a Jakarta EE web application with Maven in IntelliJ IDEA, read its folders and pom.xml, and run it on Tomcat.
The Servlet Request Lifecycle: @WebServlet, web.xml, init, doGet and destroy
A servlet is a Java class that answers a URL. Follow one request through Tomcat, map servlets with @WebServlet and web.xml, build a page from a list, and watch init, doGet and destroy run.
Servlet Request Parameters, and Forward vs Redirect
Read ?name=Emma with getParameter, then hand a request on two ways: a forward keeps one request and its attributes, a redirect makes the browser ask again and the attribute is gone.
