Day 21 of 90 Days of DevOps Challenge: Dockerfile Techniques & Java Web App Containerization

Vaishnavi DVaishnavi D
2 min read

After learning the Dockerfile fundamentals on Day 20, today I got hands-on with writing my own Dockerfile, building a custom Docker image, and taking the next step -Dockerizing a Java web application using Maven and Tomcat.

What is a Dockerfile?

A Dockerfile is a simple text file containing a list of instructions that define how to build a custom Docker image. These instructions guide Docker in setting up the environment, installing dependencies, copying files, and setting up commands.

Sample Dockerfile Example

Let’s start with a basic example to understand how Dockerfile instructions work:

FROM ubuntu
MAINTAINER zerotoroot <zerotoroot@gmail.com>

RUN echo 'hello from run instruction-1'
RUN echo 'hello from run instruction-2'

CMD echo 'hi from cmd-1'
CMD echo 'hi from cmd-2'

Build and Run the Image

# Build the Docker image
docker build -t img-1 .  # Builds a Docker image from the current directory and tags it as `img-1`

# Run the image
docker run img-1

Note: Only the last CMD instruction will be executed when the container runs. So, in this case, you'll see:

hi from cmd-2

What is Docker Hub?

Docker Hub is a cloud-based repository where you can store, manage, and share your Docker images. Think of it like GitHub for containers.

How to Use Docker Hub

  1. Login to your Docker Hub account:

     docker login
    
  2. Tag your image before pushing:

     docker tag img-1 zerotoroot/img-1
    
  3. Push the image to Docker Hub:

     docker push zerotoroot/img-1
    

Now, anyone with access can pull your image using:

docker pull zerotoroot/img-1

Dockerizing a Java Web Application

Here’s how to containerize a simple Maven-based Java web application:

Project Packaging

  • Use Maven to package your app:

      mvn clean package
    
  • This will generate a .war file inside the target/ directory (e.g., target/app.war).

Dockerfile for Java Web App

FROM tomcat:latest

MAINTAINER ZerotoRoot

EXPOSE 8080

COPY target/app.war /usr/local/tomcat/webapps/

Build and Run

docker build -t java-web-app .

docker run -d -p 8080:8080 java-web-app

Access the App

http://<your-public-ip>:8080/app/

Make sure port 8080 is open in your firewall or AWS security group.

Final Thoughts

Today’s session helped solidify my understanding of how to structure and build custom Docker images using Dockerfiles, how Docker Hub works as a registry for sharing and storing container images, and how to containerize and run a Java-based web application using Tomcat. I’m now a step closer to mastering application containerization, and tomorrow, I’ll be diving into Docker volumes and managing environment variables inside containers.

stay tuned!

0
Subscribe to my newsletter

Read articles from Vaishnavi D directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vaishnavi D
Vaishnavi D