skip to Main Content

I am going to install MySQL and Jdk-11 and run the jar file(spring boot project) on the container. If anyone has experience in this field, please help.
Thanks

this is my sql config

    host='localhost',
                port=3306,
                user='root',
                passwd='password',
FROM ubuntu
RUN apt-get update
RUN apt-get -y install mysql-server
RUN apt-get -y install openjdk-11-jdk
COPY target/orderCodeBackEnd-0.0.1-SNAPSHOT.jar /usr/app/  
WORKDIR /usr/app
ENTRYPOINT ["java", "-jar", "orderCodeBackEnd-0.0.1-SNAPSHOT.jar"]

4

Answers


    1. Create a container of MySQL / MariaDB by pulling image from MySQL Docker repository.
    sudo docker run --detach --env MARIADB_PASSWORD=secret --env MARIADB_ROOT_PASSWORD=secret -p 3306:3306 --add-host=YOUR_DESIRED_HOSTNAME:YOUR_LOCAL_MACHINE_IP mariadb:latest
    
    
    • –detach

    Will run the server in detached mode.

    • –env MARIADB_PASSWORD=secret –env MARIADB_ROOT_PASSWORD=secret

    Setting up environment variables for your DB server passwords. You can define the password as you wish. For me, I set it to secret

    • -p 3306:3306

    Port mapping, container internal port 3306 will be mapped to the port 3306 outside container.

    • –add-host=YOUR_DESIRED_HOSTNAME:YOUR_LOCAL_MACHINE_IP

    Don’t forget to change YOUR_DESIRED_HOSTNAME and YOUR_LOCAL_MACHINE_IP values if you want to establish a remote connection with the docker host machine. Usually, the hostname can be localhost if you are running docker on the same development machine. In such case, we don’t even need this --add-host flag.

    1. Now you can use the following connection parameters for connecting your application with the database if you run it in local.
        host: YOUR_LOCAL_MACHINE_IP
        port: 3306
        username: root
        password: secret
    

    However, if you want to access the db for your spring boot application from inside a docker container, you may have to use additional tool, docker-compose. The reason is because your host machine IP address may not work inside your docker container.

    I think, this following git repository will be helpful for you to understand how to write your first docker-compose. This repository has a readme.md file, which you can take help from.

    https://gitlab.com/mainul35/traver-auth

    Login or Signup to reply.
  1. It is good practice to separate different services in independent containers, thus creating a less related architecture.

    Next think, in docker hub we can find usefull, ready to use, images.
    We can pull all images from command line and create all services but there is better way – docker compose. So first file, that u need is docker-compose.yml:

    version: '2'
    services:
      mysql:
        image: mysql
        environment:
          - MYSQL_ROOT_PASSWORD=password
        ports:
          - 3306:3306
      app:
        build: .
        ports:
          - 8080:8080
        depends_on:
          - mysql
    

    in this file we describe this 2 services:

    • mysql:
      • image: image is from docker hub it’s offical docker mysql image
      • environment variable: u can find all possible variable in image docs
      • ports: there we can specify what port will be expose
    • app:
      • build: path to Dockerfile
      • depends_on: before you can create this service, create mysql first

    Dockerfile for your app:

    FROM openjdk:11-jre
    
    COPY target/orderCodeBackEnd-0.0.1-SNAPSHOT.jar /usr/app/
    
    WORKDIR /usr/app
    
    ENTRYPOINT ["java", "-jar", "orderCodeBackEnd-0.0.1-SNAPSHOT.jar"]
    

    now you can easily start these services from the terminal

    docker compose up -d
    

    you must be in the directory where docker-compose.yml is located or specife -f parametr

    Login or Signup to reply.
  2. //Dockerfile
    FROM openjdk:11
    ADD target/*.jar app.jar
    ENTRYPOINT ["java","-jar","app.jar"]
    
    //Dockerfile just desame to other one
    FROM openjdk:11
    ARG JAR_FILE=target/*.jar
    COPY ${JAR_FILE} app.jar
    ENTRYPOINT ["java","-jar","/app.jar"]
    
    //docker-compose.yaml
    services:
    
      yourapp:
        image: yourAppJarName:latest
        container_name: yourapp
        depends_on:
          - mysqldb
        restart: always
        build:
          context: ./
          dockerfile: Dockerfile
        
        ports: 
          - "9090:9090"
        environment:
          MYSQL_HOST: mysqldb
          MYSQL_USER: root
          MYSQL_PASSWORD: root
          MYSQL_PORT: 3306
    
      mysqldb:
        image: mysql:8.0.28
        restart: unless-stopped
        container_name: mysqldb
        ports: 
          - "3307:3306"
        
        cap_add:
          - SYS_NICE
        environment:
          MYSQL_DATABASE: dbname
          MYSQL_ROOT_PASSWORD: root 
    
    
    //application.properties or yaml
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://${MYSQL_HOST:localhost}:${MYSQL_PORT:3306}/dbname
        username: root
        password: root
    
    //customize you jar name in pom.xml
    
    </dependency>
        
        <dependency>
         ..........
         </dependency>
        <dependency>
         ..........
         </dependency>
    
    </dependencies>
    <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>   
                </plugin>
            </plugins>
            <finalName>yourAppJarName</finalName>
        </build>
    
      
    </project>
    
    
    Then click Project file then "Run as" then click maven "Install"
    
    you must also open your mysql then connect to 3307 since 3307 is expose
    
    Login or Signup to reply.
  3. The answer of @ConRed is not complete. I have done lots of changes from it in my application (which is shared here: https://github.com/Aliuken/JobVacanciesApp_Java11).

    These are my final files:

    docker-compose.yaml:

    version: "3.9"
    
    services:
      app-db-service:
        image: mysql:latest
        container_name: app-db-container
        ports:
          - "3307:3306"
        environment:
          - MYSQL_DATABASE=job-vacancies-app-db
          - MYSQL_ROOT_PASSWORD=admin
        networks:
          - internal-net
        restart: on-failure
        volumes:
          - app-db-data:/var/lib/mysql
          - ./src/main/resources/META-INF/db_dumps_folder:/docker-entrypoint-initdb.d
        cap_add:
          - SYS_NICE
        healthcheck:
          test: "mysql -uroot -padmin -e 'select 1'"
          interval: 1s
          retries: 120
          
      app-service:
        image: job-vacancies-app:latest
        container_name: app-container
        ports:
          - "9090:8080"
        environment:
          - MYSQL_HOST=app-db-container
          - MYSQL_PORT=3306
          - MYSQL_USER=root
          - MYSQL_PASSWORD=admin
        networks:
          - internal-net
          - external-net
        restart: on-failure
        volumes:
          - /AppData:/AppData
        depends_on:
          app-db-service:
            condition: service_healthy
        build:
          context: .
          dockerfile: Dockerfile
    
    networks:
      external-net:
        external: true
      internal-net:
        driver: bridge
    
    volumes:
      app-db-data:
        driver: local
    

    where:

    • ./src/main/resources/META-INF/db_dumps_folder contains my db dump file: db-dump.sql.
    • /AppData is the folder in my PC (which is Linux) that has images and documents used in the application.
    • healthcheck and service_healthy are used joint to determine when the db-dump.sql file was executed, to start the Spring Boot application after that.
    • internal-net is used to communicate the Spring Boot application with the database.
    • external-net is used to communicate the Spring Boot application with the user.

    Dockerfile:

    FROM adoptopenjdk/openjdk11-openj9:alpine
    USER root
    
    RUN mkdir /opt/apps
    RUN mkdir /opt/apps/jobVacanciesApp
    
    ARG JAR_FILE=lib/*.jar
    COPY ${JAR_FILE} /opt/apps/jobVacanciesApp/jobVacanciesApp.jar
    
    RUN addgroup -S jobVacanciesAppGroup && adduser -S jobVacanciesAppUser -G jobVacanciesAppGroup
    USER jobVacanciesAppUser:jobVacanciesAppGroup
    
    CMD ["java", "-jar", "/opt/apps/jobVacanciesApp/jobVacanciesApp.jar"]
    

    docker-compose-start.sh:

    docker volume prune -f
    docker network create "external-net"
    docker-compose build
    docker-compose up
    docker-compose start
    

    docker-compose-stop.sh:

    docker-compose stop
    docker-compose down
    docker volume prune -f
    docker network rm "external-net"
    

    application.yaml:

    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://${MYSQL_HOST:localhost}:${MYSQL_PORT:3306}/job-vacancies-app-db?useSSL=false&serverTimezone=Europe/Madrid&allowPublicKeyRetrieval=true
        username: root
        password: admin
    

    pom.xml:

    ...
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </exclude>
                        </excludes>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>com.aliuken.jobvacanciesapp.MainApplication</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>copy-installed</id>
                            <phase>install</phase>
                            <goals>
                                <goal>copy</goal>
                            </goals>
                            <configuration>
                                <artifactItems>
                                    <artifactItem>
                                        <groupId>${project.groupId}</groupId>
                                        <artifactId>${project.artifactId}</artifactId>
                                        <version>${project.version}</version>
                                        <type>${project.packaging}</type>
                                        <outputDirectory>lib</outputDirectory>
                                        <destFileName>job-vacancies-app.jar</destFileName>
                                    </artifactItem>
                                </artifactItems>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
            <finalName>job-vacancies-app</finalName>
        </build>
    ...
    

    To run the application:

    To stop the application:

    • Press in the terminal previously opened: Ctrl + C
    • Execute in the terminal: ./docker-compose-stop.sh
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search