SpringBoot Example with Docker mysqldb image

As part of this session we're going to learn how to use the MySQL docker image to store our details into the database. Till our last session we have used local MySql installed database. In this session will be using the containarized mysql databaase to capture the values into remote database.

From our previous session SpringBoot with Docker we have seen how to install the docker desktop.

MySql setup using Docker

  1. 1. First pull the docker image using following command. docker pull mysql. If we type this command in command prompt or terminal by default the latest image will get downloaded from the docker-hub



  2. 2. Once it is downloaded we need to run the MySql docker image using below command. docker run -p 3307:3306 --name mysqldb mysql Once we run this command we can see the below screen to specify the environment variables.



  3. 3. Then next we can rerun the mysql image by specifying the required environment variables. docker run -p 3307:3306 --name mysqldb -e MYSQL_ROOT_PASSWORD=admin -e MYSQL_DATABASE=productdb mysql Before we executing the command we need to remove the already running mysqldb, execute following command to check the running container details docker ps -a Remove the container using followring command docker rm <<CONTAINER ID>>





  4. 4. MysqlDB is successfully started on the 3307 port number. We can open any Mysql IDE (ex: MySql Workbench) and then connect using following details
    1. Host: localhost
    2. Port: 3307
    3. Username: root (default)
    4. Password: admin


Implement product microservice
  1. 1. Implement product microservice using below REST endpoints.
    1. HTTP POST: /api/v1/product/create
    2. HTTP GET: /api/v1/product/getAll
    3. HTTP GET: /api/v1/product//getById/{productId}
  2. Full source code is available in follwong GitHub repository: SpringBoot With containarized docker mysql image Example

  3. Inside the product microservice create the Dockerfile and add the below code to containarized the spring boot application.
    
      FROM openjdk:17-jdk-slim
      WORKDIR /opt/app
      ARG JAR_FILE=target/sb-product-service-0.0.1-SNAPSHOT.jar
      COPY ${JAR_FILE} sb-product-service.jar
      ENTRYPOINT ["java","-jar","sb-product-service.jar"]
    
    
    First run the mvn clean install in the root directory. Post that in target folder sb-product-service-0.0.1-SNAPSHOT.jar jar file will be generated. Just copy paste the jar file in Dockerfile as shown above.

  4. 2. Next navigate the project root directory and then next run the following command to run the build for the creation of image, docker build -t sb-product-service .



  5. 3. Run the application using following command, docker run -p 8099:8080 sb-product-service, once we run this command the application startup will fail with the error message as Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure because while we ran the service we have not passed the database details to establish the connectivity between application and mysqldb.

  6. 4. Run the application by passing the all required properties using below command, even though the application startup will fail with the same reason, because mysql and application container both should be map to the single network.
    docker run -p 8099:8080 --name sb-product-service -e MYSQL_HOST=mysqldb -e MYSQL_USER=root -e MYSQL_PASSWORD=admin -e MYSQL_PORT=3306 sb-product-service
Docker Network

Mysql and application containers will be running on isolated network. If we want to connect these two isolated networks then we need to create the network and assign it to the both containers.

  1. 1. First check the network lists using following command. docker network ls


  2. 2. If we want to know more about the network commands, execute the following docker network ls


  3. 3. Next create the network using following command, docker network create spring-mysql-product-service-net, once network is created assign the newly created network to the running containers using following command, docker network connect spring-mysql-product-service-net mysqldb, Once it is assigned let inspect, is mysql connected with newly created network or not. docker container inspect mysqldb