Skip to content

A simple example which combines 2 docker files, one to spin up a mysql container and a java container. Also put together these containers in the same network in order to communicate throughout container name.

Notifications You must be signed in to change notification settings

mixaverros88/dockerized-java-mysql-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dockerize java mysql example project

This simple example combines 2 docker files or docker-compose file, one to spin up a mysql container and the other a java container.

These docker files are located in the corresponding folders (mysql, java).And the docker-compose file in the root folder.

Also you can find a network folder, which contains an sh file to create a network in order to allow the communication between the containers throughout containers names.


Start the mysql container

preview image You can find the mysql dockerfile in the mysql folder. There you can run the build.sh file in order to build the docker image or you can run the following command.

docker build -t my-mysql .

If you want to change the database name you can change the ENV in dockerfile or you can remove it from docker file and pass the value at the run stage.

After the build of the image, you can run the image. You can use the run.sh file. At the run stage you can specify the root pass, and the ports. Or you can run the below command

docker run -d -p 3306:3306 --name mysqlapp -e MYSQL_ROOT_PASSWORD=supersecret my-mysql

If you want to validate the creation of the database you can connect to the container with the below commands.

Connect in the running container

docker exec -t my-mysql bash 

log in as a root

mysql -uroot -p

Root credentials

supersecret

Display all the databases

showdatabases;

Use the company database

use company;

Display all the tables of company database

show tables;

Show all the rows of a employees

select * from employees;

If the results are the following.

preview image

The process is correct.


Start the java container

You can find the java dockerfile in the java folder. There you can run the build.sh file in order to build the docker image or you can run the following command.

docker build -t java-connect-with-mysql .

After the build of the image, you can run the image. You can use the run.sh file. At the run stage you can specify the ports and the volume. Or you can run the below commands

filepath=$PWD
cd $filepath
mkdir deploy
cd deploy
# Download the war from gihub
curl -LOk https://github.com/mixaverros88/docker_with_java_and_mysql/raw/master/java/target/RESTfulExample.war
# run the docker
winpty docker run  -d --name javaapp -p 80:8080 -v /$(pwd):/usr/local/tomcat/webapps java-connect-with-mysql

You can reach this service through docker machine ip/demorest/ To find docker machine ip execute the following command

docker-machine ls

Create network

preview image Create a docker bridge network in order to allow communication between the containers, through container name instead of container IP.

docker network create --driver bridge isolated 

Add db container into the network

docker network connect isolated db

Add java-connect-with-mysql container into the network

docker network connect isolated java-connect-with-mysql

Create secrets

echo "my-db-pass" | docker secret create mysql_root_pass -

About

A simple example which combines 2 docker files, one to spin up a mysql container and a java container. Also put together these containers in the same network in order to communicate throughout container name.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published