Build a Docker image using a Dockerfile

Category : Kubernetes | Sub Category : Kubernetes With Java | By Prasad Bonam Last updated: 2023-11-21 10:35:52 Viewed : 219


Below is a simple example of a Dockerfile that you can use to build a Docker image for a basic Java application. In this example, assume you have a Java application JAR file named myapp.jar:

Dockerfile
# Use an official OpenJDK runtime as a base image FROM openjdk:11-jre-slim # Set the working directory inside the container WORKDIR /app # Copy the JAR file into the container at /app COPY myapp.jar /app/ # Specify the command to run on container start CMD ["java", "-jar", "myapp.jar"]

Here is a breakdown of what each line in the Dockerfile does:

  • FROM openjdk:11-jre-slim: Specifies the base image to use, which is an official OpenJDK 11 runtime image with a slim Debian-based image.

  • WORKDIR /app: Sets the working directory to /app inside the container.

  • COPY myapp.jar /app/: Copies the myapp.jar file from the local directory into the /app directory in the container.

  • CMD ["java", "-jar", "myapp.jar"]: Specifies the default command to run when the container starts. In this case, it runs the Java application using the JAR file.

To build the Docker image, navigate to the directory containing your Dockerfile and the JAR file and run the following command in the terminal:

bash
docker build -t my-java-app .

This assumes that the Dockerfile is named Dockerfile (without any file extension) and is in the current directory. The -t flag is used to tag the image with a name, in this case, my-java-app.

After the build completes, you can run a container based on this image with the following command:

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

This command maps port 8080 from the host to port 8080 in the container. Adjust the ports as needed based on your applications configuration.

Remember to replace myapp.jar with the actual name of your JAR file if it is different. Additionally, make sure that your JAR file is in the same directory as your Dockerfile.

lets walk through an example of building a Docker image for a Spring Boot application using a Dockerfile. In this example, I will assume you have a basic Spring Boot application with the following structure:

plaintext
my-spring-app/ |-- src/ | `-- main/ | `-- java/ | `-- com/ | `-- example/ | `-- MySpringApp/ | `-- MySpringAppApplication.java |-- Dockerfile |-- .dockerignore |-- mvnw |-- mvnw.cmd |-- pom.xml

Here is the content of the files:

  1. MySpringAppApplication.java:

    java
    package com.example.MySpringApp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MySpringAppApplication { public static void main(String[] args) { SpringApplication.run(MySpringAppApplication.class, args); } }
  2. Dockerfile:

    Dockerfile
    # Use the official Maven image as a build stage FROM maven:3.8.4-openjdk-11 AS build # Set the working directory WORKDIR /app # Copy the project files and build the application COPY pom.xml . COPY src ./src RUN mvn clean install # Use the official OpenJDK image as the runtime stage FROM openjdk:11-jre-slim # Set the working directory WORKDIR /app # Copy the compiled JAR file from the build stage COPY --from=build /app/target/my-spring-app-*.jar my-spring-app.jar # Expose the port that the application will run on EXPOSE 8080 # Define the command to run the application CMD ["java", "-jar", "my-spring-app.jar"]
  3. .dockerignore:

    plaintext
    target/
  4. pom.xml:

    (This is a standard Spring Boot pom.xml file.)

Now, lets build the Docker image:

bash
docker build -t my-spring-app:1.0 .

This command builds a Docker image named my-spring-app with the tag 1.0 using the provided Dockerfile and application files.

After building the image, you can run a container based on it:

bash
docker run -p 8080:8080 my-spring-app:1.0

This command starts a container based on the my-spring-app:1.0 image and maps port 8080 on the host to port 8080 on the container.

You can then access the Spring Boot application by navigating to http://localhost:8080 in your web browser. This is a basic example, and depending on your specific Spring Boot application, you might need to adjust the Dockerfile accordingly.

Search
Related Articles

Leave a Comment: