To create a Docker image for a Spring Boot application, you first need to create a Dockerfile
for the application. The Dockerfile
contains the instructions for building the Docker image.
Here is an example Dockerfile
for a Spring Boot car application:
1 2 3 4 5 6 |
FROM openjdk:8-jdk-alpine VOLUME /tmp ARG JAR_FILE COPY ${JAR_FILE} app.jar ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] |
In the Dockerfile
above, we start with the openjdk:8-jdk-alpine
image, which contains the OpenJDK 8 runtime environment. We then create a volume at /tmp
for temporary files.
Next, we copy the JAR file for the Spring Boot application into the image and set it as the app.jar
file. Finally, we set the entrypoint for the image to run the java
command with the -jar
option to start the Spring Boot application.
To build the Docker image, run the following command in the directory containing the Dockerfile
:
1 2 |
docker build -t my-car-app . |
Replace my-car-app
with the desired name for your Docker image. The .
at the end of the command specifies the current directory as the build context.
Once the image is built, you can run a Docker container using the following command:
1 2 |
docker run -p 8080:8080 my-car-app |
The -p
option maps the container’s port 8080 to the host’s port 8080. This allows you to access the Spring Boot application from your host at http://localhost:8080
.
That’s it! You now have a Docker image for your Spring Boot car application. You can share this image with others or deploy it to a production environment.