I should have learned Docker earlier

Problem

In my last semester as a 3rd student, my team has a Full-stack web project. We decided to use REST API and i was responsible for develop the back-end.

The problem comes when my teammate, who was building the front-end need to test her FE code with my APIs. Our project is in local development, which means my teammate has to download my back-end code, set up the environment and database in order to do APIs testing in her local development. This take us a whole day because me and my mate are even use different OS (ubuntu and window).

Docker solution for local development

My team could have wasted no time at all if we had known how to use docker for our project. So now i will explain my solution to dockerize a local development with Spring Boot and MySql.

We will have to create 2 images: 1 for Spring Boot app and 1 for the database since docker instance is like a Virtual Machine, the Spring docker instance cannot access our local database, so we will have to create a docker instance for database too.

Spring Boot Dockerfile

FROM eclipse-temurin:17-jdk-alpine
VOLUME /tmp
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

For more about create Dockerfile for Spring Boot application, see their official document Spring Boot Docker.

Docker compose file

docker-compose.yml

version: '3.1'

services:
  db:
    image: mysql:latest
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: ecommerce3
      MYSQL_USER: user
      MYSQL_PASSWORD: password
    ports:
      - 3307:3306

  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 8080:8080
    depends_on:
      - db
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/ecommerce3
      SPRING_DATASOURCE_USERNAME: user
      SPRING_DATASOURCE_PASSWORD: password
      SPRING_JPA_HIBERNATE_DDL_AUTO: update

Notes:

  • MySql offical image is required (latest version)

  • Port 8080 and 3307 is open for connection

After that, simply run

docker compose up

Hello World means the run is successful

Check all running container:

How to dump data to MySQL image database

After docker compose, the `ecommerce3` database is still empty so we will need to dump data into it

First possible solution is access MYSQL container bash and execute all SQL statements:

 docker exec -it crud-docker-db-1 bash

Access MySQL through the terminal using the root user and the password "root"

mysql -u root -proot

Then copy all your sql statements from your sql dump file, something like this:


CREATE DATABASE  IF NOT EXISTS `ecommerce3` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;
USE `ecommerce3`;
DROP TABLE IF EXISTS `address`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `address` (
  `id` int NOT NULL AUTO_INCREMENT,
  `address_line` varchar(500) DEFAULT NULL,
  `province` varchar(200) DEFAULT NULL,
  `district` varchar(200) DEFAULT NULL,
  `commune` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `address`
--

LOCK TABLES `address` WRITE;
/*!40000 ALTER TABLE `address` DISABLE KEYS */;
INSERT INTO `address` VALUES (1,'123 Main Street','Sample Province','Sample District','Sample Commune'),(2,'3636 nho em ','khon nho em','rat nho em','khong nho em lam'),(5,'\"Đường vào tim em ôi băng giá\"',NULL,NULL,NULL),(6,'123 nha quan 1, Huyện Đan Phượng, Hà Nội',NULL,NULL,NULL),(7,'123 quan 1 , Huyện Bạch Thông, Bắc Kạn',NULL,NULL,NULL),(8,'Thanh An, Huyện Mê Linh, Hà Nội',NULL,NULL,NULL),(9,'Thon 1, qq, Huyện Sìn Hồ, Lai Châu',NULL,NULL,NULL),(10,'jhsaadkj, Huyện Lâm Bình, Tuyên Quang',NULL,NULL,NULL);
/*!40000 ALTER TABLE `address` ENABLE KEYS */;
UNLOCK TABLES;

Dump completed

The final step is to check if we have the data using endpoint /api/user/all to get all the users in database:

End

I have just figured out 1 solution for setup Spring boot and MYSQL locally using docker. I will write part 2 when i find a better solution for this, hopefully.