21.8 C
Texas
angeloma
Senior Writer and partner

Deploy Apache Tomcat with Docker Compose

Hello friends, in this post, you will learn how to deploy Apache Tomcat using Docker compose. So you can deploy your applications quickly and easily in any system supported by Docker.

Apache Tomcat

Java and its multipurpose language and that make it so good and useful to learn. Well, after coding your web application, it gets to the point where you have to deploy it. To do this, it requires a server that can interpret that Java code. Remember that we are talking about web applications. And to deploy and serve it, nothing like Apache Tomcat.

Estricatemente speaking Tomcat is not a server but transforms the caught JPS into servlets that can serve the application in question.

On the other hand, Apache Tomcat is open source and can be installed on any system running Java.

Deploy Apache Tomcat with Docker Compose

1.- Install Docker on Linux

- Advertisement -

The first step is to have Docker installed in the system, obviously.

And using your distribution’s package manager, you can install Docker Compose. For example:

:~$ sudo apt docker-compose

For Debian, Ubuntu, and derivatives.

:~$ sudo dnf install docker-compose

Or, for RHEL, CentOS and derivatives.

2.- Create the docker-compose file

Spreading Apache Tomcat using the official Docker image is quite easy. In this case, we will use the latest version available but we will also add a MariaDB image so that if your application requires it you can use it without problems.

So, create the file called docker-compose.yml and add the following content:

version: '2'
services:
      tomcat:
         image: 'tomcat:9.0.39'
         restart: on-failure:3
         ports:
             - '8080:8080'
         volumes:
             - './tomcat-users.xml:/opt/bitnami/tomcat/conf/tomcat-users.xml:rw'
             - './context.xml:/usr/local/tomcat/conf/Catalina/localhost/manager.xml:rw'
             - './context.xml:/usr/local/tomcat/conf/Catalina/localhost/host-manager.xml:rw'
             - '.[.war_path]:/usr/local/tomcat/webapps/project.war
             - '.[project_folder_path]:/usr/local/tomcat/webapps/myproject

      mariadb:
         image: mariadb:latest
         restart: unless-stopped
         environment:
             - MYSQL_ROOT_PASSWORD= angelo
             - MYSQL_DATABASE= tomcat
         volumes:
             - /home/angelo/mysql:/var/lib/mysql

volumes:
tomcat_data:
driver: local

And let’s explain some things.

First, we will expose port 8080 of the host for Tomcat to use. That remains the same. But in Volume, we can define our local configuration files to be read by Tomcat. This is optional but can be useful.

Also inside the Volume section, you can reference the war file of your project as well as the project folder.

Replace each value with your own, for example, paths and filenames. As well as the port you want to expose.

In the case of MariaDB, everything is even simpler, because the configuration is very clear. In the end, replace the directory /home/angelo/mysql with the one you want to have access to the data.

Save the changes and close the file.

Before running it, you have to create the necessary directories and files.

If you don’t have the file tomcat-users.xml, create it

nano tomcat-users.xml

And add:

<?xml version="1.0" encoding="UTF-8"?>
<tomcat-users xmlns="http://tomcat.apache.org/xml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
version="1.0">
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="admin" password="angelo123" roles="manager-gui,admin-gui"/>
</tomcat-users>

Replace username and password with your own.

Also, the folder for MariaDB data:

mkdir /home/angelo/mysql

Again, replace it with yours.

3.- Running Docker Compose

Now yes, with everything ready we can perform docker compose.

sudo docker-compose up -d
Deploy Apache Tomcat with Docker Compose
Deploy Apache Tomcat with Docker Compose

And then, you can open your browser and access your applications http://your-server:8080

If you do not upload any project, you will see the following screen:

Apache Tomcat running
Apache Tomcat running

This is not an error, it just indicates that there is no project uploaded. This means that Apache Tomcat is ready for you to deploy applications.

Conclusion

With Docker we can do wonders quickly and easily thanks to docker compose. So today you have learned how to deploy Apache Tomcat.

So, share this post and join our Telegram channel and our Facebook page. Also, buy us a coffee 😉

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article