Saturday, May 9, 2020

Create Docker Container for SpringBoot Application

Pre-conditions:

  • understand how to use Docker
  • understand how to create SpringBoot applications
  1. Package your SpringBoot application using Maven - "mvn clean package".
  2. 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"]
  3. Modify pom.xml to add repackage target to have a runnable .jar and maven-jar-plugin to specify mainClass in manifest.
  4. 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).
  5. Build Docker container using "docker build -t <image_name> ."
  6. Verify the newly build image using "docker images"
  7. 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)
  8. the container can be stop and start using "docker container stop <container_name>" and "docker container start <container_name>" respectively.
  9. check container logs using "docker container logs <container_name>"

No comments:

Post a Comment