Docker Notes java microservices
Docker Notes java microservices
+++++++++++++++++++++
-> In order to deploy our application in a machine we need to setup all the
Softwares which are required to our application
Ex: OS, Java 1.8v, MYSQL DB, Tomcat Web Server 9.0v etc.....
-> In Realtime project should be deployed into multiple environments for testing
purpose
-> SIT env will be used by Testing team to test functionality of the application
-> UAT env will be used by Client to test functionality of the application
-> PROD means live environment (It is used to deliver the project)
-> To deploy application to these many enivornments we need to take of all the
softwars required to run our application in all environments. It is very difficult
task.
Virtualization
+++++++++++
-> Installing Multiple Guest Operating Systems in one Host Operating System
-> We need to install all the required softwares in HOST OS to run our application
-> It is used to package all the softwares and application code in one container
for execution
-> Container will take care of everything which is required to run our application
-> Using Docker image we can create docker container and we can execute it
Conclusion
+++++++++++
-> Docker will take care of application and application dependencies for execution
-> Deployments into multiple environments will become easy if we use Docker
containers concept
$ docker info
++++++++++++++++++++Docker Commands+++++++++++++++++++
Dockerfile
++++++++++++
Dockerfile is file which contains instructions to create an image. Which contains
Docker Domain Specific Key Words to build image.
DockerImage
++++++++++++
DockerContainer
+++++++++++++++++++++
Run time instance of an image.If you run docker image container will be created
that's where our application(process) is running.
DockerRepo/Registry
++++++++++++++++++++
We can store and share the docker images.
Public Repo
++++++++++++++
Docker hub is a public reposotiry. Which contains all the open source softwares as
a docker images. We can think of docker hub as play store for docker images.
Private Repo
+++++++++++++++++++
(Nexus,JFrog,D.T.R(Docker Trusted Registory)),
AWS ECR
++++++++++++
We can store and share the docker images with in our company network using private
repo
Docker Enigine/Daemon/Host
++++++++++++++++++++++++++++++
It's a software or program using which we can create images & contianers.
Docker CE
Docker CE will not be supported by Redhat.
Docker EE
Docker EE will be support most of the os including redhat.
++++++++++++++++++++
What is docker hub?
+++++++++++++++++++++
It's a public repository for docker images. You can think as play store for
docker images.
$ vi Dockerfile
FROM ubuntu
RUN echo "Run One Updated"
RUN echo "RUN TWO"
CMD echo "Echo From Image"
CMD echo "Echo From Latest"
RUN echo "RUN Three"
++++++++++++++++++++++
Dockerfile
+++++++++++++++++++++
-> Docker engine will process Dockerfile instructions from top to bottom
FROM
MAINTAINER
COPY
ADD
RUN
CMD
ENTRYPOINT
ENV
LABEL
USER
WORKDIR
EXPOSE
VOLUME
FROM
++++++++
FROM : It indicates base image to run our application. On top of base image we will
create our own image
Example :
FROM java:jdk-1.8.0
FROM tomcat:9.2
FROM mysql
MAINTAINER
+++++++++++
-> It represents who is author or Dockerfile
COPY
+++++
-> It is used to copy files / folders to image while creating an image
Example :
ADD
++++++
-> ADD is also used to copy files to image while creating an image
-> ADD keyword can download files from remote location (http)
-> ADD keyword will extract tar file while copying to imgae
RUN
+++++
-> We can write multiple RUN instructions, they will execute in the order (from top
to bottom)
Example :
CMD
++++
Example :
-> We can write multiple CMD instructions in Dockerfile but Docker will process
only last CMD instruction.
Sample Dockerfile
++++++++++++++++
FROM ubuntu
MAINTAINER Ashok IT
# Run image
$ docker run imageone
ENTRYPOINT
+++++++++++
-> ENTRYPOINT instructions will execute while creating container
Example"
WORKDIR
+++++++++
-> It is used to set Working Directory for an image / container
Note: The Dockerfile instructions which are available after WORKDIR those those
instructions will be proess from given working directory
ENV
++++
LABEL
++++++
ARG
++++++
Ex:
ARG branch=develop
LABEL branch $branch
USER
++++++
-> We can set user for an image / container
Note: After USER instruction, remaining instructions will be processed with given
USER
EXPOSE
++++++++
-> It represents on which port number our container is running
VOLUME
++++++++
-> It is used for data storage
+++++++++++++++++++++++++++++++++
Dockerizing Spring Boot Application
+++++++++++++++++++++++++++++++++
FROM java:8-jdk-alpine
WORKDIR /usr/app
ENTRYPOINT ["java","-jar","spring-boot-docker-app.jar"]
+++++++++++++++++++++++++++++++++
Dockerizing Java Web App
+++++++++++++++++++++++++++++++++
FROM tomcat:8.0.20-jre8
Note: After running the container access application using below URL
URL : http://ec2-vm-public-ip:8080/java-web-app/
+++++++++++++++++++++++++++++++++
Dockerizing Python Flask Application
+++++++++++++++++++++++++++++++++
FROM python:3.7
WORKDIR /opt/app
COPY . .
EXPOSE 5000
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++
Docker Volumes
++++++++++++++
-> Docker volumes are used to persist the data generated by Docker containers
-> Using Docker volume we can de-couple data storage from Docker container
-> Using Docker volume we can share the data among multiple Docker containers
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Running nginx image using docker container with container name as "webapp1"
$ docker run --name=webapp1 -d -p 80:80 nginx
Note: Access static website using EC2 VM public ip which is hosted by nginx
Note: Access static website using EC2 VM public ip which is hosted by nginx
(Modified content shud display here)
Note: Access static website using EC2 VM public ip which is hosted by nginx
(changes we made in first container will not reflect here)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++
Note: Access static website using EC2 VM public ip which is hosted by nginx
Note: Access static website using EC2 VM public ip which is hosted by nginx
# Run another docker container (modified data should reflect here also)
$ docker run -d --name=webapp21 --mount
source=new_vol,destination=/usr/share/nginx/html -p 80:80 nginx
+++++++++++++++++++++++++++++++++++++++
Host Directory Mounting to Docker Container
+++++++++++++++++++++++++++++++++++++++
# Run Docker container for nginx image with directory mouting (container name is
C1)
$ docker run -d -p 80:80 -v /tmp/nginx/html:/usr/share/nginx/html --name c1
nginx:latest
+++++++++++++++++++++++
Docker Compose
++++++++++++++++++++++
-> Running multiple containers manually for all apis is difficult job
-> Using Docker compose we can define & deploy multi container based applications
easily
-> We will give input to docker compose tool using YAML file to run multiple
containers
-> Docker Compose YML file should have information realted to all our service
version:
services:
network:
volumes:
+++++++++++++++++++++++
Docker Compose Setup
+++++++++++++++++++++
# Give permission
$ sudo chmod +x /usr/local/bin/docker-compose
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++
# Create a docker compose file for setting up dev environment. Mysql container is
linked with wordpress container.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++
$ vim docker-compose.yml
---
services:
mydb:
environment:
MYSQL_ROOT_PASSWORD: ashokit
image: "mysql:5"
mysite:
image: wordpress
links:
- "mydb:mysql"
ports:
- "9090:80"
version: "3"
+++++++++++++++++++++++++++++++++++++++++++++
Spring Boot App with MSQL DB using Docker Compose
+++++++++++++++++++++++++++++++++++++++++++++
Dockerfile
----------------
FROM openjdk:jdk1.8
WORKDIR /usr/local/
EXPOSE 8080
ENTRYPOINT [ "java" , "-jar", "sb-app.jar" ]
-> If we are using MySQL DB in spring boot application then we need to MySQL DB in
a container
Note: Instead of managing two containers seperatley we can use Docker Compose.
version: "3"
services:
boot-app:
image: sb-rest-api
ports:
- "8080" : 8080
depends_on:
- mysql-db
mysql-db:
image: mysql:8
environment:
- MYSQL_ROOT_PASSWORD = ashokit
- MYSQL_DATABASE = bootdb
++++++++++++++++
Docker Network
++++++++++++++++
-> Docker Networking enables a user to link a docker container to as many networks
as they require
-> Docker network is used to provide complete isolation for Docker containers
1) They share single operating system and maintains containers in isolated manner
-> When Docker s/w is installed in a machine by default 3 docker networks will be
configured
1) none
2) host
3) bridge
Note: One container we can attach to multiple networks
-> When container is attached to multiple networks then those containers can
communicate
1) Bridge
2) Host
3) None
4) Overlay
5) Macvlan
Bridge Driver
+++++++++++++
Bridge : This the default network driver created on the Docker host machine
Note: Bridge network drivers are very useful when application running in standalone
container
# we can see more details about bridge driver using below command
Host Driver
++++++++++
-> It is useful when standalone container is available
-> The container will not get any IP address when we enable Host Driver
Note: For example a container is executed that binds to port 80 with Host Network
Driver. In this case we no need to map container port to host machine port.
-> This is useful when we are running our containers with large no.of ports
++++++++++
None Driver
+++++++++++
-> In this type of network, the containers will have no access to network
++++++++++++++++
Overlay Driver
+++++++++++++++++
-> We will use Docker Swarm to orchestrate our Docker containers
-> Overlay Driver will be used when we have Docker Swarm cluster
++++++++++++
Macvlan Driver
++++++++++++
-> Using this MAC address Docker engine routes the traffic to a particular route
+++++++++++++++++++++++++++++++++++++++
Working with Docker Networking Commands
+++++++++++++++++++++++++++++++++++++++
Note: If we don't specify driver then by default it will take bridge driver
# Run the docker container using custom network which we have created
$ docker run --name nginx20 -d --network my-network -p 7070:80 nginx
# Connect to nginx30 container and ping nginx31 container IP using ping command
++++++++++++++++++++++++++++++++++++++++
Running Containers Using Host Network Driver
+++++++++++++++++++++++++++++++++++++++
-> When we use Host Network driver port mapping is not required
Note: If you observer we are running nginx container without port mapping because
we are using Host Network Driver
+++++++++++++++++++++++++++++++++++++++++++
Running Containers Using Macvlan Network Driver
+++++++++++++++++++++++++++++++++++++++++++
Macvlan networks are special virtual networks that allow you to create “clones” of
the physical network interface attached to your Linux servers and attach containers
directly your LAN. To ensure this happens, simple designate a physical network
interface on your server to a macvlan network which has its own subnet and gateway.
$ ifconfig
inet : 172.31.13.126
Subnet : 172.31.13.0/24
Gateway : 172.31.13.1
+++++++++++++++
Docker Volumes
++++++++++++++
-> Volumes are the preferred mechanism for persisting data generated by and used by
Docker containers
Use of Volumes
++++++++++++++
1) Decoupling container from storage
$ docker volume ls
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// exec command
$ docker exec -it webApp bash
Let’s stop the container and start it again. we can still see the changes that we
made.
=> what if we stop this container and start another one and load the page. There is
no way that we could access the file that we have changed in another container.
$ docker volume ls
Once we run this command and ssh into docker volumes, we can see that volume
prepopulated with default welcome page from nginx location /usr/share/nginx/html
-> Let’s go and change the index.html from the new_vol location by ssh into the
docker.
// exec command
$ docker exec -it webApp bash
// cd to welcome page and edit it
$ cd /usr/share/nginx/html
$ echo "I changed this file while running the conatiner" > index.html
-> Let’s stop this container and start another one with the same command
$ docker stop webApp1
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-> Login into jenkins & Create one jenkins job in jenkins
-> Login into second jenkins and see (previosly created job will be displayed here)
+++++++++++++++
Docker Swarm
++++++++++++++
-> We will setup Master and Worker nodes using Docker Swarm cluster
-> Master will schedule the tasks (containers) and manage the nodes and node
failures
-> Worker nodes will perform the action (containers will run here)
Swarm Features
+++++++++++++++
1) Cluster Management
2) Decentralize design
3) Declarative service model
4) Scaling
5) Multi Host Network
6) Service Discovery
7) Load Balancing
8) Secure by default
9) Rolling Updates
Setup
+++++
1 - Master
2 - Nodes
Formula : (n-1)/2
If we take 2 servers
3-1/2 => 1 (it can be leader when the main leader is down)