Elevate Your Deployment Process: Dockerizing Java, Python, and React Applications

Dockerizing Java, Python, and React Applications

In today's software development landscape, containerization has become an essential practice. It provides a consistent and isolated environment for running applications, making deployment and scaling more manageable. In this blog post, we'll explore how to Dockerize Java, Python, and React applications.

If you don't know how to set up an AWS Linux EC2 instance and effortlessly install Docker by following this step-by-step guide: devtestops.hashnode.dev/getting-started-wit...

Java Applications Dockerization

When it comes to Java applications, there are two primary types we'll discuss: normal Java applications without Spring Boot and Java applications with Spring Boot.

  1. Normal Java Application without Spring Boot

These applications are typically packaged as WAR (Web Application Archive) files and deployed to a web server like Tomcat.

GitHub Link: https://github.com/Pankaj-Surya/maven-web-app

Here's the Dockerfile for containerizing a normal Java web application:

# Use Tomcat 8 as base image
FROM tomcat:8.0.20-jre8
# Copy the WAR file to the Tomcat webapps directory
COPY target/app.war  /usr/local/tomcat/webapps/app.war
# Expose port 8080 for access
EXPOSE 8080

Here's how you can build and run the Docker container:

# Build the Docker image
$ docker build -t maven-web-app .
# Run the Docker container
$ docker run -d -p 8080:8080 maven-web-app

Output:

Note: Please open aws machine TCP port 8000

  1. Spring Boot Applications

Spring Boot simplifies Java application development by providing an embedded server. These applications are typically packaged as JAR files.

GitHub Link: https://github.com/Pankaj-Surya/spring-boot-docker-app

Here's the Dockerfile for containerizing a Spring Boot application:

# Use OpenJDK 11 as base image
FROM openjdk:11
# Copy the JAR file to the container
COPY target/app.jar  /usr/app/app.jar
# Set working directory
WORKDIR /usr/app/
# Expose port 8080 for access
EXPOSE 8080
# Run the Spring Boot application
ENTRYPOINT [ "java", "-jar", "app.jar" ]

To build and run the Docker container for a Spring Boot application:

# Build the Docker image
$ docker build -t sbapp .
# Run the Docker container
$ docker run -d -p 9090:8080 sbapp

Output :

Note: Please open aws machine TCP port 9090

Python Application with Docker

Python is a versatile scripting language used for various purposes. Dockerizing Python applications can simplify their deployment.

GitHub Link: https://github.com/Pankaj-Surya/python-flask-docker-app

Here's the Dockerfile for containerizing a Python application:

# Use Python 3.6 as base image
FROM python:3.6
# Set maintainer information
MAINTAINER Pankaj <Pankaj.b@oracle.com>
# Copy the application files to the container
COPY . /app/
# Set working directory
WORKDIR /app/
# Expose port 5000 for access
EXPOSE 5000
# Install required Python packages
RUN pip install -r requirements.txt
# Run the Python application
ENTRYPOINT [ "python", "app.py" ]

To build and run the Docker container for a Python application:

# Build the Docker image
$ docker build -t python-flask-app .
# Run the Docker container
$ docker run -d -p 5000:5000 python-flask-app

Output:

Note: Please open aws machine TCP port 5000

React JS Application with Docker

React JS is a JavaScript library used for building user interfaces. Dockerizing a React JS application involves using Node Package Manager (npm) to manage the required software.

GitHub Link: https://github.com/Pankaj-Surya/react-docker-app

Here's the Dockerfile for containerizing a React JS application:

# Use the latest Node.js image as base
FROM node:latest

# Set working directory in the container
WORKDIR /app
# Copy package.json to the container
COPY package.json ./
# Install required packages
RUN npm install
# Copy the application files to the container
COPY . .
# Start the React application
CMD ["npm", "start"]

To build and run the Docker container for a React JS application:

# Build the Docker image
$ docker build -t react-app .
# Run the Docker container
$ docker run -d -p 3000:3000 react-app

Output:

Note: Please open aws machine TCP port 3000

Troubleshooting

During the containerization process, you might encounter issues. Here are some troubleshooting tips:

  • To print container logs:

      $ docker logs <container-id>
    
  • To enter a running container:

      $ docker exec -it <container-id> /bin/bash
    

Remember to enable the necessary ports (8080, 9090, 5000, etc.) in the security group of your Docker machine to access the applications through your browser.

In conclusion, Dockerizing applications simplifies deployment, improves portability, and ensures consistency across various environments. By following the steps and Dockerfiles provided, you can containerize your Java, Python, and React applications effectively.