Junior — Middle
How can you containerize and run a Java program in Docker?
sobes.tech AI
Answer from AI
To containerize a Java program in Docker, a Dockerfile is usually created, specifying a base image with JDK, copying the compiled jar file, and setting the startup command. Example Dockerfile for a Spring Boot application:
FROM openjdk:17-jdk-alpine
COPY target/myapp.jar /app/myapp.jar
ENTRYPOINT ["java", "-jar", "/app/myapp.jar"]
Then, in the directory with the Dockerfile, run:
docker build -t my-java-app .
docker run -p 8080:8080 my-java-app
This will create an image and run a container with the application accessible on port 8080.