0

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"]
3
  • Assalamu alaikum, may I please know what error are you getting? Without knowing the error, it is difficult to provide a solution. Commented May 11, 2022 at 18:52
  • 1
    By the way, you should not install your DB server in your same container of Spring boot app. You should just pull the mysql server from docker registry, configure the network (if needed), attach it to a port and start the container. Then use the connection parameters in Spring Boot application. Commented May 11, 2022 at 18:55
  • i installed all apps but cant config sql(username , password and...) please show the best solution. @SyedMainulHasan Commented May 11, 2022 at 19:06

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
I'm sorry, I implemented your solution, but I got an error : "The driver has not received any packets from the server." can you check?
0
  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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.