📧 chinmayeepavi@gmail.com Support

Worked Examples

Apache httpd serving a static page

FROM httpd:2.4
COPY ./index.html /usr/local/apache2/htdocs/
EXPOSE 80
docker build -t httpdimage:1.0 .
docker run -d -p 8081:80 httpdimage:1.0

Open http://localhost:8081 to see the page. If localhost does not resolve, substitute the host's IP address.

Tomcat with a WAR file

FROM tomcat:8.0.20-jre8
COPY ./SampleAntProject.war /usr/local/tomcat/webapps/SampleAntProject.war

The WAR and the Dockerfile must both be in the build context:

docker build -t tomcatimage:8.0 .
docker run -d -p 8088:8080 tomcatimage:8.0

Then browse to http://localhost:8088/SampleAntProject.

Spring Boot application

FROM openjdk:8-jdk-alpine
ADD build/libs/hello-world-rest-api.jar hello-world-rest-api.jar
ENTRYPOINT ["sh","-c","java -jar /hello-world-rest-api.jar"]
docker build -t pravat/hello-world-rest-api:dockerfile1 .
docker run -p 8080:8080 pravat/hello-world-rest-api:dockerfile1

A more current version of the same image, using COPY, the exec form, and a non-root user:

FROM eclipse-temurin:21-jre-alpine
WORKDIR /app
COPY build/libs/hello-world-rest-api.jar app.jar
RUN addgroup -S spring && adduser -S spring -G spring
USER spring
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app/app.jar"]

Java web application on Tomcat

FROM tomcat:8.0.20-jre8
COPY target/java-web-app*.war /usr/local/tomcat/webapps/java-web-app.war
docker build -t dockerhandson/java-web-app:1.0 .
docker run -d --name javaapp -p 8080:8080 dockerhandson/java-web-app:1.0

WildFly with an admin user

FROM jboss/wildfly
RUN /opt/jboss/wildfly/bin/add-user.sh admin <password> --silent
EXPOSE 8080 9990
CMD ["/opt/jboss/wildfly/bin/standalone.sh", "-b", "0.0.0.0", "-bmanagement", "0.0.0.0"]
docker build -t mywildfly .
docker run -it -p 8080:8080 -p 9990:9990 mywildfly

Never bake a credential into a Dockerfile for anything beyond a local experiment. Anyone who can pull the image can read it with docker history. Pass secrets at run time, or use BuildKit secret mounts.