Sunday, March 5, 2023

DockerFile Session History

   649  mkdir /DockerFileDemo

  650  cd /DockerFileDemo/

  651  vi Dockerfile

  652  docker images

  653  docker ps

  654  docker stop 430c24476b5a 3dec2971eb8c d1f5432037e7

  655  docker ps

  656  docker ps -a

  657  docker system prune

  658  docker ps -a

  659  docker images

  660  docker images prune

  661  docker images prune --force

  662  docker system prune

  663  docker images

  664   docker system prune

  665   docker image prune

  666  docker image prune -a

  667  docker images

  668  docker ps

  669  vi Dockerfile

  670  docker build -t image1 .

  671  docker images

  672  docker run -it image1

  673  docker ps

  674  vi Dockerfile

  675  docker build -t image2 .

  676  echo "this file copied using copy instruction"> index.html

  677  docker build -t image2 .

  678  docker run -it image2

  679  vi Dockerfile

  680  docker build -t image3 .

  681  vi Dockerfile

  682  docker build -t image4 .

  683  docker images

  684  docker run -it  image4

  685  docker ps

  686  docker inspect e6150af8d7b9

  687  curl http://172.17.0.4:80

  688  curl http://172.17.0.4:80/index.html

  689  curl 172.17.0.4

  690  docker images

  691  vi Dockerfile

  692  cd ../DockerDemo/

  693  ls p-l

  694  ls -l

  695  cat Dockerfile

  696  cd ../DockerFileDemo/

  697  vi Dockerfile

  698  docker build -t image5 .

  699  docker run -d -n nginx_web image5

  700  docker run -d --n nginx_web image5

  701  docker run -d --name nginx_web image5

  702  docker ps

  703  dokcer attach f973ec8bf955

  704  dokcer exec -it f973ec8bf955

  705  docker attach f973ec8bf955

  706  docker ps

  707  docker run -d --name nginx_web1 image5

  708  docker ps

  709  docker inspect 8d454e0b04e0

  710  curl 8d454e0b04e0

  711  curl 172.17.0.5

  712  docker ps

  713  docker run -d --name nginx_web1 -p 81:80 image5

  714  docker run -d --name nginx_web3 -p 81:80 image5

  715  docker ps

  716  cat Dockerfile

  717  docker ps

  718  docker logs 13.233.38.60

  719  docker logs 0face617feb1

  720  docker attach 0face617feb1 /bin/bash

  721  docker attach 0face617feb1

  722  docker ps

  723  vi Dockerfile

  724  docker build -t image6 .

  725  docker run -it image6

  726  vi Dockerfile

  727  docker build -t image7 .

  728  docker run -it  -p 82:80 image7

  729  docker ps

  730  vi Dockerfile

  731  history

Dockerfile1

FROM ubuntu

MAINTAINER kishor Aswar

RUN apt-get update -y

RUN apt-get install nginx -y

WORKDIR /var/www/html

COPY index.html /var/www/html/

ADD  https://www.free-css.com/assets/files/free-css-templates/download/page288/meetup.zip /var/www/html

RUN apt-get install unzip

RUN unzip  /var/www/html/meetup.zip

EXPOSE 80

ENTRYPOINT ["nginx", "-g", "daemon off;"]

#EXPOSE

Dockerfile In Detail

Docker file Instruction Command:

FROM - Defines a base image, it can be pulled from docker hub
(for example- if we want to create a javascript application with node as backend then we need to have node as a base image, so it can run node application.)

RUN - Executes command in a new image layer( we can have multiple run commands )

CMD - Command to be executed when running a container( It is asked to have one CMD command, If a Dockerfile has multiple CMDs, it only applies the instructions from the last one.

EXPOSE - Documents which ports are exposed (It is only used for documentation)

ENV - Sets environment variables inside the image

COPY - It is used to copy your local files/directories to Docker Container.

ADD - It is more feature-rich version of the COPY instruction. COPY is preferred over ADD. Major difference b/w ADD and COPY is that ADD allows you to copy from URL that is the source can be URL but in COPY it can only have local ones.

ENTRYPOINT - Define a container's executable (You cannot override and ENTRYPOINT when starting a container unless you add the --entrypoint flag.)

VOLUME - It defines which directory in an image should be treated as a volume. The volume will be given a random name which can be found using docker inspect command.

WORKDIR - Defines the working directory for subsequent instructions in the Dockerfile(Important point to remember that it doesn't create a new intermediate layer in Image)









Difference between RUN,CMD and ENTRYPOINT?

RUN - RUN instruction allows you to install your application and packages required for it. It executes any commands on top of the current image and creates a new layer by committing the results. Often you will find multiple RUN instructions in a Dockerfile.

To run this command you will need a separate new layer and this command is mainly used to build images, and install packages and applications.

#RUN apt-get install python

CMD - CMD instruction allows you to set a default command, which will be executed only when you run container without specifying a command. If Docker container runs with a command, the default command will be ignored. If Dockerfile has more than one CMD instruction, all but last CMD instructions are ignored.
The CMD describes the default container parameters or commands. The user can easily override the default command when you use this

#CMD "echo" "Hello World!"

ENTRYPOINT - ENTRYPOINT instruction allows you to configure a container that will run as an executable. It looks similar to CMD, because it also allows you to specify a command with parameters. The difference is ENTRYPOINT command and parameters are not ignored when Docker container runs with command line parameters.
A container with an ENTRYPOINT is preferred when you want to define an executable. You can only override it if you use the --entrypoint flag.

Prefer ENTRYPOINT to CMD when building executable Docker image and you need a command always to be executed. Additionally use CMD if you need to provide extra default arguments that could be overwritten from command line when docker container runs.




The main difference between CMD and Entrypoint is that you can override the CMD instruction from Docker CLI when the container is running. However, you can't override the entrypoint command with just command line parameters. Instead, you need to use the docker run command with a particular syntax.

docker run --entrypoint [new_command] [docker_image] [optional:value]

docker run -it --entrypoint /bin/bash image2


FROM alpine:3.5
LABEL maintainer="Collabnix"

ENTRYPOINT ["/bin/echo", "Hi, your ENTRYPOINT instruction in Exec Form !"]


Executable form V/s Shell Form:

Executable form:

FROM ubuntu
RUN apt-get update && apt-get install -y nginx
ENTRYPOINT ["nginx", "-g", "daemon off;"]

docker inspect <container-id>

 "Entrypoint": [
                "nginx",
                "-g",
                "daemon off;"
            ],

nginx process id is 1

Shell Form:
FROM ubuntu
RUN apt-get update && apt-get install -y nginx
ENTRYPOINT nginx -g "daemon off;"
~                                         

 "Entrypoint": [
                "/bin/sh",
                "-c",
                "nginx -g \"daemon off;\""
            ],

Shell Format sh process id is 1:

[root@ip-172-31-34-29 DockerDemo]# docker exec -it 854e14bbc4ae sh
# top
top - 18:14:36 up 3 days, 16:16,  0 users,  load average: 0.00, 0.00, 0.00
Tasks:   5 total,   1 running,   4 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :    964.8 total,    185.0 free,    187.7 used,    592.0 buff/cache
MiB Swap:      0.0 total,      0.0 free,      0.0 used.    629.4 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
    1 root      20   0    2888    976    880 S   0.0   0.1   0:00.20 sh
    7 root      20   0   55156  12256  10660 S   0.0   1.2   0:00.00 nginx
    8 www-data  20   0   55524   3404   1468 S   0.0   0.3   0:00.00 nginx


Executable form:

asks:   6 total,   1 running,   5 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :    964.8 total,    194.4 free,    178.1 used,    592.3 buff/cache
MiB Swap:      0.0 total,      0.0 free,      0.0 used.    639.2 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
    1 root      20   0   55200  12152  10512 S   0.0   1.2   0:00.20 nginx
    7 www-data  20   0   55524   3368   1436 S   0.0   0.3   0:00.00 nginx
    8 root      20   0    2888    972    876 S   0.0   0.1   0:00.19 sh

[root@ip-172-31-34-29 DockerDemo]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS              PORTS     NAMES
bcccebab6d82   myimg3    "/bin/sh -c 'ng…"   About a minute ago   Up About a minute             vigorous_swanson
6d03a1b2bfc6   myimg1    "nginx -g 'daemon of…"   13 minutes ago       Up 13 minutes                 unruffled_jones



The SHELL form runs the command as a child process (on a shell). The EXEC form runs the executable on the main process.

Commands are written without [] brackets and are run by the container's shell, such as /bin/sh -c

Commands are written with [] brackets and are run directly, not through a shell


Shell features
The main thing you lose with the exec form is all the useful shell features: sub commands, piping output, chaining commands, I/O redirection, and more. 
These kinds of commands are only possible with the shell form:

FROM ubuntu:latest

# Shell: run a speed test
RUN apt-get update \
 && apt-get install -y wget \
 && wget -O /dev/null http://speedtest.wdc01.softlayer.com/downloads/test10.zip \
 && rm -rf /var/lib/apt/lists/*

# Shell: output the default shell location
CMD which $(echo $0)



Most shells do not forward process signals to child processes, which means the SIGINT generated by pressing CTRL-C may not stop a child process:

# Note: Alpine's `/bin/sh` is really BusyBox `ash`, and when
#   `/bin/sh -c` is run it replace itself with the command rather
#   than spawning a new process, unlike Ubuntu's `bash`, meaning
#   Alpine doesn't exhibit the forwarding problem
FROM ubuntu:latest

# Shell: `bash` doesn't forward CTRL-C SIGINT to `top`
ENTRYPOINT top -b

# Exec: `top` traps CTRL-C SIGINT and stops
ENTRYPOINT ["top", "-b"]

FROM ubuntu
RUN apt-get update && apt-get install -y nginx
EXPOSE 80
ENTRYPOINT ["nginx", "-g", "daemon off;"]
#ENTRYPOINT nginx -g "daemon off;"



In general, use the shell form for RUN, and the exec form for everything else.


EXPOSE PORT:
FROM ubuntu:latest
EXPOSE 80/tcp
EXPOSE 80/udp
[root@ip-172-31-34-29 DockerDemo]# docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS         PORTS            NAMES
3dec2971eb8c   portexpose    "nginx -g 'daemon of…"   6 minutes ago   Up 6 minutes   80/tcp           eloquent_swirles
d1f5432037e7   expose_port   "bash"                   8 minutes ago   Up 8 minutes   80/tcp, 80/udp   vibrant_liskov
Volume:
Volumes can be declared in your Dockerfile using the VOLUME statement. This statement declares that a specific path of the container must be mounted to a Docker volume. When you run the container, Docker will create an anonymous volume (volume with a unique id as the name) and mount it to the specified path.

Thursday, March 2, 2023

Docker Commands With Example

[root@ip-172-31-34-29 ~]# docker cp index1.html 6ea34f03afd1:/usr/share/nginx/html/

[root@ip-172-31-34-29 ~]# docker container commit 6ea34f03afd1 kishoraswar123/myweb1

sha256:b41c7cab5316358ffb68db98a1b81666a987c75bce5806494da857e30a2a171c

[root@ip-172-31-34-29 ~]# docker images

REPOSITORY              TAG       IMAGE ID       CREATED         SIZE

kishoraswar123/myweb1   latest    b41c7cab5316   4 seconds ago   142MB

my_web                  latest    86334b49f661   6 minutes ago   142MB

nginx                   latest    3f8a00f137a0   2 weeks ago     142MB

httpd                   latest    3a4ea134cf8e   2 weeks ago     145MB

ubuntu                  latest    58db3edaf2be   4 weeks ago     77.8MB

[root@ip-172-31-34-29 ~]# docker push kishoraswar123/myweb1

Using default tag: latest

The push refers to repository [docker.io/kishoraswar123/myweb1]

92948da76654: Pushed

3ea1bc01cbfe: Mounted from library/nginx

a76121a5b9fd: Mounted from library/nginx

2df186f5be5c: Mounted from library/nginx

21a95e83c568: Mounted from library/nginx

81e05d8cedf6: Mounted from library/nginx

4695cdfb426a: Mounted from library/httpd

latest: digest: sha256:f85a611c28d4bd757ea4b64216323327dd743cf3b01f35b745854493d8dc08b9 size: 1778

[root@ip-172-31-34-29 ~]#


=======================



docker start


docker attach


docker stop


docker logs


docker pause


docker unpause


docker exec


Docker Save:


[root@ip-172-31-34-29 ~]# docker save image_name > tar_file_name.tar

[root@ip-172-31-34-29 ~]# ls -l

total 123660

-rw-r--r-- 1 root root 126601728 Mar  2 17:58 tar_file_name.tar


========================


Docker Load:



[root@ip-172-31-34-29 ~]# docker load < demo.tar

Loaded image: demo_image:latest


=====================


Rename Container:


[root@ip-172-31-34-29 ~]# docker ps

CONTAINER ID   IMAGE                COMMAND                  CREATED        STATUS        PORTS                                   NAMES

5e20070ee824   my_nginx_webserver   "/docker-entrypoint.…"   16 hours ago   Up 16 hours   0.0.0.0:8080->80/tcp, :::8080->80/tcp   eloquent_tu



[root@ip-172-31-34-29 ~]# docker rename 5e20070ee824 myapp

[root@ip-172-31-34-29 ~]# docker ps

CONTAINER ID   IMAGE                COMMAND                  CREATED        STATUS        PORTS                                   NAMES

5e20070ee824   my_nginx_webserver   "/docker-entrypoint.…"   16 hours ago   Up 16 hours   0.0.0.0:8080->80/tcp, :::8080->80/tcp   myapp

[root@ip-172-31-34-29 ~]#


=======================


docker rm



docker kill - stop and remove


docker inspect



================

docker volume 

https://docs.docker.com/storage/


https://www.javatpoint.com/what-is-docker-volume




Docker NEtwork:


https://docs.docker.com/network/

https://earthly.dev/blog/docker-networking/


Tuesday, February 28, 2023

Docker Commands:

 [root@ip-172-31-34-29 ~]# history

    1  yum update -y

    2  yum install docker

    3  docker --version

    4  systemctl status docker

    5  systemctl start docker

    6  systemctl status docker

    7  ps -aef|Grep docker

    8  ps -ef|grep docker

    9  docker --version

   10  docker pull nginx

   11  docker pull ubuntu

   12  docker images

   13  docker pull httpd

   14  docker run  -d -p 80:80 nginx

   15  docker ps

   16  touch index.html

   17  echo "kishor">index.html

   18  cat index.html

   19  docker cp index.html 6ea34f03afd1:/var/www/html/

   20  docker cp index.html 6ea34f03afd1:/usr/share/nginx/html/

   21  docker ps

   22  history

Sunday, February 26, 2023

Docker

 [root@ip-172-31-6-122 ~]# history

    1  yum update

    2  yum install docker

    3  systemctl start docker

    4  docker ps

    5  systemctl status docker

    6  docker run -d nginx

    7  docker ps

    8  docker run -d --name myweb nginx

    9  docker ps

   10  docker container ls

   11  docker  images

   12  docker pull httpd

   13  docker images

   14  docker ps

   15  history



Docker is a set of platforms as a service (PaaS) products that use the Operating system level visualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels

 

Docker V/S VM:



 

Docker Architecture:

 



 

 

Docker Engine

It is the core part of the whole Docker system. Docker Engine is an application which follows client-server architecture. It is installed on the host machine. There are three components in the Docker Engine:

  • Server: It is the docker daemon called dockerd. It can create and manage docker images. Containers, networks, etc.
  • Rest API: It is used to instruct docker daemon what to do.
  • Command Line Interface (CLI): It is a client which is used to enter docker commands.

 

Docker Client

Docker users can interact with Docker through a client. When any docker commands runs, the client sends them to dockerd daemon, which carries them out. Docker API is used by Docker commands. Docker client can communicate with more than one daemon.

Docker Registries

It is the location where the Docker images are stored. It can be a public docker registry or a private docker registry. Docker Hub is the default place of docker images, its stores’ public registry. You can also create and run your own private registry.

When you execute docker pull or docker run commands, the required docker image is pulled from the configured registry. When you execute docker push command, the docker image is stored on the configured registry.

Docker Objects

When you are working with Docker, you use images, containers, volumes, networks; all these are Docker objects.

Images

Docker images are read-only templates with instructions to create a docker container. Docker image can be pulled from a Docker hub and used as it is, or you can add additional instructions to the base image and create a new and modified docker image. You can create your own docker images also using a dockerfile. Create a dockerfile with all the instructions to create a container and run it; it will create your custom docker image.

Docker image has a base layer which is read-only, and the top layer can be written. When you edit a dockerfile and rebuild it, only the modified part is rebuilt in the top layer.

Containers

After you run a docker image, it creates a docker container. All the applications and their environment run inside this container. You can use Docker API or CLI to start, stop, delete a docker container.

Below is a sample command to run a ubuntu docker container:

docker run -i -t ubuntu /bin/bash

Volumes

The persisting data generated by docker and used by Docker containers are stored in Volumes. They are completely managed by docker through docker CLI or Docker API. Volumes work on both Windows and Linux containers. Rather than persisting data in a container’s writable layer, it is always a good option to use volumes for it. Volume’s content exists outside the lifecycle of a container, so using volume does not increase the size of a container.

You can use -v or –mount flag to start a container with a volume. In this sample command, you are using geekvolume volume with geekflare container.

docker run -d --name geekflare  -v geekvolume:/app nginx:latest

Networks

Docker networking is a passage through which all the isolated container communicate. There are mainly five network drivers in docker:

  1. Bridge: It is the default network driver for a container. You use this network when your application is running on standalone containers, i.e. multiple containers communicating with same docker host.
  2. Host: This driver removes the network isolation between docker containers and docker host. It is used when you don’t need any network isolation between host and container.
  3. Overlay: This network enables swarm services to communicate with each other. It is used when the containers are running on different Docker hosts or when swarm services are formed by multiple applications.
  4. None: This driver disables all the networking.
  5. macvlan: This driver assigns mac address to containers to make them look like physical devices. The traffic is routed between containers through their mac addresses. This network is used when you want the containers to look like a physical device, for example, while migrating a VM setup

 

 

Install Docker:

Create EC2 instance:

Login on ec2 instance as a root.

Execute below command:

#yum update -y

#yum install docker

#systemctl start docker

#systemctl status docker


Thursday, February 23, 2023

Tomcat installation and configuration on linux server

1. Create one EC2 instance: 

2.Login on ec2 instance and swith to root user.

Command: #sudo su -

3.Install java

Command:  #yum install java  

4. Set JAVA_HOME

Command: #export JAVA_HOME=/usr/lib/jvm/java-17-amazon-corretto.x86_64

5. check java home is setup succesfully or not

 Command: #echo $JAVA_HOME

/usr/lib/jvm/java-17-amazon-corretto.x86_64

6. Set path

command: #export  PATH=$PATH:$JAVA_HOME

7. Check java home addedd succesfully in path or not

Commnad: # echo $PATH

O/P:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/lib/jvm/java-17-amazon-corretto.x86_64

-----------------------

8. Create middleware directory under opt

mkdir /opt/middleware


9. Go to middleware directory and download apache tomcat.

#cd /opt/middleware

#wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.72/bin/apache-tomcat-9.0.72.tar.gz


10. Extract tomcat and add admin user in  tomcat-users.xml file

#tar -xvf apache-tomcat-9.0.72.tar.gz

cd apache-tomcat-9.0.72/conf

#ls -l

vi  tomcat-users.xml

<role rolename="tomcat"/>

<role rolename="manager-gui"/>

<role rolename="admin-gui"/>

<role rolename="manager-script"/>

<role rolename="manager-jmx"/>

<user username="admin" password="admin" roles="tomcat,manager-gui,admin-gui,manager-script,manager-jmx"/>


---

11.Go to /opt/middleware/apache-tomcat-9.0.72/webapps/manager/META-INF and /opt/middleware/apache-tomcat-9.0.72/webapps/host-manager/META-INF and comment out below content

 <!--

  <Valve className="org.apache.catalina.valves.RemoteAddrValve"

         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />

  <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/> -->


12. Start tomcat

[root@ip-172-31-15-218 bin]# pwd

/opt/middleware/apache-tomcat-9.0.72/bin

[root@ip-172-31-15-218 bin]# ./startup.sh

#ps -ef|grep java 

#ps -ef|grep tomcat


13. Deploy application on tomcat.

[root@ip-172-31-15-218 webapps]# pwd

/opt/middleware/apache-tomcat-9.0.72/webapps

#wget https://github.com/AKSarav/SampleWebApp/raw/master/dist/SampleWebApp.war

Note : context path is SampleWebApp

#ls -l  ==> you will see your war and respective directory.


14.go browser and access your application: 

http://public-ip:8080/SampleWebApp




Sample Game App Deployment on EKS cluster

 https://padmakshi.medium.com/setting-up-an-eks-cluster-and-deploying-a-game-application-a-step-by-step-guide-08790e0be117