How to Create a Tomcat Docker Container (Docker Tomcat Image). In this article we will introduce what Tomcat is with it’s features and what a docker container is and move onto creating Tomcat Docker Container. Let’s get started!
What is Tomcat
Tomcat is a Java servlet container that’s open source. The Websites API, Java Server Pages and Java Servlet are just a few of the Java Enterprise Specs that can be implemented with it. Tomcat, often known as Apache Tomcat is considered a web server (or otherwise known as web container/ servlet container) that processes the relevant servlets that are then used for production applications that need to process thousands of requests.
Apache Tomcat powers numerous large scale web applications across a diverse range of industries and organizations.
- Apache Tomcat is that it’s open source.
- Easy tool for developers to use as the Apache software foundation provides regular updates to make it compatible with other software versions and providing bug fixes.
- Tomcat server can be used as web server in addition to servlet engine.
- Secure connection as Tomcat supports SSL( Secure Socket Layer).
- Cross platform compatible.
- Tomcat is also a Java application, hence running as Web NMS web server in SSL mode is much simpler.
- Tomcat also has the re direction to SSL when enabled.
- Lightweight so consumes less memory and resource utilization.
What is Docker
Docker Container is an open source platform for building, testing, deploying and managing containerized applications. Docker as a containerization platform enables developers to package applications into containers, a components with application source code and the operating system (OS) libraries and dependencies.
Docker containers are popular because organizations move to cloud native development and hybrid multicloud environments.
Follow this article and we will go through How to Create a Tomcat Docker Container (Docker Tomcat Image). For this tutorial, I have used a windows environment.
How to Create a Tomcat Docker Container (Docker Tomcat Image)
Create Tomcat Docker Image
1. Create a dev directory
When creating a development directory it will be used for creating the image and also for other purposes for this tutorial.
2. Add Docker File
When creating a docker image, a docker file is required. Create a docker file and name it as Dockerfile. Give careful attention to the naming convention and also the way you save the file. It has to start exactly with capital D with a file appended to it. Other naming conventions do not work for the docker file. If you create a file with names like docker, dockerfile, Docker, dockerFile, DockerFile, it will not work.
This is something that has to be created in a particular way. Please note, that when saving the file, ensure that file doesn’t get saved as a text file. Windows, by default, saves any file as a text file even if you append an extension name to the file. The solution is to save as “Dockerfile” within double quotes and then press the save button in windows.
Below is the sample script that is added to the Dockerfile .
FROM ubuntu:16.04 # Install prerequisites RUN apt-get -y update && apt-get -y upgrade RUN apt-get -y install openjdk-8-jdk wget RUN apt-get -y install curl RUN mkdir /usr/local/tomcat RUN wget https://downloads.apache.org/tomcat/tomcat-10/v10.0.20/bin/apache-tomcat-10.0.20.tar.gz -O /tmp/tomcat.tar.gz RUN cd /tmp && tar xvfz tomcat.tar.gz RUN cp -Rv /tmp/apache-tomcat-10.0.20/* /usr/local/tomcat/ EXPOSE 8080 # java ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64 # Define default command. CMD ["bash"] MAINTAINER email@example.com WORKDIR /usr/local/tomcat/webapps RUN curl -O -L https://github.com/bhaskarndas/sample-war/raw/main/sampletest.war CMD ["https://net.cloudinfrastructureservices.co.uk/usr/local/tomcat/bin/catalina.sh", "run"]
A) Docker File
For those who are absolute beginners in the Docker ecosystem, here are some of the explanations for the terms used in the docker file.
- FROM – It is the base image on top of which the entire container will run. In other words, you can say that it provides the environment for the container to run within the docker environment. In this case, ubuntu 16 has been used, but other alternatives can also be used, such as CentOS, alpine, etc.
- Install Perquisites – they are analogous to the basic tools, frameworks and libraries required for setting and running a development environment. The RUN command will execute the necessary installation, updating and upgradation related to installers such as java, tomcat, etc.
- EXPOSE – This allows the user to expose the ports outside the container.
- CMD – The default command prompt for the container. Without this, the container wouldn’t be able to execute commands and the container would not be able to run. It is analogous to loading a Linux system in your local machine, which would pack the command prompt and would start executing commands for loading the OS. Here Bash is the default shell.
- Maintainer – It is an optional parameter in the Dockerfile. If you are testing for a local environment, it is not necessary. However, if you are maintaining images in the docker hub, then it identifies the chief maintainer for that particular image.
- WORKDIR – It is the workspace where all the development and deployment of applications inside the container will take place.
3. Build Docker Image
When docker file has been created, it is time to build the image. So, open the windows command prompt as administrator and then cd to the development folder which was created in step 1. Now execute the following command:
docker build -t [Name Of the Image] .
In the above command syntax, dot (.) in the end represents the current working directory. It is preferred to add the docker hub username followed by the image name. This ensures easy to push and pull of the images in and from the docker hub and also easy to identify the image if uniform naming is used.
4. Running a Docker Container
After the image has been created, it can be run as a container using docker.
A) Command Syntax
docker container run -it -d --name [container name] -p port:port [image name]
The explanation for the command to run the image as a container are below:
- -d: Runs the container in the background.
- -it: Allows the user to have an ssh session with the container.
- –name – This allows the user to specify the name for the container. Notice that the image name was given when we built the image in step 3. Now a container will be running using that image, so the container name has to be distinct. The reason is obvious a single image can be used to create multiple containers across various systems or in a single system. In a microservices environment, multiple containers could be spin up as per the demand using a single image. So the naming has to be unique to identify one container from the other.
- -p port: port – It enables port forwarding from the container to the host.
- image name: The name of the image that will be used to spin up the container. If not found locally, it will search the docker hub or repositories with the same name.
5. Accessing Deployed Application
In the previous step container port has been forwarded to the host port, i.e., 8080 has been forwarded to the host’s port 8080, so the application can be easily accessed in the local environment using
Here is the list of important docker commands:
Great job! You have learned how to Create a Tomcat Docker Container (Docker Tomcat Image).
How to Create a Tomcat Docker Container (Docker Tomcat Image) Conclusion
Apache Tomcat is the most widely and commonly used software among web application developers today. Studies have claimed that more than 60% of java applications make use of apache tomcat.
In this tutorial, we learned how to build an image using the apache tomcat installer. Basic principles are the same for creating images using a docker file. The image created locally can be easily pushed to the Docker hub, and the same can be used across various environments. There is a lot more than image creation that can’t be covered in this article. However, it should provide some basics of image building for beginners. For more visit apache tomcat windows