Skip to content

Dockerfile for Tomcat with JDK

A Dockerfile for Tomcat with JDK defines the instructions to build a Docker image containing the Java Development Kit (JDK) and Apache Tomcat.^[600-developer__docker__Dockerfile__Dockerfile-01.md] This custom image provides a standardized runtime environment for Java web applications.

File Structure

Before building the image, the working directory should contain the Dockerfile and the necessary JDK installation package^[600-developer__docker__Dockerfile__Dockerfile-01.md]. A typical structure includes:

.
├── Dockerfile
└── jdk-8u91-linux-x64.tar.gz

Dockerfile Instructions

The Dockerfile typically uses a CentOS base image and performs the following operations^[600-developer__docker__Dockerfile__Dockerfile-01.md]:

  • Base Image: Specifies the foundation, such as centos:7.^[600-developer__docker__Dockerfile__Dockerfile-01.md]
  • Maintenance: Sets metadata like the maintainer's email using LABEL.^[600-developer__docker__Dockerfile__Dockerfile-01.md]
  • Dependencies: Installs utilities like wget using yum to facilitate downloading Tomcat.^[600-developer__docker__Dockerfile__Dockerfile-01.md]
  • JDK Installation: Extracts the local JDK archive (e.g., jdk-8u91-linux-x64.tar.gz) into the root directory.^[600-developer__docker__Dockerfile__Dockerfile-01.md]
  • Tomcat Installation: Downloads the Apache Tomcat binary archive and extracts it.^[600-developer__docker__Dockerfile__Dockerfile-01.md]
  • Environment Configuration: Sets JAVA_HOME and updates the system PATH variable to include the JDK binaries.^[600-developer__docker__Dockerfile__Dockerfile-01.md]
  • Runtime Command: Sets the default command to execute Tomcat's startup script (catalina.sh).^[600-developer__docker__Dockerfile__Dockerfile-01.md]
  • Networking: Exposes the default Tomcat port, usually 8080.^[600-developer__docker__Dockerfile__Dockerfile-01.md]

Building and Running

To create the image from the Dockerfile, the docker build command is used^[600-developer__docker__Dockerfile__Dockerfile-01.md]. Adding the --no-cache flag ensures the build process executes all instructions without relying on previous cached layers^[600-developer__docker__Dockerfile__Dockerfile-01.md].

Once built, the container can be run by mapping the container's port to the host machine^[600-developer__docker__Dockerfile__Dockerfile-01.md]. For example, to run the container and access the shell interactively, one might execute a command similar to docker run -it mytomcat /bin/bash^[600-developer__docker__Dockerfile__Dockerfile-01.md].

Sources