Home > Software design >  install mysql and java jdk-11 in Dockerfile and run my spring boot jar file in container
install mysql and java jdk-11 in Dockerfile and run my spring boot jar file in container

Time:05-12

I am a beginner in Docker and I am going to install ubuntu Linux on Docker, install it on 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"]

CodePudding user response:

  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

CodePudding user response:

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

  • Related