Docker-Compose a Spring Boot App Backed By MySql
Tags: docker, java, spring
Published: 2020-04-02 17:08:53

---
title: Docker-Compose a Spring Boot App Backed By MySql
published: true
description: A web service running atop Spring Boot and persisting data to MySql, all in Docker containers with a simple YAML file.
tags: docker, java, spring
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/skatqavcele4lkdprcru.jpg
---

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](Compose is a tool for defining and running multi-container Docker applications.)

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](https://docs.docker.com/compose/install/). 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:

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

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