└── Dockers-Psa-notes.txt /Dockers-Psa-notes.txt: -------------------------------------------------------------------------------- 1 | Docker 2 | _____________ 3 | 4 | -> Resolves Compatibility problem using containerization 5 | -> Docker will help us to install our software on different server without worrying about compatibility issue 6 | -> To deploy same application on multiple servers for load balancing we need not manually install technology stack (like Angular 18v, java 17V etc) manually one by one if docker is not used 7 | -> Software upgrades can be done easily on multiple machines using docker 8 | 9 | Note: 10 | ✔ Docker Engine acts as a bridge between the container and the host OS 11 | ✔ Containers don’t have their own kernel, they use the host’s kernel 12 | ✔ Docker Engine manages containers but does not create them—it runs & controls them 13 | 14 | 15 | 16 | Note: 17 | 18 | Linux VM--->Install Docker Engine--->Docker Engine acts as bridge between Linux Kernel and docker Container-->Provides isolated enviroment to run Docker Image. 19 | 20 | #################### 21 | # 22 | # docker container 23 | #################### 24 | # 25 | # dOCKER ENGINE 26 | #################### 27 | # 28 | # lINUX vm 29 | #################### 30 | 31 | Docker Architecture: 32 | ________________________________________________________________________ 33 | Docker File: Here we will provide Intructions to create docker image 34 | Docker Image: Application Package with dependencies of it 35 | Docker Registry: Its a hub to store docker images 36 | Docker Container: Isolated environment to run docker image 37 | 38 | What is containerization? 39 | 40 | containers package an application along with its dependencies (libraries, configuration files, etc.), ensuring that it runs consistently across different computing environments. 41 | 42 | ######################### ######################### #################### @@@@@@@@@@@@@@@@@ 43 | Dockerfile %%%%%%%% 44 | Container1 45 | (Contains instructions ------> Docker Image ------------> Docker hub/registry ----------> %%%%%%%%% 46 | to download dependencies (build) (Application Code + (Store) (Collection of images) Container2 47 | Dependencies to run that) %%%%%%%%%% 48 | ) 49 | ########################## ########################## ##################### @@@@@@@@@@@@@@@@@ 50 | 51 | Linux Server 52 | 53 | Installing Docker: 54 | _________________________ 55 | 56 | For Amazon Linux use the following commands 57 | 58 | Install Docker In Amazon Linux VM 59 | ____________________________ 60 | sudo yum update -y 61 | sudo yum install docker -y 62 | sudo service docker start 63 | sudo usermod -aG docker ec2-user 64 | exit 65 | _____________________________________ 66 | 67 | For Ubuntu use the following commands 68 | 69 | Install Docker In Ubuntu VM 70 | sudo apt update 71 | curl -fsSL get.docker.com | /bin/bash 72 | sudo usermod -aG docker ubuntu 73 | exit 74 | Verify docker installation 75 | 76 | Use this command to check version of docker installed 77 | _____________________________________________________ 78 | docker -v 79 | 80 | ############################################################################################## 81 | ############################################################################################## 82 | 83 | For practise pull the sample image from docker hub repository of pankaj sir academy: docker pull psait/pankajsiracademy:latest 84 | 85 | For Practise pull docker official image: docker pull hello-world 86 | 87 | ############################################################################################### 88 | ############################################################################################### 89 | 90 | 91 | Important Docker Commands 92 | _______________________________________________________ 93 | docker pull : download docker image from hub 94 | 95 | docker pull [image-name] 96 | 97 | docker run : run docker image - this will create container (Isolated Enviroment to run docker image- This is not a OSs) 98 | 99 | docker run [image-name / image-id] 100 | 101 | docker ps :To display docker containers that are running 102 | 103 | docker ps 104 | 105 | display stopped containers: 106 | 107 | docker ps -a 108 | 109 | docker stop :To Stop docker container 110 | 111 | docker stop [container-id] 112 | 113 | docker start : Start docker container 114 | 115 | docker start [container-id] 116 | 117 | docker rm : will remove stopped docker container 118 | 119 | docker rm [contianer-id] 120 | 121 | docker rmi : Will Remove docker image 122 | 123 | docker rmi [image-name / image-id] 124 | 125 | 126 | To remove all stopped containers and un-used docker images we can use below command 127 | 128 | docker system prune -a 129 | 130 | 131 | How to create Docker Image and run that to access from browser? 132 | ________________________________________________________________ 133 | 134 | run Docker Image from docker hub using the command 135 | 136 | Docker run [image-name] - This command when executed we will not be able to access our container in browser because it requires port mapping. 137 | 138 | As Container is running inside linux VM. We will have to map linux vm (host port) to container(Container port) this is called as port mapping. To do this perform the following 139 | 140 | Docker run -p host-port:container-port [image-name] 141 | 142 | Example: docker run -p 9090:9090 [image-name] 143 | 144 | Example: docker run -d -p 9090:9090 [image-name] will run the cintainer in background 145 | 146 | Now you can access our application using the url http://public-ip:host-post(linux-vm) 147 | 148 | Note 149 | 1. Enable Inbound rule in security group custom ip IPv4 anywhere with host port number. 150 | 2. If you run Multiple Containers in same linux vm then host port number should be different for every container 151 | ____________________________________________________________________________________ 152 | 153 | Install jekins using docker image name - docker run -d -p 8080:8080 jenkins/jenkins 154 | Note Enable Inbound rule in security group custom ip 8080 IPv4 anywhere 155 | 156 | _______________________________________________________________________________________ 157 | 158 | 159 | 160 | 161 | 162 | If Docker not used then we have to do the following to install jekins in linux vm 163 | 164 | ________________________________________________________________________________________ 165 | 166 | Step - 1 : Create Linux VM with Ubuntu, use t2.micro as jekins is heavy software & requires good configuration server 167 | 168 | Create Ubuntu VM using AWS EC2 (t2.medium) 169 | Enable 8080 Port Number in Security Group Inbound Rules 170 | Connect to VM using ssh cleint 171 | 172 | Step-2 : Install Jdk as jekins is java application and requires jdk to run jekins 173 | 174 | sudo apt update 175 | sudo apt install fontconfig openjdk-17-jre 176 | java -version 177 | 178 | Step-3 : Install Jenkins 179 | sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \ 180 | https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key 181 | echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \ 182 | https://pkg.jenkins.io/debian-stable binary/ | sudo tee \ 183 | /etc/apt/sources.list.d/jenkins.list > /dev/null 184 | sudo apt-get update 185 | sudo apt-get install jenkins 186 | 187 | Step-4 : Start Jenkins application using the commands given below 188 | sudo systemctl enable jenkins 189 | sudo systemctl start jenkins 190 | 191 | Step-5 : Verify Jenkins 192 | sudo systemctl status jenkins 193 | 194 | Step-6 : Open jenkins server in browser using VM public ip. Jekins runs on port 8080 by default 195 | http://public-ip-address:8080/ 196 | 197 | Step-7 : Copy jenkins admin pwd 198 | sudo cat /var/lib/jenkins/secrets/initialAdminPassword 199 | 200 | Step-8 : Create Admin Account & Install Required Plugins in Jenkins to start your CI/CD pipeline learning journey 201 | 202 | _____________________________________________________________________________________________________________________ 203 | 204 | Note Docker will simply our journey to get required softwares in server through single line - 205 | 206 | Example download Sonarqube using docker image. If not use linux command to do the same. 207 | 208 | docker run -d --name sonarqube -p 9000:9000 -p 9092:9092 sonarqube:lts-community 209 | 210 | 211 | Example download nexus using docker image, If not use linux command to do the same. 212 | 213 | docker run -d -p 8081:8081 --name nexus sonatype/nexus3 214 | _______________________________________________________________________________________________________________________ 215 | 216 | 217 | 218 | How to create docker file? Create Docker Image? push that to docker hub? 219 | ______________________________________________________________________________ 220 | 221 | In order to build a docker file as a developer you should know when to use the following keywords.Dockerfile instructions (FROM, CMD, ADD, etc.) are case-sensitive and must be written in uppercase. 222 | 223 | 1. FROM 224 | ___________ 225 | ->The FROM instruction in a Dockerfile specifies the base image for your container or it will be used to specify what softwares should be downloaded to run our app. It is always the first instruction in a Dockerfile because it defines the environment where your application will run 226 | 227 | FROM : 228 | 229 | → Name of the base image (e.g., ubuntu, node, python) 230 | → (Optional) Specifies the image version 231 | 232 | Example: 233 | 234 | FROM python:3.9 235 | FROM openjdk:17 236 | FROM tomcat:9.0 237 | 238 | 2. MAINTAINER 239 | ______________ 240 | -> The MAINTAINER instruction was used in older Docker versions to specify the author of the Dockerfile. 241 | Example of old version - MAINTAINER Your Name (This is deprecated) 242 | 243 | Example for new version - LABEL maintainer="Your Name " 244 | 245 | 3. RUN 246 | __________ 247 | -> The RUN instruction in a Dockerfile is used to execute commands during the image build process. 248 | 249 | Example : 250 | 251 | RUN 'git clone ' 252 | 253 | RUN 'mvn clean package' 254 | 255 | Note: If you specify multiple RUN instructions in Dockerfile, then those will execute in sequential manner. 256 | 257 | 4. CMD 258 | ____________ 259 | -> The CMD instruction in a Dockerfile specifies the default command that runs when the container starts 260 | 261 | CMD "java -jar myapp.jar" 262 | 263 | Note: when we write multiple CMD instructions in dockerfile, docker will execute only last CMD instruction only. 264 | 265 | 266 | Let us create our first docker file: 267 | ________________________________________ 268 | 269 | Step 1: vi dockerfile 270 | 271 | FROM openjdk:17 272 | MAINTAINER Pankaj 273 | RUN echo 'run-1' 274 | RUN echo 'run-2 275 | CMD echo 'cmd-1' 276 | CMD echo 'cmd-2' 277 | 278 | Step 2: create docker image using the command 279 | -> docker build -t pankajsiracademy/image1 . 280 | 281 | we have used dot (.) in above command to mention that dockerfile in present in same working directory 282 | 283 | Step 3: To check image created use the command 284 | --> docker image 285 | 286 | Step 4: to delete the image 287 | --> docker rmi image-id 288 | 289 | Step 5: You can again create the image 290 | --> -> docker build -t pankajsiracademy/image1 . 291 | 292 | Step 6: Run your docker image 293 | --> docker run [image-id] 294 | 295 | Note: 296 | 1. Only last CMD instruction will run 297 | 2. docker run [image-id] echo 'hello world', you will notice now hello world will execute and not the last CMD of docker file 298 | 299 | To over come the above said instruction we can use 300 | 301 | 5. ENTRYPOINT 302 | ________________ 303 | -> Alternative command for CMD but the advantage is we cannot override this command 304 | 305 | Example-1 : CMD "java -jar app.jar" 306 | 307 | Example-2 : ENTRYPOINT ["java", "-jar", "app.jar"] 308 | 309 | 6. COPY 310 | _________________ 311 | 312 | --> COPY instruction will copy the files from source to destination. 313 | --> It is used to copy application code from host machine to container machine. 314 | --> Here Source means "HOST Machine" and Destination means "Container machine" 315 | 316 | Example : COPY target/your-app.jar /usr/app/ 317 | 318 | 7. ADD 319 | _________________ 320 | 321 | --> ADD instruction will copy the files from source to destination same as COPY, But in addition it can extract your compressed tar file 322 | --> Here Source can be host machine 323 | --> ADD cannot download from HTTP/S3 URLs—this is a misconception. (Be carefull in interviews) 324 | 325 | Example: 326 | ADD target/app.jar /usr/app/ 327 | 328 | ADD /usr/app/ (Wrong) 329 | 330 | 8. WORKDIR 331 | __________________ 332 | 333 | --> The WORKDIR instruction sets the working directory (Like cd command ) 334 | --> If the directory does not exist, Docker will automatically create it 335 | 336 | Example: WORKDIR /path/to/directory 337 | 338 | 339 | COPY target/your-app.jar /usr/app/ 340 | WORKDIR /usr/app/ 341 | CMD "java -jar your-app.jar" 342 | 343 | 9. EXPOSE 344 | _____________________ 345 | 346 | -> EXPOSE command is used to specify application is running on which PORT number. Using this you cannot change the port number of application. You are just mentioning that our application is running on the port number 9090. 347 | 348 | -> If our application is running on port number 8081 you are mentioning expose 9090 then it is wrong. 349 | 350 | -> It is optional to use 351 | 352 | Example : EXPOSE 9090 353 | 354 | 355 | Now time for practicals guys, Let us do Dockerizing Spring boot application 356 | ___________________________________________________________________________ 357 | 358 | -> Spring Boot: framework which is used to develop enterprise based applications. 359 | -> Spring Boot applications will be packaged as a jar file for deployment. 360 | -> To run the jar file we will use command : java -jar 361 | -> To run springboot application jar file we will use tomcat server as "embedded server". 362 | -> By default spring boot application will run on port number 8080 363 | 364 | Step 1: Create Docker File 365 | 366 | FROM openjdk:17 367 | 368 | COPY target/demo-app.jar /usr/app/ 369 | 370 | WORKDIR /usr/app/ 371 | 372 | EXPOSE 8080 373 | 374 | ENTRYPOINT ["java", "-jar", "demo-app.jar"] 375 | 376 | 377 | Step 2: Note (Install maven and git in linux VM first) 378 | 379 | 1) Clone git repo in docker host machine (linux): git clone 380 | 381 | 2) Point to project root folder: cd and run to generate jar: mvn clean package 382 | 383 | 3) Create docker image and check that: 384 | -> docker build -t psait/pankajsiracademy: . 385 | ( 386 | Here 387 | -> psait is username 388 | -> repositoryname of docker hub 389 | -> can 390 | a. prod-v1 or prod-v2v2 for production 391 | b. dev-v1 for development environment 392 | c. test-v1 for testing environment 393 | d. staging-v1 for staging environment 394 | ) 395 | -> docker images 396 | 397 | 4) run docker container: docker run -d -p 8080:8080 --name psa psait/your-app 398 | 399 | 5)See the image is running or not: docker ps 400 | 401 | 6) docker logs 402 | 403 | 5) Access application URL in browser: http://public-ip:8080/ 404 | 405 | 406 | Steps to push docker image to docker hub 407 | __________________________________________ 408 | 409 | -> login into docker hub account from bash 410 | 411 | docker login 412 | Enter username: psait 413 | Enter password 414 | 415 | -> push docker image 416 | docker push psait/pankajsiracademy: 417 | 418 | --------------------------------------------------------------------------------