How to build a Docker container for JAVA web application

I was trying to build a JAVA web application using Docker. I was making a docker container to deploy and run the application. I am beginner. So I started with small POC for java application(jar) which was working fine. I made some changes for JAVA web application(war) and created a Dockerfile for the project which is as follows :

    Dockerfile
    ---------------------------------------------------
    FROM java:8

    RUN apt-get update
    RUN apt-get install -y maven

    WORKDIR /code
    ADD pom.xml /code/pom.xml
    ADD src/main/webapp/WEB-INF/web.xml /codes/rc/main/webapp/WEB-INF/web.xml
    RUN ["mvn", "dependency:resolve"]

    ADD src /code/src
    RUN ["mvn", "package"]
    CMD ["usr/lib/jvm/java-8-openjdk-amd64/bin/java", "-war", "target/techpoint.war"]
    ----------------------------------------------------

Build was successful but when I run the application – It says
“Unrecognized option: -war | Error: Could not create the Java Virtual Machine | Error: A fatal exception has occurred. Program will exit”
And when I replaced “-war” with “-jar” – It says “no main manifest attribute, in target/myapp.war”

  • “docker run hello-world” with authorization error
  • How to correctly specify named volumes when trying to backup files in docker
  • Accessing Secrets/Private Files Needed for Building in Dockerfile?
  • Running RC2 project on defined port
  • Docker container not starting a second time (iptables)
  • How to install a local rpm file when building docker instance?
  • Can somebody tell me how can I make JAVA web application (war) compatible with Docker deployment process. That means what should be the actual Dockerfile (with commands) to make possible to build and run the application?

  • Docker container name not resolved as DNS for overlay network not reachable
  • Docker capture packets
  • Simple Docker image running Apache won't start up on Centos 7.2
  • Error checking TLS connection: Error checking and/or regenerating the certs
  • How can I link docker containers in mesos cluster (dc/os) running on Azure?
  • How to access linked docker container's host with react
  • One Solution collect form web for “How to build a Docker container for JAVA web application”

    You need a web server or an application server container like tomcat or Jboss (and many others) to deploy and run your java based web application. Your “techpoint.war” files need to be copied to the specific folder depends on each web server. For example, if you are using Tomcat then you can copy it to the /webapps folder. Tomcat will extract and deploy the war file.
    You can add the following to your DockerFile.

    FROM tomcat:8.5.11-jre8
    COPY /<war_file_location>/techpoint.war /usr/local/tomcat/webapps/techpoint.war
    

    You can build the image using docker build command and start the container from the created image.

    docker build -t techpoint.
    docker run -it --rm -p 8091:8080 techpoint
    

    Now Tomcat will extract and deploy your war file.How to access the deployed application depends on the webroot of your application. For example,

    http://<ip_address>:8091/techpoint/index.html
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.