DOCKER Notes from Jay Likhare (Github: @JayLikhare316) #Q.What is Docker? Why we need docker? A. Docker is a containerization platform that allows developers to package, ship, and run applications in containers. Containers are lightweight and portable, making it easier to develop, test, and deploy applications. #Q.What are containers? A. Containers are a type of virtualization that allows multiple applications to run on a single host operating system, each with its own isolated environment. Containers share the host operating system, but each container has its own file system, network stack, and process space. #Q.Virtualizer vs hypervisor? A. Hypervisor is a type of virtualization that creates a separate operating system for each virtual machin (HVM), whereas virtualizer is a type of virtualization that runs on top of a host operating system and shares the host operating system with the guest operating system (non-HVM). #Q.Monolythic app vs Microservice app? A. Monolythic app is a single application that performs multiple functions, whereas microservice app is a collection of small, independent services that work together to perform a specific function. #Q.What is port forwarding? A. Port forwarding is a technique used in networking to forward incoming traffic from one port to another port. It allows a service running on a specific port on a host to be accessed from outside the host. #install docker in linux. enable & start docker sudo systemctl enable docker sudo systemctl start docker sudo ststemctl status docker sudo docker run -d -p instance-port:container port container name or container ID (-d for detached -p for port forwarding -P for random port forwording) sudo docker images sudo docker ps -a --> to see all stopped containers sudo docker ps sudo docker run -itd amazonlinux bash sudo docker exec -it /bin/bash --> to go inside the docker shell cd usr/share/nginx.html/ rm index.html echo "hello world" > index.html sudo docker inspect --> to inspect docker container sudo docker logs --> to get logs of the container sudo docker stop --> to stop the container sudo docker start --> to start the container sudo docker rm --> to remove the container sudo docker rmi --> to remove the image sudo docker rmi -f --> to remove the image forcefully sudo docker commit sudo docker image --> to see all images for docker sudo docker tag --> to tag the image #login into the docker account in linux sudo docker login --> to login into the docker sudo docker push --> sudo docker run --name <> -d -P sudo docker tag demo (username)/(reponame) sudo docker push (username)/(reponame):latest #if I want to create my private image you can use ECR you have to create a ec2 role for ecr copy login command aws ecr get-login-password --region --profile | docker login --username AWS sudo docker tag .dkr.ecr..amazonaws sudo docker push .dkr.ecr..amazonaws/ sudo docker pull .dkr.ecr..amazonaws/ task:- task1) # get the docker image of amazon linux sudo docker pull amazonlinux sudo docker exec run -itd -p 80:8080 amazonlinux # add tomcat package # add student ui # then commit the image -->container se image banana # store on ecr as well as docker hub task2) # get the docker image of amazon linux # install nginx # add free css task3) # host mysql container # -e (enviornment variables) # use msql user/mysql password 30 dec 2024-- task4) #make a deamon service of tomcat. #docker container storage #persistane # docker config files etc/docker docker volumes:- docker create volume --> to create docker volume (universal docker storage) docker volume ls --> to list volumes cd var/lib/docker --> root directory of docker /var/run/docker.sock --> docker socket file # Q.what is socket file? # A.socket file is a file that allows two processes to communicate with each other. sudo docker run -d -v dev:usr/share/nginx/html -P nginx --> to add volume to the container sudo -v for volume dev for volume name :path to mount volume --name --> for adding name to container # DOCKERFILE:- # dockerfile is a text file that contains instructions for building a docker image. # docker file is a step by step instruction to our docker image. # dockerfile commands:- 1. FROM:- it is used to specify the base image for our image. 2. RUN:- it is used to run a command in the container. 3. COPY:- it is used to copy a file from the local machine to the container. 4. ADD:- it is used to copy a file from the local machine to the container. 5. WORKDIR:- it is used to set the working directory in the container. 6. EXPOSE:- it is used to specify the port that the container will listen on. 7. ENV:- it is used to set an environment variable in the container. 8. VOLUME:- it is used to specify a directory in the container that will be persisted ev 9. CMD:- it is used to specify the default command to run when the container is started. 10. ENTRYPOINT:- it is used to specify the default command to run when the container is started 11. USER:- it is used to specify the user that the container will run as. 12. ONBUILD:- it is used to specify a command that will be run when the image is 13. ARG:- it is used to specify a variable that can be passed to the dockerfile. 14. LABEL:- it is used to specify a label that can be used to identify the image. >>> demofile: FROM nginx COPY boxer-html /usr/share/nginx/html vim dockerfile --> paste above to dockerfile docker build -t dockerfile ## FROM amazonlinux RUN yum update && yum install -y nginx EXPOSE 80 CMD ["usr/sbin/nginx", "-g" , "daemon off;"] vim dockerfile --> paste above to dockerfile sudo docker build -t dummy-nginx sudo run -it dummy-nginx bash ## FROM amazonlinux RUN yum update && yum install java-17 -y COPY . . RUN chmod 777 apache-tomcat-10.1.34/bin/catalina.sh EXPOSE 8080 CMD ["apache-tomcat-10.1.34/bin/catalina.sh", "run"] ##task- Q.Difference between entrypoint and cmd? cmd is used to specify the default command to run when the container is started. entrypoint is used to specify the default command to run when the container is started. ## docker (tomcat with studentui) FROM amazonlinux RUN yum update && yum install -y java-17 && yum install maven -y COPY tomcat /tomcat/. RUN chmod 777 tomcat/bin/catalina.sh EXPOSE 8080 COPY student-ui /student-ui/. WORKDIR /student-ui RUN mvn clean package RUN mv target/*.war /tomcat/webapps/student.war CMD ["/tomcat/bin/catalina.sh", "run"] ## #multistage docker file:(from docker container {dockerhub}) FROM maven:3.9.9-amazoncorrectto-debian-bookwarm AS builder --> #maven ko builder ka naam de diya COPY student-ui RUN mvn clean package FROM tomcat:jre8-alpine COPY --from=builder /target/*.war webapps/student.war what is multistage dockerfile? multistage dockerfile is a feature of docker that allows you to build multiple images from a single dockerfile. This is useful when you want to create a smaller final image, but you need to perform some operations in a larger image. ## Docker 3D architecture:- what is docker compose? docker compose is a tool for defining and running multi-container docker applications. with docker compose, you can define a docker application using a single configuration file, and then run it using a single command. sudo docker file name --> docker-compose.yml sudo docker-compose up --> to start the containers sudo docker-compose up -d --> to start the containers in detached mode i.e. in background sudo docker-compose down --> to stop the containers ## Docker Network (By default we use bridge network) Bridge network:- it is the default network in docker. It is used to connect containers to the host machine. Host network:- it is used to connect a container to the host machine's network. None network:- it is used to isolate a container from the host machine's network. macvlan network:- it is used to assign a mac address to a container. overlay network:- it is used to connect multiple docker hosts together. Q. 172.16.0.0/16 --> default ip used by docker.why? {because it is uncommon ip, nobody uses it, thats why} A. Docker uses the 172.16.0.0/16 ip range by default because it is a private ip range that is not routable on the internet. This means that containers running on a host can communicate with each other using these ip addresses, but they will not be accessible from outside the host. # Docker compose file:- version : '3' services: app: ##given name of project image: nginx ports: - "80:80" volumes: - myvolume:/usr/share/nginx/html/ ##givennameof volume : path to be mounted on web: image: httpd ports: - "8080:80" tomcat: image: tomcat:8.5-jdk8-alpine ports: - "8081:8080" volumes: myvolume: ##how to run docker-compose file docker-compose up -d ##-d means detached mode what is docker network ? # Types of network in docker ? why does docker use 172.16.0.0/16 ip ? aws vpc default network ip is 172.31.0.0/16