In this blog, we will learn what is docker-compose and how we can deploy a tomcat application which uses mysql database. We will learn how we can setup a development environment using docker-compose in a single command
Prerequisite:
INTRODUCTION
docker-compose up
to start your applicationdocker-compose down
to clean up all the docker containersLet’s take an example here:
We have a project called user registration which uses mysql for storing the data . In terms of microservices, we can say that we have two services as shown below:
You can clone this git repo and try the below example
Explanation of docker-compose
3. web: This is our service name -> using image, ports and volumes
4. **volumes: **To store the database files
Now we will create main docker-compose
file which will together launch a tomcat, mysql and phpmyadmin container
Tomcat container — To run your application
**Database container **— To store the data
PhpMyAdmin — Access the database through GUI
So we will have three services
docker-compose down
all your data will retain. If you use the volume then all data will get lost if you run the docker-compose down
Also, we are importing sql file so that our database is ready with the sample data. It is good so that each developer will always have the base or the actual data to run their application on the local machine
2. phpmyadmin — which is used to access the database through GUI and it depends on service db
3. web — which is used to access your web application and it also depends on service db
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- /opt/test:/var/lib/mysql
- ./mysql-dump:/docker-entrypoint-initdb.d
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: testdb1
MYSQL_USER: testuser
MYSQL_PASSWORD: root
ports:
- 3306:3306
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
ports:
- '8081:80'
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: root
web:
depends_on:
- db
image: tomcat
volumes:
- ./target/LoginWebApp-1.war:/usr/local/tomcat/webapps/LoginWebApp-1.war
ports:
- '8082:8080'
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: testdb1
MYSQL_USER: testuser
MYSQL_PASSWORD: root
#docker-compose #docker-image #docker