Docker With Spring Boot and MySQL: Docker Compose (Part 2)
The second part of this tutorial series demonstrates how to use docker-compose with the Docker CLI and a sample application to run multiple containers.
Join the DZone community and get the full member experience.
Join For FreeIn my previous article, I wrote about Docker, the CLI commands needed to run a database, and Spring Boot applications. We used Dockerfile to set up the environment and run the application by running containers separately and then building a link between them. But for multiple container applications we can use the docker-compose tool. Docker CLI can manage a single container, but docker-compose can manage multiple containers and define the dependent services.
Important Terms
If we want to run services with the docker-compose tool we have to follow these steps that are also defined in Docker documentation.
- We need to define the application environment with a Dockerfile, so it can be reproduced anywhere.
- We need to define the services that make up the application in docker-compose.yml so they can be run together in an isolated environment.
- Run docker-compose commands to run/stop the container or deploy/undeploy the application.
We need a docker-compose.yml file to write the services. In a Dockerfile, we defined the environment of the application, and in docker-compose file we write down the other properties of services, like which service will run on which port, which service will be dependent on other services, which port will be forwarded to other port for public access, define network, cluster applications, etc.
You may also enjoy: Getting Started With Docker Compose
Installation
In my previous article, I described the steps to install Docker. So, we can check that docker-compose is installed by running the command docker-compose -v
. In my case it returns: docker-compose version 1.24.1, build 4667896b
Code Download and Run
Now we can download the code from here. Or we can clone from and checkout the feature/docker branch. Here we can see the Dockerfile is similar to the code described on my previous article.
FROM java:8
COPY /build/libs/book-manager-1.0-SNAPSHOT.jar book-manager-1.0-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","book-manager-1.0-SNAPSHOT.jar"]
And the docker-compose.yml file looks like this:
xxxxxxxxxx
version'3'
services
docker-mysql
restart always
container_name docker-mysql
image mysql
environment
MYSQL_DATABASE book_manager
MYSQL_ROOT_PASSWORD root
MYSQL_ROOT_HOST'%'
volumes
./sql:/docker-entrypoint-initdb.d
ports
"6033:3306"
healthcheck
test"/usr/bin/mysql --user=root --password=root--execute \"SHOW DATABASES;\""
interval 2s
timeout 20s
retries10
book-manager-app
restart on-failure
build ./
expose
"10222"
ports
10222:10222
environment
WAIT_HOSTS mysql3306
depends_on
docker-mysql
I defined two services named by docker-mysql and book-manager-app. Service book-manager-app is dependent on docker-mysql. We are using docker-compose version 3. MySQL will run on the 3306 port at a docker container but we can access it publicly from 6033. book-manager-app will run on port 10222. We have an initial DML and DDL file at the SQL directory which will run during startup time of Docker and MySQL setup.
Run Application With docker-compose
Now we will apply some commands to run our application. I think we have already downloaded project from the above link. We will go to the project root directory. To run the application we will use following commands:
-
docker-compose up
— This will execute Dockerfile commands and will run services defined in the docker-compose file. -
docker-compose down
— This will stop and remove all containers that were running by docker-compose file. -
docker-compose up --build
— If we do an update on the Dockerfile, the war/jar file, or the docker-compose file, then we have to execute this command to get updated data on the Docker machine.
So, I think after running docker-compose up
, It runs the application with all services in the Docker machine. Ignore some initial exception logs for database communication. To check whether it runs or not, we can check http://localhost:10222/book. We can see the list of books here.
Happy Coding!!!
Further Reading
Speed Up Development with Docker Compose
Opinions expressed by DZone contributors are their own.
Comments