======= Docker ======= => Containerization software => Using Docker we can run our application in any machine. => Docker will make our application portable. ==================== Life without Docker ==================== => We need to install softwares in computer to run our application Ex: Ng 18v, Java 17v, MySQL 8.5v => To handle load, we will deploy our application in multiple machines. => If We want to deploy our application in 100 machines then we need to install required softwares in all 100 machines. Note: In future if we want upgrade our project from Java 17 to Java 21 then we need to un-install java 17 and install java 21 in 100 machines. IT is not easy task. => To overcome this problem we will use Docker. ================= Life with Docker ================= => If we use Docker it will take care of softwares installation required to run our application. => We will execute our application as a Docker container. Container = Virtual Machine (linux vm) => Docker works based on Virtualization concept. =========================== What is Containerization ? ============================ The process of packaging our "application code + application dependencies (softwares)" as single unit for execution is called as Containerization. ====================== Docker Architecture ? ====================== Dockerfile : It contains instructions to build image Docker Image : package of code + dependencies Docker Registry : Used to store docker images Docker Container : Virtual machine in which application will be deployed. ======================= Docker Setup in Linux ======================= https://github.com/ashokitschool/DevOps-Documents/blob/main/02-Docker-Setup.md ================ Docker Commands ================ docker images : display docker images available in our machine. docker pull : download docker image docker pull docker run : run docker image (create container) docker run docker ps : display running docker containers docker ps # display stopped containers docker ps -a docker stop : Stop docker container docker stop docker start : Start docker container docker start docker rm : To remove stopped docker container docker rm docker rmi : Remove docker image docker rmi Note: To remove all stopped containers and un-used docker images we can use below command docker system prune -a ======================================================= Running Real-world applications using docker images ======================================================= docker pull ashokit/spring-boot-rest-api docker run ashokit/spring-boot-rest-api docker run -d ashokit/spring-boot-rest-api docker ps docker logs docker run -d -p host-port:container-port ashokit/spring-boot-rest-api Ex: docker run -d -p 9090:9090 ashokit/spring-boot-rest-api ########### Java App URL : http://public-ip:host-port/welcome/{name} docker pull ashokit/python-flask-app docker run -d ashokit/python-flask-app docker run -d -p 5000:5000 ashokit/python-flask-app ########### Python App URL : http://public-ip:host-port/ Note: Here -d represents detached mode. Note: Here -p represents port mapping. (host-port:container-port) Note: host port and container port no need to be same. Note: Host port number we need to enable in ec2-vm security group inbound rules to allow the traffic. ============= Dockerfile ============= => Dockerfile contains set of instructions to build docker image. filename : Dockerfile => Dockerfile contains set of instructions to build docker image. => 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 (dependent software) for our application Ex : FROM tomcat:9.0 FROM openjdk:17 FROM python:3.3 FROM node:19 FROM mysql:8.5 ============ MAINTAINER ============ => To specify author of Dockerfile (who created/modifed Dockerfile) Ex: MAINTAINER Ashok Note: It is optional. ======== RUN ======== => RUN keyword is used to specify instructions (commands) which are required 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. Note: Using CMD we can run application in container Ex : CMD "java -jar sbapp.jar" CMD "python app.py" Note: If we write multiple CMD instructions in dockerfile, docker will execute only last CMD instruction. =========== ENTRYPOINT =========== > It is used to execute instruction when container is getting created. Note: ENTRYPOINT is used as alternate for 'CMD' instructions. Ex-1 : CMD "java -jar app.jar" Ex-2 : ENTRYPOINT ["java", "-jar", "app.jar"] ===== 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/ ===== ADD ===== => ADD instruction is used to copy files from source to destination. COPY target/app.jar /usr/app/ ADD /usr/app/ ========= WORKDIR ========= => WORKDIR instruction is used to set / change working directory in container machine. Ex : COPY target/app.jar /usr/app/ WORKDIR /usr/app/ CMD "java -jar app.jar" ======= EXPOSE ======= => EXPOSE instruction is used to specify application is running on which PORT number. Ex : EXPOSE 9090 Note: It is optional. ========================================= How to push docker image into docker hub ========================================= # login into docker hub account docker login # push docker image docker push =================================== Dockerizing SpringBoot application =================================== => Spring Boot is a java framework which is used to develop java based applications. => Spring Boot applications will be packaged as a jar file. We need java software to run spring boot application as jar file. => To run the jar file we will use below command Ex: java -jar Note: When we run springboot application jar file, internally springboot will use tomcat server as "embedded container" with default port number 8080. # App Git Repo : https://github.com/ashokitschool/spring-boot-docker-app.git ================ Java SpringBoot App Dockerfile =========== FROM openjdk:17 COPY target/sb-app.jar /usr/app/ WORKDIR /usr/app/ EXPOSE 8080 ENTRYPOINT ["java", "-jar", "sb-app.jar"] ============================================================ 1) Clone git repo in docker host machine (linux) git clone https://github.com/ashokitschool/spring-boot-docker-app.git 2) Go inside project directory and perform maven build cd spring-boot-docker-app mvn clean package 3) Create docker image docker build -t ashokit/sb-app . docker images 4) Create docker container docker run -d -p 8080:8080 --name ashokit ashokit/sb-app docker ps docker logs 5) Access application URL in browser Linux : http://public-ip:8080/ ==================================================== Dockerizing Java Web application (no springboot) ==================================================== ## App Git Repo : https://github.com/ashokitschool/maven-web-app.git => Normal java web apps will be packaged as war file. Note: war file will be created inside project target directory. => To execute that java web application we need to deploy that war file in tomcat server. => Inside tomcat server we will have "webapps" folder. It is called as deployment folder. => To run war file we need to keep war file inside tomcat/webapps folder. ================ Dockerfile for Java web application =============== FROM tomcat:latest EXPOSE 8080 COPY target/maven-web-app.war /usr/local/tomcat/webapps/ ==================================================================== ================================= Dockerizing Angular application ================================= => Angular is a frontend framework => Angular is used to develop frontend of the applications Note: Angular developed by google company. => We will use Node Package Manager( NPM ) to build angular applications Note: When we build angular app it will generate "dist" folder for deployment => To run angular app we can copy "dist" folder data to "Nginx" server. -------------------------------- FROM node:18 AS build WORKDIR /app COPY package*.json ./ RUN npm install COPY . . RUN npm run build --prod FROM nginx:alpine COPY --from=build /app/dist/angular_docker_app /usr/share/nginx/html EXPOSE 80 ----------------------------- git clone https://github.com/ashokitschool/angular_docker_app.git cd angular_docker_app docker build -t ngapp . docker images docker run -d -p 80:80 ngapp Note: Enable 80 port number in security group inbound rules. => Access application in browser using docker-server public ip address URL : http://public-ip/ ============================================================================ Docker Compose Tutorial : https://youtu.be/7FBsiR7-e3Q?si=rcbHZ7dKIpZPajk2 ============================================================================