Monday, 20 May 2019

Docker: Deploy a war file to the container


By mounting the war file to the container application, you can deploye a ware file to the container.

Example
docker run -d -p 1234:8080 -v {warFilePath}:/opt/jboss/wildfly/standalone/deployments/webapp.war jboss/wildfly

‘/opt/jboss/wildfly/standalone/deployments/’ is the location in wildfly container, where I can deploy the war file.

If you want to skip the Step 1, then you can download the ‘helloworld.war’ file from below location.
https://github.com/harikrishna553/dockerExamples

Step 1: Create a war file.
Create new dynamic web project in Eclipse.



Give the project name as 'helloworld'.



Click on ‘Finish’ button.

Project structure looks like below.




Create a package ‘com.sample.app’.

Create new servlet. Right click on the package -> New -> Servlet.




Give the servlet name as ‘Welcome’.



Click on Finish button.

Update Welcome.java class like below.

Welcome.java
package com.sample.app;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/welcome")
public class Welcome extends HttpServlet {
 private static final long serialVersionUID = 1L;

 public Welcome() {
  super();

 }

 protected void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  // TODO Auto-generated method stub
  response.getWriter().append("Welcome to Docker Application Development");
 }

}


Export the application as war file.


Right click on the project -> Export -> WAR file.


Step 2: Deploy the war file to the docker container.
Execute the below command.


docker run -d -p 1234:8080 -v {WAR_FILE_PATH}:/opt/jboss/wildfly/standalone/deployments/webapp.war jboss/wildfly

$docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
$
$docker run -d -p 1234:8080 -v /Users/krishna/Documents/helloworld.war:/opt/jboss/wildfly/standalone/deployments/helloworld.war jboss/wildfly
05f8f543bf505690986eab3c40872b38b1ea925cec5aaceb2e3fec9c70d252ed
$
$docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
05f8f543bf50        jboss/wildfly       "/opt/jboss/wildfly/…"   15 seconds ago      Up 14 seconds       0.0.0.0:1234->8080/tcp   unruffled_ardinghelli
$


Open the url ‘http://localhost:1234/helloworld/welcome’ in browser, you can see below screen.


Previous                                                 Next                                                 Home

No comments:

Post a Comment