- 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
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
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.
- 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