DEV Community

Cover image for Docker-Compose a Spring Boot App Backed By MySql
scottshipp
scottshipp

Posted on • Updated on • Originally published at code.scottshipp.com

Docker-Compose a Spring Boot App Backed By MySql

You can find the IP address of a running MySql Docker container and configure the Spring Data JPA connection for it by running Docker CLI commands but there’s actually a better way: docker-compose.

What is docker-compose?

Compose is a tool for defining and running multi-container Docker applications.
~ from the docker-compose site

Essentially, compose is the right tool for the job. Our goal is having a Spring Boot app running in a Docker container, a MySql instance running in a Docker container, and a connection between them. Docker-compose will allow us to define this setup declaratively and will automate it for us. Let’s get started.

Installing docker-compose

First you will of course need to install docker-compose. It’s pretty easy and you’ll find the instructions in the installation section of the documentation. Go check that out and then come back here. If you can run “docker-compose –version” then you’re ready to proceed.

Writing a docker-compose file

OK the next thing you need is a “compose file.” This is a YAML file named “docker-compose.yml” that you put in the root directory of your application. It consists of a series of entries defining each Docker container in your composition. We want a Spring Boot app docker container and a MySql docker container. So we’ll have two entries.

This of course assumes that you’ve already written a Dockerfile for your Spring Boot app. There are plenty of resources online for how to do that.

Before we begin, you should have a MySql Docker container already, that your Spring Boot app can connect to. So get off this page right now and go make sure you have all that stuff going. Basically, you should be able to run something like 'docker start myapp-mysql' to start the MySql container and something like 'docker run -p 8080:8080 –name myapp-main -t {container id}' to run the container with the Spring Boot app.

Now just declare the two things in docker-compose.yml:

version: '3'

services:

  myapp-mysql:
    image: mysql:8.0
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=MyAppDb
      - MYSQL_USER=myapp-user
      - MYSQL_PASSWORD=password
    ports:
      - 3306:3306

  myapp-main:
    image: myapp-image-name-that-appears-when-you-run-docker-images
    restart: on-failure
    depends_on:
      - myapp-mysql
    ports:
      - 8080:8080
    environment:
      - DATABASE_HOST=myapp-mysql
      - DATABASE_USER=myapp-user
      - DATABASE_PASSWORD=password
      - DATABASE_NAME=MyAppDb
      - DATABASE_PORT=3306
Enter fullscreen mode Exit fullscreen mode

docker-compose.yml

Networking the containers together

Part of docker-compose’s default behavior these days is to automatically create a network for the containers to run on, and it gives each container a hostname identical to its container name.

By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
https://docs.docker.com/compose/networking/

This means that you can go to your application.properties file in the Spring Boot app and change the connection from an IP address to (using this example) “myapp-mysql” and change any of the other configuration values to match what’s in docker-compose.yml.

spring.datasource.url=jdbc:mysql://myapp-mysql:3306/MyAppDb?useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=myapp-user
spring.datasource.password=password
Enter fullscreen mode Exit fullscreen mode

application.properties

Don’t forget that you need to rebuild the Docker container for your Spring Boot app after making this change (probably with “mvn package dockerfile:build”).

Bring the composition up

Now you just run “docker-compose up” and docker-compose will do the work of bringing up both containers and supplying them with the configuration contained in docker-compose.yml. It makes it so easy!

You should be able to then interact with your application the normal way, via its published port. Of course you can use all the usual Docker commands while its running also: “docker container ls”, “docker inspect”, etc.

When you’re ready to bring it all down again, just run “docker-compose down.”

Congratulations! You have docker-compose skills now! 🙌

Oldest comments (3)

Collapse
 
monoradioactivo profile image
Adrián Moreno

Thanks! Helped me a lot

Collapse
 
kswat profile image
kswat

Thanks for the blog.
Can I ask how to secure passwords in docker compose?
I have encrypted password in my application properties, how can I supply that from docker compose? DATABASE_PASSWORD=ENC(myencpassword) and MYSQL_PASSWORD=realpassword ?

Collapse
 
scottshipp profile image
scottshipp

Sorry for the late reply. There are various methods. One of them would be to securely populate an environment variable within the process that launches it, and refer to the environment variable within Docker Compose.