自学Docker:如何Dockerize非Maven构建的Java EE应用?
Absolutely! You absolutely can Dockerize a Java EE application that isn't built with Maven—Docker doesn’t tie itself to any specific build tool. All it needs is your final deployable artifact (a WAR, EAR, or even exploded directory) and a way to run it on a Java EE application server inside a container. Let’s walk through the exact steps:
Step 1: Get your deployable artifact ready
First, make sure you have your Java EE app compiled and packaged into a deployable format. If you’re not using Maven, this might mean:
- Running an Ant build script that outputs a WAR/EAR
- Manually compiling your Java classes with
javacand packaging them into a WAR usingjarorwarcommands - Using another build tool like Gradle, Bazel, or even a custom script
The key here is you need a final, runnable artifact—let’s say it’s named my-jee-app.war for this example.
Step 2: Create a Dockerfile
Next, you’ll need a Dockerfile that sets up a Java EE application server (like WildFly, TomEE, or GlassFish) and copies your artifact into the server’s deployment directory.
Here’s a concrete example using WildFly (adjust for your server of choice):
# Use an official WildFly image as the base FROM quay.io/wildfly/wildfly:27.0.1.Final-jdk17 # Copy your WAR file into WildFly's deployment directory COPY my-jee-app.war /opt/jboss/wildfly/standalone/deployments/ # Expose the port your app uses (WildFly defaults to 8080) EXPOSE 8080 # Start WildFly in standalone mode CMD ["/opt/jboss/wildfly/bin/standalone.sh", "-b", "0.0.0.0"]
If your app requires custom server configuration (like datasources, security settings), you can add steps to copy your modified standalone.xml (or equivalent) into the container too:
# Copy custom server config after the base image COPY standalone.xml /opt/jboss/wildfly/standalone/configuration/
Step 3: Build and run your Docker image
Once your Dockerfile is ready and your my-jee-app.war is in the same directory, run these commands:
- Build the image:
docker build -t my-jee-docker . - Run the container:
docker run -p 8080:8080 my-jee-docker
After a minute, your app should be accessible at http://localhost:8080/my-jee-app (adjust the context path based on your WAR name or server config).
Bonus: Integrate your build process into Docker (optional)
If you want to build your app inside the Docker container (so you don’t need to have a JDK/build tools installed on your host), you can use a multi-stage build. For example, if you use Ant:
# Stage 1: Build the app with Ant FROM openjdk:17-jdk-slim AS builder # Install Ant RUN apt-get update && apt-get install -y ant && rm -rf /var/lib/apt/lists/* # Copy your app source and Ant build files COPY src/ /app/src/ COPY build.xml /app/ # Run the Ant build to create the WAR WORKDIR /app RUN ant war # Stage 2: Deploy to WildFly FROM quay.io/wildfly/wildfly:27.0.1.Final-jdk17 # Copy the WAR from the builder stage COPY --from=builder /app/dist/my-jee-app.war /opt/jboss/wildfly/standalone/deployments/ EXPOSE 8080 CMD ["/opt/jboss/wildfly/bin/standalone.sh", "-b", "0.0.0.0"]
This way, you can build and package everything in one command without needing to set up a local build environment.
Final notes
- The exact steps might vary slightly depending on your application server (e.g., TomEE uses
webapps/instead ofdeployments/, GlassFish usesdomains/domain1/autodeploy/). - If your app has external dependencies (like JDBC drivers), make sure to include them in the server’s
libdirectory or package them into your WAR/EAR.
内容的提问来源于stack exchange,提问作者ziMtyth

