Docker
Docker
Docker
- Frontend
- Backend
- Database
6) Docker Architecture
=======================
Docker Basic Commands
=======================
Note: when we use 'run' command it will check for image in local, if image not
available then it will pull the image and it will run the image
===================
Docker Terminology
=====================
Dockerfile : It contains set of instructions to build docker image
============
Dockerfile
============
FROM
MAINTAINER
COPY
ADD
RUN
CMD
ENTRYPOINT
ENV
LABEL
USER
WORKDIR
EXPOSE
VOLUME
========
FROM
========
Syntax:
FROM <image-name>
Example:
FROM java:jdk-1.8.0
FROM tomcat:9.5
FROM mysql
FROM python
=============
MAINTAINER
==============
Example:
========
COPY
========
-> It is used to copy files / folders to docker image from our system while
creating image
Syntax:
Example:
========
ADD
========
-> It is also used to copy the files from one location to another location
Ex:
-> ADD can work with source location & URL also
=======
RUN
=======
-> We can write multiple RUN instructions in docker file, Docker will process all
RUN instructions
-> When we have multiple RUN instructions they will execute from top to bottom
Example:
=========
CMD
=========
-> Technically we can write multiple CMD instructions in dockerfile but Docker will
process only last CMD instruction
(There is no use of writing multiple CMD instructions in Dockerfile)
FROM
MAINTAINER
COPY
ADD
RUN
CMD
==============Dockerfile===============
FROM ubuntu
==================================
# Building Docker image using Dockerfile
$ docker build -t <image-name> .
================
ENTRYPOINT
================
Example
------------
==============
WORKDIR
==============
Ex:
WORKDIR <DIRNAME>
===========
ENV
===========
eX:
========
LABEL
========
Ex:
ex:
WORKDIR /home/username/app/
============
EXPOSE
============
-> Expose keyword represents on which port number our application container running
Ex:
EXPOSE 8080
==========
USR
==========
EX:
USR root
===========
ARG
==========
Ex:
ARG branch
RUN sh 'git clone -b $branch <repo-url>'
===============
VOLUME
===============
FROM
MAINTAINER
COPY
ADD
RUN
CMD
ENTRYPOINT
USR
ARG
LABEL
EXPOSE
WORKDIR
ENV
VOLUME
=========================
Java Web application Types
=========================
-> When we develop Java web application without Spring Boot then we need an
external Server to run our java web application (Ex: Apache Tomcat).
-> Normal java web apps will be packaged as war file (web archieve)
-> When we develop Java web application with Spring Boot then we no need worry
about server because Spring Boot providing embedded server, it will take care of
web application execution.
-> Spring Boot applications will be packaged as jar file (java archieve)
=================================
Dockerize Normal Java Web Application
=================================
Application Code : Java web application with Maven Build Tool (Git Hub Repo)
---------------------------------------
Dockerfile---------------------------------------------------------------
FROM tomcat:10.0.26-jre8
$ cd <project-folder>
URL : http://13.235.243.1:8080/maven-web-app/
==============================
Dockerize Spring Boot Application
===============================
-> Spring Boot is a java framework which is used to develop java based applications
-> Spring Boot will provide embedded server to run java web application (no need to
configure server manually)
---------------------------------Dockerfile---------------------------------
FROM openjdk:11
COPY target/spring-boot-docker-app.jar /usr/app/
WORKDIR /usr/app/
-----------------------------------------------------------------------------------
--
=================================
Dockerizing Python Flask Application
=================================
FROM python:3.6
COPY . /app
WORKDIR /app
================================
3) Why Docker: Free, Easily we can package, build and run our applications in any
machine
4) Docker Architecture:
build run
Dockerfile -----------> Docker Image ------------> Docker
Container
5) Docker Terminology
- Dockerfile
- Docker Image
- Docker Hub
- Docker Container
FROM
MAINTAINER
COPY
ADD
RUN
CMD
ENTRYPOINT
EXPOSE
WORKDIR
USR
ENV
LABEL
ARG
VOLUME
====================================
Docker Commands we have used so far
====================================
$ docker info
$ docker images
$ docker rmi <image-id>
$ docker pull <image-id>
$ docker run <image-id>
$ docker tag <image-name> <image-tag-name>
$ docker login
$ docker push <image-tag-name>
$ docker run -d -p host-port:container-port <image-name>
$ docker ps
$ docker ps -a
$ docker stop <container-id>
$ docker rm <container-id>
$ docker rm -f <container-id>
$ docker rm -f $(docker ps -a -q)
$ docker system prune -a
$ docker logs -f <container-id>
$ docker exec -it <container-id> bash
=================
Docker Network
=================
-> Docker network is used to provide isolated network for Docker Containers
1) none
2) host
3) bridge
-> Bridge driver is recommended we are running standalone container. It will assign
one IP for container
-> Host Driver is also used for standalone container. IP will not be assigned for
container
-> Overlay network driver is used for Orchestration. Docker Swarm will use this
Overlay network driver
-> Macvlan driver will assign MAC address for a container. It makes our container
as Physical.
======================
Monolith Vs Microservices
======================
-> Monolith means single application will be available for all the functionalities
1) Products_Api
2) Cart_Api
3) Payment_Api
4) Orders_Api
5) Tracking_Api
6) Cancel_Api
7) Admin_Api
8) Reports_Api
9) Usermanament_api
=> Running Multiple containers manully for all the apis is difficult job
=> Docker Compose is a tool which is used to manage multi container based
applications
=> Using Docker Compose we can easily setup & deploy multi container based
applications
=> We will give containers information to Docker Compose using YML file (docker-
compose.yml)
=> Docker Compose YML should have all the information related to containers
creation
======================
Docker Compose YML File
======================
version:
services:
network:
volumes:
=====================
=====================
Docker Compose Setup
=====================
# Give permission
$ sudo chmod +x /usr/local/bin/docker-compose
=======================================
Spring Boot with MySQL using Docker Compose
=======================================
---
version: "3"
services:
application:
image: springboot-app
networks:
- springboot-db-net
ports:
- "8080:8080"
depends_on:
- mysqldb
mysqldb:
image: mysql:5.7
networks:
- springboot-db-net
environment:
- MYSQL_ROOT_PASSWORD: root
- MYSQL_DATABASE: sbms
networks:
- springboot-db-net:
...
$ docker-compose up -d
$ docker-compose ps
========================
Docker Volumes
========================
-> Once container removed then we will loose the data that stored in the container
-> In realtime we shouldn't loose the data even the container got removed
-> Application will store data in database, even if we delete application container
or db container data should be available.
-> To make sure data is available after the container is deleted we will use Docker
Volumes concept
=> Volumes are the preferred mechanism for persisting data generated by and used by
Docker containers.
-> The volumes which are created but not associated to any container are called as
Dangling Volumes
volumes:
app_data:
-----------------------------------------------------------------------------------
-----