Docker for devs with hands-on| Part-5

  • 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.
FROM openjdk:8u312-jdk
MAINTAINER Aditya Goel "adityagoel123@gmail.com"
EXPOSE 8080
WORKDIR /usr/local/bin/
COPY fleetman-0.0.1-SNAPSHOT.jar webapp.jar
CMD ["java", "-Dspring.profiles.active=docker-demo", "-jar", "webapp.jar"]
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.
docker images
docker container run -itd -p 8089:8080 <NAME_OF_DOCKER_IMAGE>
docker container exec -it d1 bash
docker containers ps
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.
docker images
docker container ps -a
docker container exec -it e7 bash
docker network ls
  • This Bridge Network will allow us Outbound Traffic.
  • But unfortunately, the bridge network does not allow container to container networking.
docker network create <NAME_OF_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.
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.
docker container exec --it 3b bash
apt-get update && apt-get install iputils-ping
ping <NAME_OF_OTHER_CONTAINER>
FROM openjdk:8u312-jdk
MAINTAINER Aditya Goel "adityagoel123@gmail.com"
EXPOSE 8080
WORKDIR /usr/local/bin/
COPY fleetman.jar webapp.jar
CMD ["java", "-Dspring.profiles.active=docker-demo", "-jar", "webapp.jar"]
docker image build -t fleetmanjarimage5 .
docker images
docker container run -itd -p 8085:8080 --network aditya-network --name fleetmantrackercontainer4 fleetmanjarimage5
  • 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.
docker volume ls
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.
docker volume inspect <NAME_OF_VOLUME>
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
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.21.0</version>

<configuration>
<dockerHost>unix:///var/run/docker.sock</dockerHost>
<verbose>true</verbose>
<images>
<image>
<name>adityagoel123/fleetman-web-with-mysql</name>
<build>

<dockerFileDir>${project.basedir}/src/main/docker/</dockerFileDir>

<!--copies artficact to docker build dir in target-->
<assembly>
<descriptorRef>artifact</descriptorRef>
</assembly>
<tags>
<tag>latest</tag>
<tag>${project.version}</tag>
</tags>
</build>
</image>
</images>
</configuration>
</plugin>

--

--

--

Software Engineer for Big Data distributed systems

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

ITS A BIG DEEP THANK YOU AGILE VENTURES

Creating An App Using Laravel WebSockets and NextJs (PART 1)

Industry use cases of Jenkins

Do your missions

Keep your points, even when loading a new scene in Unity.

To understand the depth of the program/software

Syntax… and Errors!

Python with JavaScript : Code style differences

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
aditya goel

aditya goel

Software Engineer for Big Data distributed systems

More from Medium

Installing Docker in docke container running Ubuntu 20.04

Reverse Proxy Feature of NGINX Server

Connect to Docker Service using hostname in Docker Swarm

Getting Started with Docker