Docker for devs with hands-on| Part-5
In case you are landing here directly, it’s recommended to read through this documentation first.
Following are the topics, which we shall be touching through this blog :-
- Deploying full fledged JAR file (Spring Boot based App) inside the Docker container containing JDK based docker image.
- Deploying MYSQL database inside the Docker container.
- Demonstrating containers talking to each other.
- SpringBoot based Webapp talking to Mysql (running with different container).
- Assigning labels to the data-volumes for data used by Mysql.
- Allocating customised directories from host machine, for data used by Mysql.
- Automating the Docker-Image generation using Maven.
Question:- First, Kindly demonstrate the launch-process of a simple Spring-Boot based application through JDK based Docker container with the help of Dockerfile :-
Part #1.) Let’s search for a right Docker based Image (containing JDK) . We shall be using the OpenJDK version 8u312.
Part #2.) Let’s utilise the below-mentioned Dockerfile for launching container containing JDK and thereby deploying our JAR file into it :-
MAINTAINER Aditya Goel "firstname.lastname@example.org"
COPY fleetman-0.0.1-SNAPSHOT.jar webapp.jar
CMD ["java", "-Dspring.profiles.active=docker-demo", "-jar", "webapp.jar"]
Part #3.) Let’s now head to our true friend terminal and change directory to the place, where we have our Dockerfile as demonstrated above in step #1.
Part #4.) Let’s now perform the process of building a DockerImage from the aforementioned Dockerfile :-
docker image build -t webapp-springboot-aditya .
- Note that, we had kept the name of the docker-image as “webapp-springboot-aditya”.
- Also, observe that, there are only two things essential in order to generate a Docker-Image, first one is Dockerfile and another one is the JAR file. That’s the reason, we are building DockerImage from the directory, which contains as less as number of files possible.
Part #5.) Let’s check whether, our DockerImage has been formed well :-
Part #6.) Let’s launch a fresh container from this newly formed image :-
docker container run -itd -p 8089:8080 <NAME_OF_DOCKER_IMAGE>
Part #7.) Lets now login into our container :-
docker container exec -it d1 bash
Part #8.) Let’s verify, the JDK version in our container :-
Part #9.) Let’s observe whether our webapp is running now. Note that, host port is 8089 :-
Now, we have our web-application running based on the SpringBoot.
Part #10.) Let’s now see our container :-
docker containers ps
Question:- Now, Kindly demonstrate the launch-process of a container, which contains MYSQL installed into it :-
Part #1.) Let’s search for a right Docker based Image for powering to MYSQL. We shall be using the Mysql Version5 for this demo :-
Part #2.) Let’s investigate the Dockerfile for the mysql :- We can see that, somewhere volume is being mounted inside the Docker Container.
Part #3.) Let’s now go ahead and download the concerned docker image for
docker container run -d -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=fleetman mysql:5
- Note that, “-d” option indicates the detached mode.
- Note that, “-e” option indicates that, we can supply environment variable while launching the container.
Part #4.) Let’s now verify, whether the docker image got downloaded or not ? → Yes, we can see that, docker image “mysql” with TAG as “5” has been well downloaded.
Part #5.) Let’s now verify, whether the docker container is up and running :-
docker container ps -a
Part #6.) Let’s now login to the container and especially to the MYSQL console to observe, whether container launched, really have MYSQL installed in it or not :-
docker container exec -it e7 bash
Question :- Let’s talk something about the Docker Networks ?
Answer:- Lets first investigate the networks that we have on our host machine :-
docker network ls
Bridge Network → This is the default network. It’s the network that all of our containers have been connecting to.
- This Bridge Network will allow us Outbound Traffic.
- But unfortunately, the bridge network does not allow container to container networking.
If we want container to container networking, then we have to create our own network and add it to this list, which is really simple to do.
Host Network → The host network here is the network that your host is connected to. So that is your connection to your router, basically.
None Network → It would simply mean your container would have no networking at all. Nothing would be able to connect to it, but crucially, it wouldn’t be able to connect outwards either. So, I guess you could use that if you really genuinely don’t want your container to be outward.
Question :- Let’s create our own network :-
Answer:- In order for dockers to communicate with each other, our own network is required :-
docker network create <NAME_OF_NETWORK>
Question :- Can you demonstrate the containers talking to each other ?
Answer:- Here, we are going to demonstrate that, how dockers can talk to each other with the help of networks :-
Step #1.) We start our container(powering MYSQL) with proper name as well as network :-
docker container run -d -e MYSQL_ROOT_PASSWORD=password --e MYSQL_DATABASE=fleetman --network aditya-network --name mysqldbpanga mysql:5
- Note that, we have used option “ — network” with the help of which we have defined the network that we shall be using to launch this container.
- Note that, we have used option “ — name” with the help of which we have defined the name of our network.
Step #2.) Let’s now see, whether our container got launched :-
Step #3.) Now, we start our container(powering SpringBoot micro-service) with proper name as well as network :-
docker container run -itd -p 8086:8080 --network aditya-network --name humanfleetasia webapp-springboot-aditya
- Note again that, we have used option “ — network” with the help of which we have defined the network that we shall be using to launch this container.
- Note again that, we have used option “ — name” with the help of which we have defined the name of our network.
- Also observe that, we have used port option “— p”, with the help of which, we have defined the host-port at which the webapp that shall be exposed from within the container.
Step #4.) Let’s now see, whether our container got launched :-
Step #5.) Let’s now login to our above container :-
docker container exec --it 3b bash
Step #6.) Now, we install the “PING” software inside this container :-
apt-get update && apt-get install iputils-ping
Step #7.) Now, let’s try to ping other container (the one powering to MYSQL) from this container :-
Question :- Can you demonstrate the full fledged webapp talking to mysql in an another container ?
Step #1.) Here is how our configuration properties file looks like :-
Step #2.) Here is how our Dockerfile looks like :-
MAINTAINER Aditya Goel "email@example.com"
COPY fleetman.jar webapp.jar
CMD ["java", "-Dspring.profiles.active=docker-demo", "-jar", "webapp.jar"]
Step #3.) Let’s now build the docker-image from the Dockerfile :-
docker image build -t fleetmanjarimage5 .
Step #4.) Let’s now check, whether our new docker images have been formed
Step #5.) Let’s now launch a container with the help of above docker-image :-
docker container run -itd -p 8085:8080 --network aditya-network --name fleetmantrackercontainer4 fleetmanjarimage5
Step #6.) Let’s now check whether our application has been launched :-
Step #7.) Let’s also verify, whether the db-tables got created within our database :-
Question :- As we know that, if we exited and exited this container, then our data shall be lost, Is there somewhere the data of our container (Mysql) is saved somewhere as well ?
Answer:- We can actually know, where would the data from this container shall be saved locally on our Host by running following query :-
- Inside the Docker-container, the data of mysql is saved at location : “/var/lib/mysql”.
- At our Host, the data of mysql is saved at location : “/var/lib/docker/volumes/<volume_id>/_data”. Also note that, for MAC system, this location is actually on the file system of that virtual machine that you’re running in the background. We very rarely see it or feel it or it really care that it’s there, but it is there and that’s where it’s stored.
Question :- Onto our host machine, how can we really verify, whether this volume exists or not ?
Answer:- We can verify the same by using below command :-
docker volume ls
Question :- Can we allocate some name of the aforesaid auto-volume created by Docker container (powering to MYSQL) ?
Answer:- We can very well assign the name of the volume. Let’s re-launch the Docker(powering to Mysql) that we launched above, with label for the volume assigned too :-
docker container run -v mysqldata:/var/lib/mysql -d -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=fleetman --network aditya-network --name mysqldbpanga mysql:5
- Note that, with “-v” flag, we have assigned the label “mysqldata” to the volume : “/var/lib/mysql” within our container.
- Note that, with “-d” flag, we indicate that, the container shall run in detached mode.
- Note that, with “-e” flag, we wanted to pass some environment variables to our container.
- Note that, with “-network” flag, we wanted to inform to our container that we want this network “aditya-network” to be attached to this container.
- Note that, with “ — name” flag, we wanted to assign the name “mysqldbpanga” to our container.
Question :- Can we inspect the volume now ?
Answer:- We can very well inspect the docker volume now :-
docker volume inspect <NAME_OF_VOLUME>
Note that, “Mountpoint” in above output indicates the path to the directory at host machine, where data from the respective container shall be saved into.
Question :- Please demonstrate the working with volumes ?
Answer:- We shall perform following steps :
Step #1.) First launch a container with data-volume mounted and log into the same and then we shall access mysql and create table and insert some data into it.
Step #2.) Next, we shall destroy this container totally :-
Step #3.) Next, we now launch yet another fresh container with same data-volume and log into it :-
Note that, container-id for this particular container is different from what it was launched earlier.
Step #4.) Now, we would verify that, whether the (table+data) created in first container is still present within the second container as well.
Well yes, from above screenshot, it’s clear that, whatever data we wrote in previous container’s mysql is still preserved within this container.
Step #5.) Note that, actual data at host-machine is stored at some strange location :-
Question :- Now that, we have seen that some strange location on the host machine has been chosen by docker itself, Is there a way to specify our own location at the host machine as well ?
Answer:- We can very well assign our own location at the host-machine too. Here we shall be performing the following steps to demonstrate the same :-
Step #1.) First we shall launch a container with our own directory at our host machine and then login inside the same :-
docker container run -v /Users/B0218162/Documents/LEARNINGS/MEDIUM-BLOG/Docker/DOCKER_LEARN/DATA_DIR_FOR_CONTAINER:/var/lib/mysql -d -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=fleetman --network aditya-network --name mysqldbpanga mysql:5
Note that, the location “/Users/B0218162/Documents/LEARNINGS/MEDIUM-BLOG/Docker/DOCKER_LEARN/DATA_DIR_FOR_CONTAINER” shall be shared from the host-machine with the directory “/var/lib/mysql” inside the docker container.
Step #2.) Now, we access the mysql within our container and create the table inside the mysql , insert some data inside the same and exit from this container.
Step #3.) Let’s again launch the another container from the same image again :-
Note that, the container-id this time is changed now, which means that, this is yet another fresh container.
Step #4.) Let’s verify that, whether the table+data exists in this container or not ?
And yes, the data exists very well within this new container as well.
Step #5.) Let’s see the directory, which was shared from our host machine :-
Yeah, it’s clear that, we can’t make out anything from above data files, but these are something, which is used under the hoods to hold the data of our database running inside the docker container.
Question :- So far, we had been manually generating the Maven Image from the DockerFile. Can we use Maven, in order to automatically generate the DockerImage too ?
Answer:- Yes, it’s very much possible to use maven, in order to generate the docker-image directly, post the build process is done.
Step #1.) First, we use the maven plugin “fabric8” in pom.xml :-
<!--copies artficact to docker build dir in target-->
Step #2.) Second, we create a RUN-Configuration into our IntelliJ IDE :-
Observe that, we intend to perform 3 actions together i.e. “clean”, “package” & “docker:build”.
Step #3.) Now, let’s execute the aforesaid maven configuration:-
Step #4.) Let’s verify that, whether new docker-image have been generated or not :-
Question :- Can we have a sort of Virtual Private Networks with Dockers ?
Answer :- Yes, very well. The docker networking is designed to make docker-networks, so as multiple containers can talk to each other in expected fashion.
That’s all in this section. If you liked reading this blog, kindly do press on clap button multiple times, to indicate your appreciation. We would see you in next series.