Softwares installation Tutorials : https://ashokit.in/yt-content/softwares-installation AWS Cloud Tutorials : https://ashokit.in/yt-content/cloud-tutorials Linux tutorials : https://ashokit.in/yt-content/linux-tutorials ===================================== Software Application Architecture ===================================== 1) Frontend : User Interface (UI) 2) Backend : Business logic 3) Database : Storage ============================= Tech Stack of Application ============================= Frontend : Angular 18v Backend : Java 17v Database : MySQL Webserver : Tomcat Note: If we want to run our application code, then we need to setup all required dependencies/softwares in the machine. Note: dependencies nothing but the softwares which are required to run our application ========================== Application Environments ========================== => In realtime we will use several environments to test our application. 1) DEV => 10 machines 2) SIT => 20 machines 3) UAT => 50 machines 4) PILOT => 100 machines 5) PROD (final delivery) => 200 machines => Dev env used by developers for code integration testing => SIT env used by Testers for system integration testing => UAT env used by client side team for acceptance testing (Go or No Go) => Pilot env used for pre-production testing => Prod env used for live deployment (end users can access our app) => As a devops enginner we are responsible to setup infrastructure to run our application => We need to install all required softwares (dependencies) to run our application Note: We need to setup dependencies in all environments to run our application. Note: There is a chance of doing mistakes in dependencies installation process (version compatability issues can occur) ===================== Life without Docker ===================== ### (1) "It works on my machine" problems - Developers would build and test apps on their laptops. - When they move the app to servers, it often breaks because of differences in OS, libraries, versions, configurations, etc. Without Docker, teams would spend hours or days debugging these environment differences. ### (2) Complex Software Installations - installing something like a database (e.g., PostgreSQL) required manually: - download right version of s/w - install dependencies - configure services properly ### (3) Heavy Virtual Machines (VMs) - Before Docker, developers used Virtual Machines like VirtualBox, VMware, or Hyper-V to simulate servers. - VMs are large, slow to start, and resource-hungry (each VM has a full OS inside). ### (4) Scaling Applications is Difficult - Manually configuring and copying app binaries. - Manually setting up load balancers. ================== What is Docker ? ================== => Docker is a free & open source software => Docker is used for containerization Container = package (app code + required softwares) => With the help of docker, we can run our application in any machine very easily. => Docker will take care of dependencies installation required for app execution. => We can make our application portable using Docker. ===================== Docker Architecture ===================== 1) Dockerfile 2) Docker Image 3) Docker Registry 4) Docker Container => Dockerfile is used to specify where is our app-code and what dependencies are required for our application execution. => Docker Image is a package which contains (app_code + dependencies) Note: Dockerfile is required to build docker image. => Docker Registry is used to store Docker Images. Note: When we run docker image then Docker container will be created. Docker container is a linux virtual machine. => Docker Container is used to run our application. ============================ Docker Setup in Linux VM ============================ Step-1 : Create EC2 VM (Amazon linux ami) Step-2 : Connect with that vm using ssh client (git bash) Step-3 : Execute below commands # Install Docker sudo yum update -y sudo yum install docker -y sudo service docker start # Add ec2-user user to docker group sudo usermod -aG docker ec2-user # Exit from terminal and Connect again exit # Verify Docker installation docker -v ================= Docker Commands ================= docker images : To display docker images available in our system docker ps : To display running docker containers docker ps -a : To display running + stopped containers docker pull : To download docker image from docker hub $ docker pull ashokit/spring-boot-rest-api docker run : To create/run docker container $ docker run hello-world docker rm : To delete docker container docker stop : To stop running docker container docker start : To start docker container which is in stopped state docker rmi : To delete docker image docker logs : To display container logs # delete stopped containers + unused images + build cache docker system prune -a ======================================================= Running Real-world applications using docker images ======================================================= ### public docker image name (java springboot app) : ashokit/spring-boot-rest-api docker pull ashokit/spring-boot-rest-api docker run ashokit/spring-boot-rest-api docker run -d ashokit/spring-boot-rest-api Syntax : docker run -d -p : ashokit/spring-boot-rest-api docker run -d -p 9090:9090 ashokit/spring-boot-rest-api Note: To access application running in the container we will use below URL ## Java App URL : http://host-public-ip:host-port/welcome/{name} Note: Host port number we need to enable in ec2-vm security group inbound rules to allow the traffic. ### public docker image name (python app) : ashokit/python-flask-app docker pull ashokit/python-flask-app docker run -d -p 5000:5000 ashokit/python-flask-app ## Python App URL : http://host-public-ip:host-port/ Note: Host port number we need to enable in ec2-vm security group inbound rules to allow the traffic. ======================== What is Port Mapping ? ======================== Note: By default, services running inside a Docker container are isolated and not accessible from outside. => Docker port mapping is the process of linking container port to host machine port. => It is used to allow external access to applications running inside the container. Syntax : docker run -p : image_name Note: host port and container port no need to be same. ============= Dockerfile ============= => Dockerfile contains set of instructions to build docker image. Filename : Dockerfile => To write dockerfile we will use below keywords 1) FROM 2) MAINTAINER 3) RUN 4) CMD 5) COPY 6) ADD 7) WORKDIR 8) EXPOSE 9) ENTRYPOINT ==== FROM ===== => It is used to specify base image required to run our application. ex: FROM openjdk:17 FROM python:3.3 FROM tomcat:9.0 FROM mysql:8.5 FROM node:19.5 ============ MAINTAINER ============ => MAINTAINER is used to specify who is author of this Dockerfile. => This is Optional in Dockerfile. Ex : MAINTAINER Ashok ===== RUN ===== => RUN keyword is used to specify instructions (commands) to execute at the time of docker image creation. Ex : RUN 'git clone ' RUN 'mvn clean package' Note: We can specify multiple RUN instructions in Dockerfile and all those will execute in sequential manner. ======== CMD ======== => CMD keyword is used to specify instructions (commands) which are required to execute at the time of docker container creation. Ex: CMD 'java -jar app.jar' CMD 'python app.py' Note: If we write multiple CMD instructions in dockerfile, docker will execute only last CMD instruction. ===== COPY ===== => COPY instruction is used to copy the files from source to destination. Note: It is used to copy application code from host machine to container machine. Source : HOST Machine Destination : Container machine Ex : COPY target/app.jar /usr/app/ COPY target/app.war /usr/bin/tomcat/webapps/ COPY app.py /usr/app/ ===== ADD ===== => ADD instruction is used to copy the files from source to destination. Ex : ADD ADD ======== WORKDIR ======== => WORKDIR instruction is used to set / change working directory in container machine. ex: COPY target/sbapp.jar /usr/app/ WORKDIR /usr/app/ CMD 'java -jar sbapp.jar' ======== EXPOSE ======== => EXPOSE instruction is used to specify application is running on which PORT number. Ex : EXPOSE 8080 Note: By using EXPOSE keyword we can't change application port number. It is just to provide information the people who are reading our Dockerfile. =========== ENTRYPOINT =========== => It is used to execute instruction when container is getting created. Note: ENTRYPOINT is used as alternate for 'CMD' instructions. CMD "java -jar app.jar" ENTRYPOINT ["java", "-jar", "app.jar"] ================================================ What is the diff between 'CMD' & 'ENTRYPOINT' ? ================================================= => CMD instructions we can override while creating docker container. => ENTRYPOINT instructions we can't override. ================== Sample Dockerfile ================== FROM ubuntu MAINTAINER Ashok RUN echo 'hello from run instruction-1' RUN echo 'hello from run instruction-2' CMD echo 'hi from cmd-1' CMD echo 'hi from cmd-2' ------------------------- # create docker image using dockerfile $ docker build -t img-1 . # Run docker image to create docker container $ docker run img-1