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>"

Friday, May 8, 2020

Example of a SpringBoot data service

This is a simple database application using SpringBoot and PostgreSQL. The source code is at https://github.com/jonliu6/KnowledgeBase
  1. create a Maven project template using "mvn archetype:generate".
  2. fill in the application information.
    • groupId, artifactId, version, package
  3. open Maven project file - pom.xml and add SpringBoot and PostgreSQL dependencies.
  4. create application.properties in src/main/resources with the application name, port and database connection information.
  5. set up database and table(s) in PostgreSQL - scripts.
  6. create SpringBoot entry application class (with @SpringBootApplication)
  7. create a data model entity class (e.g. Article.java) mapping to the corresponding table (e.g. t_article).
  8. create a repository interface extends from CrudRepository from Spring Data with the common method declarations (e.g. findAll(), save() and etc.).
  9. create a controller class with org.springframework.web.bind.annotation.RestController.
    • autowire the repository in the controller.
    • create the HTTP RESTful methods in the controller.
  10. build and run the SpringBoot application by running "mvn spring-boot:run"
  11. test the controller method (e.g. http://<server>:<port>/<path>).
    • curl -X GET http://localhost:8080/articles/
    • curl -X GET http://localhost:8080/articles/Test
    • curl -X POST -H "Content-Type: application/json" -d "{"""title""":"""2nd Article for testing purpose""","""category""":"""Test""","""description""":"""This is a test record added from SpringBoot RestController again."""}" http://localhost:8080/articles/add
    • curl -X DELETE http://localhost:8080/articles/10008

Wednesday, May 6, 2020

How to create a Docker container for PostgreSQL


  1. install Docker Desktop from https://www.docker.com/get-started based on your OS.
  2. create your Docker account at https://hub.docker.com and log on.
  3. open a command window (in Windows), and type docker --version to check the docker version.
    • c:\users\testuser>docker --version
    • Docker version 19.03.8, build afacb8b
  4. start Docker Desktop and log on your Docket account.
  5. search postgres from  https://hub.docker.com and click postgres Office Image.
  6. in the command window, type "docker pull postgres" to get the latest PostgreSQL image. Note: by default, the PostgreSQL version is the latest. To get a particular version, execute "docker pull postgres:12.2".
  7. in the command window, type the following to create and start a PostgreSQL container (-p for port number <trigger port #:port# in container>):
    • docker run --name containername -h localhost -p 5435:5432 -e POSTGRES_PASSWORD=mypassword -d postgres:12.2
  8. use "docker ps" or "docker container ls" to show the running container status. "docker ps -a" lists all existing containers.
  9. to stop a docket container, type "docker stop containername"
  10. to restart an existing container, type "docker start containername"
  11. get in the docker container by typing "docker exec -it containername bash"
  12. within the container bash shell, you can then log in PostgreSQL as usual
    • psql -U postgres postgres
  13. if you specified host and port numbers when creating the docker postgres container, you can access from the local machine using postgres client
    • psql -h localhost -p 5435 -U postgres -W
  14. to remove the container, use "docker rm -f containername"
  15. to list docker images, type "docker images" or "docker image ls"
  16. to remove an image by name, use "docker image rm -f imagename", or by id using "docker image rmi -f 93d5585e579d"