Tuesday, March 17, 2020

6. DOCKER & KUBERNETES : ImageCreate Way2 : Create Image using Dockerfile

6. DOCKER & KUBERNETES : ImageCreate Way2 : Create Image using Dockerfile


Requirement: Creating Image using Dockerfile ############################################
Step1: Create docker file (vi Dockerfile)
Step2: Create Image using docker file (docker build -t newubuntu:1.0 -f Dockerfile .)
Step3: Run and test the image (docker run --rm newubuntu:1.0)
########################################################################################

Step1: Create docker file
Example1 : We will create DockerFile
##########################################
//refer image...

https://github.com/dhanan77/RESTAPI22.05

click on -> Sample.sh

#######################################
#!/bin/bash
if [ $# -gt 0 ]
then
if [ -f $1 ]
then
  echo "Contents of the file..."
  cat $1
else
  echo "File Not Found.."$1
fi
else
  echo "Not enough command line arguments..."
fi

########################################

copy

vi Sample.sh

paste

Esc :wq!

sh Sample.sh

sh Sample.sh xxxx

sh Sample.sh /etc/hosts

//create automation file (Docker file) for image management
vi Dockerfile

##############################################
# First Dockerfile simulation of Sample.sh
FROM ubuntu:16.04
LABEL MAINTAINER dj@devops.com
RUN mkdir /code
COPY Sample.sh /code/Sample.sh
RUN chmod +x /code/Sample.sh
CMD sh /code/Sample.sh /etc/hosts
#CMD ["sh","/code/Sample.sh","/etc/hosts"]
###############################################

Esc :wq!

ls  //we should have 2 files (Dockerfile   Sample.sh)

Step2: Create Image using docker file
//every thing does automatically mention all commands in Docker file and build..
//Every line in Dockerfile creates one layer.
//newubuntu is image name
//Creating image "newubuntu:1.0" using docker file "Dockerfile"
docker build -t newubuntu:1.0 -f Dockerfile .    //it will execute line by line mentioned in Dockerfile

docker history newubuntu:1.0  //to check all layers how layers are created...

docker inspect newubuntu:1.0

Step3: Run and test the image //we will test this image
docker run --rm newubuntu:1.0

//execute as bash, customers can go inside and check the code
//Means Its failing as Scaling section, bez people can go and paly with source code
//CF layer test - FAILED
docker run -i -t --rm newubuntu:1.0 bash
cd code
ls
cat Sample.sh

//customer can execute as below also
//CD layer test - FAILED
docker run --rm newubuntu:1.0 sh /code/Sample.sh xxx

docker run --rm newubuntu:1.0 xxx  //will not work

vi Dockerfile

###############################################
# First Dockerfile simulation of Sample.sh
FROM ubuntu:16.04
LABEL MAINTAINER dj@devops.com
RUN mkdir /code
COPY Sample.sh /code/Sample.sh
RUN chmod +x /code/Sample.sh
ENTRYPOINT ["sh","/code/Sample.sh"]
CMD ["/etc/hosts"]
CMD sh /code/Sample.sh /etc/hosts
#CMD ["sh","/code/Sample.sh","/etc/hosts"]
###############################################

Esc :wq!

//build again
docker build -t newubuntu:1.0 -f Dockerfile . 

//now test all security fails again
docker run -i -t --rm newubuntu:1.0 bash   //it will not go inside bash

docker run --rm newubuntu:1.0

docker run --rm newubuntu:1.0 /etc/resolv.conf  //valid file name should work

docker run --rm newubuntu:1.0 xxxxx  //invlid file name should not work

#########################################################################


Example2 : We will create DockerFile for PING and then create image##########################################


------------------- PING COMMAND in Docker file---------------------------------------------
//Do New operation ping like some part should be fixed and some parameter should come as parameter

ping 

docker pull ubuntu:trusty  //pull ping related layers

vi DockerfilePing

###############################################
# Second Dockerfile simulation of Ping
FROM ubuntu:trusty
LABEL MAINTAINER dj@netops.com
ENTRYPOINT ["ping","-c3"]
CMD ["localhost"]
###############################################

Esc :wq!

//build
//ping is image name
//Creating Image "ping:v1" using Dockerfile "DockerfilePing"
docker build -t ping:v1 -f DockerfilePing

docker history ping:v1 

docker run --rm ping:v1 //it will execute ping command mentioned DockerfilePing file

//go to Docker quickstart terminal

docker images

Different ways to ping:
1. Localhost (of Container IP)
2. Ping another Container IP
3. Ping Host IP (DM IP)
4. Ping External IP

//Testing on our newly created Image ping:v1
docker run --rm ping:v1
docker run --rm ping:v1 $(docker inspect -f "{{.NetworkSettings.IPAddress}}" TestLogs)
docker run --rm ping:v1 $(docker-machine ip default)
docker run --rm ping:v1 www.ndtv.com  //will work
docker run --rm ping:v1 www.hub.docker.com  //will not work as all cloud has security

No comments:

Post a Comment