Table of Contents
WAR files contain all of the components of a web application, including Java class files for servlets and web services, JSPs, HTML pages, images, and other tools that can be deployed on any servlet/JSP container. The WAR file is simply a JAR file with defined Java code directories and a single configuration file: the web.xml file, which informs the application server what to run and how to run it. The extension of WAR files is always .war. In this article, we will learn how to deploy a war file on a Tomcat Docker container.
Docker is a platform that uses containers to make it easier to build, deploy, and run applications. Containers enable a developer to package and deploy an application along with all its components, which include libraries and other dependencies, as a complete unit.
Docker is similar to a virtual machine in several ways. Unlike a virtual machine, however, Docker allows programs to use the same Linux kernel as the system they’re running on. This results in a major performance boost while also reducing the application’s size.
Before deploying a war file in a docker container, let us see how to install Docker. Installing Docker is fairly simple. Just follow the below steps. Here, we are using an Ubuntu machine.
1. Install the necessary packages required for docker. Execute the below command
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
2. Add the docker gpg key with the below command
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
3. Execute the below command to add the docker repository in your system
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
4. Finally, install docker with the below commands
sudo apt update -y sudo apt install docker-ce -y
5. Done. Docker is installed successfully. Execute the below command to verify the installation and check the docker version
6. Execute the below command to check if docker is running or not
sudo systemctl status docker
If it is running, status as ‘active’ will be displayed. Press ‘q’ to exit the status screen. If docker is not running, execute the below command to start it
sudo systemctl start docker
7. Create a symlink for docker service with the below command so that the docker service starts as soon as you start your machine
sudo systemclt enable docker
Let us now see how to deploy a war file on a tomcat docker container.
Deploy a war file on Tomcat Docker Container
1. First of all, you need to create a .war file of your web project. If you have a maven project, then execute the command mvn package to create the war file out of it. The war file will be generated in a folder named target in your maven project.
OR you can download a sample war file from the link: Download war file. A file named Hello.war will be downloaded.
Note: If you are using a cloud instance such as Amazon EC2, then follow the steps from our article: Transfer files from Windows to AWS EC2 instance to transfer the war file to the cloud instance.
2. In the terminal, create a directory and copy the war file in it.
cd mkdir docker_deploy cp /path_to_war/Hello.war docker_deploy
3. Create a dockerfile as below in the directory where you have the war file (docker_deploy)
cd docker_deploy nano dockerfile
Add the below lines in the file
FROM tomcat COPY Hello.war /usr/local/tomcat/webapps CMD ["catalina.sh", "run"]
- FROM command determines the image we want to use. The image is fetched from the docker hub.
- The COPY command is used to copy the file from the local system to a particular location in the container.
- The CMD command is used to run a command, such as starting the Tomcat server in this case.
Save and exit.
4. Build a docker image and run it as a container.
docker build -t hello . docker run -itd -p 8085:8080 hello docker ps
- The docker build command will execute the dockerfile to create an image out of it, and name the image hello.
- docker run command is used to run a docker image as a container. The parameters -itd will run the container in detached mode. The -p option is used to assign a specific port number to the service that is running inside the container. Here, we have assigned port 8085 but you can specify any port in the range 0 – 65353, but make sure the port you are assigning is not occupied by some other service.
- docker ps command displays the information about the currently running containers.
5. Done. The war file has been deployed. Now we can access our application with the URL: http://public_ip:8085/Hello/