DEV Community 👩‍💻👨‍💻

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

Posted on • Updated on • Originally published at

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'


    image: mysql:8.0
      - MYSQL_USER=myapp-user
      - MYSQL_PASSWORD=password
      - 3306:3306

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


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.

This means that you can go to your 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.

Enter fullscreen mode Exit fullscreen mode

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

Top comments (4)

kswat profile image

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 ?

scottshipp profile image

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.

monoradioactivo profile image
Adrián Moreno

Thanks! Helped me a lot

Let's Get Hacking

Join the DEV x Linode Hackathon 2022 and use your ingenuity and creativity to build using Linode.

Join the Hackathon <-