Create Docker Container for SpringBoot Application
Pre-conditions:
- understand how to use Docker
- understand how to create SpringBoot applications
- Package your SpringBoot application using Maven - "mvn clean package".
- Create a DockerFile
- FROM openjdk:11-slim
- COPY ./target/KnowledgeBase-1.0.jar /usr/app/KnowledgeBase-1.0.jar
- WORKDIR /usr/app
- RUN sh -c "touch KnowledgeBase-1.0.jar"
- ENTRYPOINT ["java", "-jar", "KnowledgeBase-1.0.jar"]
- Modify pom.xml to add repackage target to have a runnable .jar and maven-jar-plugin to specify mainClass in manifest.
- Note: if your SpringBoot access a database (e.g. PostgreSQL) in another Docker container, need to change JDBC from localhost to the IP address of the database container (in application.properties).
- Build Docker container using "docker build -t <image_name> ."
- Verify the newly build image using "docker images"
- Run the Docker container using "docker run --name <container_name> -p 8090:8080 <image_name>". Note: the trigger port can be different from the original one (e.g. 8090:8080)
- the container can be stop and start using "docker container stop <container_name>" and "docker container start <container_name>" respectively.
- check container logs using "docker container logs <container_name>"
No comments:
Post a Comment