- This project is my first CRUD based web application.
- It is created in Java Programming Language using Spring Boot Framework.
- The Dockerfile will only build the docker image of the application.
- Command to build the image is:
docker build -t $IMAGE_NAME -f Dockerfile .
- Remember to change the $IMAGE_NAME tag with the desire name of the image.
- Docker Hub is a popular and public registry for the docker images.
- Docker image of this project can be found in Docker Hub under my account.
- Link to the image of this project in Docker Hub: https://hub.docker.com/r/thespiritman/firstcrudapplication
- Since this application is using MySQL as a database.So we also need to set it up.
- We will use MySQL docker image.
- Command to pull MySQL:
docker pull mysql
- Now run mysql in docker container.Command to run:
docker run -d -p 6033:3306 --name=db --env="MYSQL_ROOT_PASSWORD=root" --env="MYSQL_PASSWORD=root" --env="MYSQL_DATABASE=FirstCRUDApp" mysql
- Using
-d
flag will run the container in detached mode. - Using
-p
flag will publish the port of the container. - Using
--name
flag will assign custom_name to a container. - Using
--env
flag will set the environment to run the container. - Here
root
user is accessing the mysql server using passwdroot
. And working in database named asFirstCRUDApp
. - Remember the username and passwd of mysql must match to that set in
/src/main/resources/application.properties
. - Since the container is running in detached mode. Command to list the running container is:
docker container ls
- Now let's try logging into container. Command to get shell of
db
container.
docker exec -it db bash
- Using
-it
flag will set the container in interactive and tty mode.
- We can import our data into database.
- Command to import:
docker exec -i db mysql -uroot -proot FirstCRUDApp < init_data/users_data.sql
- Get bash of the container with above same command and check inside the FirstCRUDApp database.
- Command to link:
docker run -t --link db:mysql -p 5555:5555 thespiritman/firstcrudapplication:latest
- Using
--link
flag will link the container of our project with the container of mysql named asdb
. - Since our project is initially running in port 5555. So using
-p
flag will forward the port to 5555 in our machine.
-
docker-compose.yml
can be found in the home directory. So we can easily build and start our container using it. -
Command:
docker-compose up -d
-
It will take some time to build and load the container.
- Visit the link
http://localhost:5555
in your browser. If everything goes well, the webpage must load.