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

Docker-Compose a Spring Boot App Backed By MySql

scottshipp profile image scottshipp Originally published at code.scottshipp.com ・3 min read

In last week’s post I showed how you can find the IP address of a running MySql Docker container and configure the Spring Data JPA connection for it. I mentioned there’s actually a better way, so I wanted to introduce that better way: docker-compose.

Docker Compose Logo

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.

If you followed along with last week’s post, you also 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

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

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! 🙌


Originally published at code.scottshipp.com

Discussion

pic
Editor guide
Collapse
clay profile image
Clay Ferguson

I've done a similar thing with SpringBoot + DockerCompose + MongoDb, in case anyone is interested in a similar working project:

github.com/Clay-Ferguson/quantizr

BTW, that project is a web-platform with social media, and file sharing, etc.

Collapse
monoradioactivo profile image
Adrián Moreno

Thanks! Helped me a lot