Introduction
Apache Tomcat, often referred to simply as Tomcat, is an open-source web server and servlet container developed by the Apache Software Foundation. It implements several Java EE (Enterprise Edition) specifications, including Java Servlet, JavaServer Pages (JSP), and Java Expression Language. Tomcat provides a runtime environment for Java web applications to run, ensuring that they can serve dynamic content to users.
Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define an entire application stack, including services, networks, and volumes, in a single file called docker-compose.yml. With a single command, you can then spin up your entire application stack, simplifying the process of managing and deploying multi-container Docker applications.
To install Docker and docker compose on ubuntu 22.04 LTS, We can use the given link.
In this post, We will deploy tomcat container using Docker Compose on Ubuntu 22.04 LTS machine.
Step 1: Creating docker-compose.yml
To deploy a nginx container using Docker Compose, you’ll need to create a docker-compose.yml
file that defines the service. Here’s a simple example:
To create a docker-compose.yml
sudo nano docker-compose.yml
Copy and paste the followings configuration.
version: '3'
services:
tomcat:
image: tomcat:latest
ports:
- "8080:8080"
volumes:
- ./webapps:/usr/local/tomcat/webapps
environment:
- CATALINA_OPTS=-Xmx512m
This docker-compose.yml
file uses the official tomcat image from Docker Hub, exposes port 8080 on the host machine, mounts a local directory (./webapps)
to the Tomcat webapps directory (to deploy applications), and sets the maximum heap size for the Java Virtual Machine (JVM) to 512 MB.
Here’s a breakdown of the key components:
- image: Specifies the Docker image to use. In this case, it’s the official Tomcat image from Docker Hub.
- ports: Maps port 8080 on the host to port 8080 on the Tomcat container, allowing you to access Tomcat’s web interface.
- volumes: Mounts a local directory (./webapps) to the Tomcat webapps directory. This is where you can deploy your web applications.
- environment: Sets environment variables. In this example, it sets the CATALINA_OPTS variable to configure the JVM options.
Step 2: Deploy Tomcat Container
To deploy this tomcat service, follow these steps, Run the following command to start the tomcat container.
sudo docker-compose up -d
This command provides a summary of the status of each service defined in your docker-compose.yml
file. It shows information such as the service name, container ID, status, ports, and names.
Here’s an example of what the output might look like:
Name Command State Ports
-------------------------------------------------------------------------
tomcat docker-entrypoint.sh tomcat Up 0.0.0.0:8080->8080/tcp
Step 4: Logging
Some time we need to also check the real time logs, So we can use the given commands.
sudo docker-compose logs -f tomcat
Step 5: Destroy Tomcat Container
To destroy (stop and remove) the tomcat container launched with Docker Compose, you can use the following command in the directory where your docker-compose.yml
file is located:
sudo docker-compose down
This command stops and removes all the containers, networks, and volumes defined in your docker-compose.yml file.
The down command stops and removes the containers but retains the data volumes by default. If you want to remove the volumes as well, you can use the -v
option:
sudo docker-compose down -v
Make sure you are in the correct directory containing your docker-compose.yml
file when running these commands. This ensures that Docker Compose identifies the correct configuration file.
After running the docker-compose down command, you can use the docker-compose ps command to verify that the containers are no longer running. The output should be empty, indicating that no containers are currently running.
Remember that this command will stop and remove all the services defined in your docker-compose.yml file, not just the apache service. If you only want to remove a specific service, you can specify the service name:
docker-compose down -v <service_name>
Replace <service_name>
with the actual name of the service you want to remove.
Conclusion
We have successfully deployed Apache tomcat server on docker’s container using docker compose on ubuntu 22.04 LTS machine, If you still have questions, please post them in the comments section below.