How to Run Java App on Docker

How to Run Java App on Docker

Docker has become a popular platform for developers to build, package and deploy their applications. One of the many benefits of Docker is that it allows you to run your application in a consistent environment, regardless of the host operating system. In this article, we will explore how to run a Java application on Docker.

Before we get started, you will need to have Docker installed on your machine. You can download and install Docker from their official website.

Step 1: Create a Dockerfile

The first step is to create a Dockerfile for your Java application. The Dockerfile is a set of instructions that Docker uses to build an image of your application. Here's an example Dockerfile:

FROM openjdk:11-jre-slim
COPY target/my-app.jar /app.jar
CMD ["java", "-jar", "/app.jar"]

Let's break down this Dockerfile:

  • FROM: Specifies the base image for the Docker image. In this case, we are using the OpenJDK 11 runtime environment.

  • COPY: Copies the my-app.jar file from your local machine to the Docker image.

  • CMD: Specifies the command to run when the container is started. In this case, we are running the java command with the -jar option, pointing to the app.jar file.

Step 2: Build the Docker Image

With the Dockerfile in place, we can now build the Docker image using the docker build command. Open a terminal and navigate to the directory containing the Dockerfile and run the following command:

docker build -t my-java-app .

The -t option allows you to tag the image with a name (my-java-app in this case). The . specifies the current directory as the build context.

Step 3: Run the Docker Container

Once the Docker image has been built, we can run a Docker container using the docker run command. Run the following command to start the container:

docker run -p 8080:8080 my-java-app

The -p option maps the container's port 8080 to the host machine's port 8080. This will allow you to access your Java application from your host machine's web browser.

Additional Examples

If your Java application requires additional configuration files, you can copy them into the Docker image using the COPY command in the Dockerfile.

FROM openjdk:11-jre-slim
COPY target/my-app.jar /app.jar
COPY config/ /config/
CMD ["java", "-jar", "/app.jar"]

You can also pass environment variables to your Java application using the -e option in the docker run command.

docker run -p 8080:8080 -e MY_ENV_VAR=my-value my-java-app

In this article, we have covered the basics of running a Java application on Docker. By using Docker, you can create a consistent and reproducible environment for your application, regardless of the host operating system. With a little bit of configuration, you can easily package and deploy your Java application using Docker.

Related Searches and Questions asked:

  • Containerize MongoDB Best Practices
  • Containerize Nginx Best Practices
  • Containerize Java Best Practices
  • Containerize Springboot Best Practices
  • That's it for this post. Keep practicing and have fun. Leave your comments if any.

    Post a Comment

    0 Comments