0% found this document useful (0 votes)
17 views

Docker StudentNotes

Uploaded by

Vinod Chowdhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Docker StudentNotes

Uploaded by

Vinod Chowdhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Dockers Student Notes:

Learning the Basics of Docker :

Introduction to Docker:

Docker is an open-source Project that automates the deployment of applications


inside
software containers,
by providing
an additional layer of abstraction and automation of operating system-level
virtualization
on Linux.

So basically, it is a tool (or a set of tools depending on how you look at it) that
packages
up an application
and all its
dependencies in a "virtual container" so that it can be run on any linux system or
distribution.

When would we use Dockers:

There are lot of reasons to use docker. Although you will generally hear
about docker
used in conjunction
with develpment
and deployment of applications, there are a ton of examples for use:

* Configuration simplification
* Enhance Developer Productivity
* Server Consolidation and management
* Applicaiton isolation
* Rapid Deployment
* Build Management

Keep in mind these are only a few use cases. We are going to explore many more
during our
course!!

Containers vs. Virtual Machines::

What is a virtual Machine:

In basic terms, a virtual machine is an emulation of a specific computer


system type.
They operate based
on the architecture
and functions of that real computer system type and
its implementation can involve specialzied hardware,software or both.

when you think of a virtual machine , you probably think of vmware, citrix
and or virtual
box.
Virtualization software allows you to
setup one operating system with another. Although they both share the same
physical
hardware,
the virtual machine is isolated from
that hardware and has to communicate with it through something called a
Hypervisor.
An aws instance is one type of virtual Machine !

What is a container:
A container is exactly what you might except it to be based on the general
definition
of the word.
It is an entirely isolated set of
packages, libraries and/or applications that are completely independent from
its
surroundings.

In the simplest example, you place your leftovers in a plastic container and
then
set it on the table. Although the table lends the platform
on which the leftover are resting upon, they are independent of the table
itself.
What you do to one does not necessarily affect the other
(although in certain instances it can)

What is the difference important ::

As in most things in life, the importance is in perspective. From the


perspective of
getting the
most performance out of hardware purchased,
virtualization was invented to allow us to share but segregate server
instances from
each other.
This way, we could protect one operating system
from another with out letting space CPU cycles, memory or disk space go to
waste.

Now, virtualization is becoming more granular. We have virtual servers, but


they are
based on emulating
virtual hardware through a hypervisor.
This means that they are heavy in terms of system requirements. Containers
however,
use shared operating
systems and more efficient in system
resource terms.

Docker Architecture:

Docker is a client-server application where both the daemon and client can be
run on
the same system or you can connect a docker client with a remote docker daemon.

Docker clients and daemos communicate via sockets or through a RESTFful


API(representational state transfer- it is a stateless transfer over httpd of a
web page c
containing an XML file that descirbles and includes the desired content).

The main components of Docker are:


daemon
client
Docker.io registry

See the docker architecture picture you saved to the dockers directory.

From the picture we can conclude that docker engine manages all the resource
allocations
for the applications, there is no necessasity to install a new OS and install
application on
top of it. This will not save our resources like memory,disk & hardware as well as
our precious time to install and configure the OS :)

Hasn't this already been done ?

Many companies already have this concept before but Docker hit the right spot in
right
time. PFB companies which are already using this concept.

FreeBSD :-> Jails


Sun(and now oracle) Solaris :-> Zones
Google :-> IMctfy
Openvz

Topic 1 :: Introduction & Installation

1. Installation & configuration of dockers


--> explain the default folder structure
--> docker hub & its usage
--> image & its defaults with tags

Commands:
Installation :

yum install docker -y


systemctl enable docker
systemctl restart docker

Topic 2:: Working with containers

1. pulling centos image: centos & centos:6.9


#docker pull centos
#docker pull centos:6.9
2. Starting the first container using centos & explain in detail about the
magic happened
options : -i,-t & usecases
# docker run -it centos /bin/bash

3. Running container in backend using "-d" option && Working with Multiple
Images
4. Inspecting the container.
#docker inspect <containerid>
5. Attaching and executing commands inside the container
#docker run -ti centos /bin/bash
note: ctrl+p to comeout of container with out killing it
#docker exec -it <containerid> /bin/bash -c "uptime;df -hT"

6. Create two containers with test.txt file & explain in detail about the
container architecture

Topic 3: Building our own images or Image customization.

1. Explain docker image build using "commit" & via Dockerfile


#docker commit <containerid> <imagename>
#docker build -t "imagename" .

For explainig this concept you can install package telnet & create user
called devops.

Dockerfile :
FROM centos
RUN yum install telnet -y && useradd devops

Topic 4: Dockerfile References

exec
# docker exec -it <containerid> /bin/bash -c "uptime"
RUN
ENV
ADD
COPY
USER
EXPOSE
PORTFORWARD
VOLUME(Storage management)
CMD
ENTRYPOINT

Example ::

FROM centos
RUN yum install telnet nginx -y && useradd test1
ENV myenv=100
ADD tmp.tar /tmp/
COPY tmp.tar /tmp/
EXPOSE 80
VOLUME ["/usr/share/nginx/html"]
CMD ["nginx", "-g", "daemon off;"]

Execution:
docker run -d --name mynginx -v /mydata:/usr/share/nginx/html/ -p 80:80 mynginx:v1

CMD vs ENTRYPOINT ::

CMD:
This helps you to overwrite the base command declared inside the image

ENTRYPOINT:
You cannot over write the entrypoint once you created the image

CMD Execution ::

[root@dockers ~]# cat script1.sh


#!/bin/bash
echo "Hi I am Script1"
[root@dockers ~]# cat script2.sh
#!/bin/bash
echo "Hi I am script2"
[root@dockers ~]#

[root@dockers ~]# cat Dockerfile


FROM centos:7
COPY script1.sh script2.sh /tmp/
CMD ["/bin/bash","/tmp/script1.sh"]

Execution :
docker build -t "cmd:v1" .

[root@dockers ~]# docker run cmd:v1


Hi I am Script1
[root@dockers ~]#

[root@dockers ~]# docker run cmd:v1 /bin/bash /tmp/script2.sh


Hi I am script2
[root@dockers ~]#

ENTRYPOINT Execution ::

[root@dockers ~]# docker build -t "entry:v1" .


Sending build context to Docker daemon 73.22kB
Step 1/3 : FROM centos:7
---> 5e35e350aded
Step 2/3 : COPY script1.sh script2.sh /tmp/
---> Using cache
---> 5f974cbdbe98
Step 3/3 : ENTRYPOINT ["/bin/bash","/tmp/script1.sh"]
---> Running in 07caa0e525ed
Removing intermediate container 07caa0e525ed
---> cf9341cc8594
Successfully built cf9341cc8594
Successfully tagged entry:v1
[root@dockers ~]# docker run entry:v1
Hi I am Script1
[root@dockers ~]# docker run entry:v1 /bin/bash /tmp/script2.sh
Hi I am Script1

Topic 5: Storage Management

Storage Overview:

Data is classified based up on our need, it means temporary data & perminent data.
Ephemeral represents short time data. Basically containers will get the temporary
volumes binded with them when we create them.
If we are storing any data inside the container that data will stay with it till
its next restart. which means if its getting restarted
then the entire data is lost. Inorder to overcome this situation we are having
storage concept which in deed helps us to attach a volume to the
container and it act like perminent storage in simple we call it like persistant
volume.

Categories of data storage:

Non-persistent
Local storage
Data that is ephemeral (short term data)
Every container has it
Tied to the lifecycle of the container

Persistent
Volumes
Volumes are decoupled from containers

Non-persistent Data ::

By default all the containers uses local storage. Default location for the
containers are as follows.

Storage locations:

Linux: /var/lib/docker/[STORAGE-DRIVER]/
Windows: C:\ProgramData\Docker\windowsfilter\

In each and every OS there will be default storage drivers which helps contaniers
for their consumption.

Storage Drivers:

RHEL uses overlay2.


Ubuntu uses overlay2 or aufs.
SUSE uses btrfs.
Windows uses its own.

Persistent Data Using Volumes ::

When ever we are using volumes with containers it will do the below.

-> creates the volume first and then creates the container.
-> mounts it to a directory inside the container
-> if container have data inside that directory it will be overwritten
-> deleting a container does not delete the volume
-> We have different drivers available in the market some of them are
Block storage --> EBS
File storage -> EFS,NFS
Object storage -> Amazon Simple Storage Service (S3),Google Cloud
Storage,Azure Blob Storage

Volume Commands:

docker volume ls
docker volume create myvol1
docker volume inspect myvol1

Create the volume myvol1 and attach it to the nginx container.

hint: /usr/share/nginx/html is the default directory for nginx.

Explain diff between bind mounts & volume mounts.

Topic 6: Network Management

1. Explain in detail about the ifconfig output & network command of docker
2. Explain about the man page of docker
man docker-network-create
3. Create one adapter with the below configuration.
gateway 10.1.0.1
ip-range=10.1.4.0/24
subnet 10.1.0.0/16

4. Now lets create one container with this adapter


docker run -idt --name mynet1 --net devops --ip 10.1.4.5 centos
/bin/bash

5. Creating one container with ipaddress.


docker run -idt --name mynet1 --net devops --ip 10.1.4.5 centos
/bin/bash

6. Delete the pods, & network adapter created.

Topic 7: Monitoring Docker containers & housekeeping


1. Monitoring:
docker stats
docker top
2. Housekeeping docker containers.
docker rm -f <containerid>
docker rmi -f <imageid>

Topic 8: Working with docker registry

1st way via tar:

1. docker save -o myimage.tar mycustom


2. docker load -i myimage.tar

2nd Way via Dockerhub:

1. Customize one docker image and try to tag it & push it to docker hub
2. Save & load the images locally
3. Remove the local image & pull the dockerhub image pushed from step 1 &
show the output.
Note: User docker login & logout to login to the dockerhub

Real Time Examples ::

Example1:

****** Hosting application on multiple containers ******

Explain in detail about the requirement of hosting our application.

--> Create the base image for our apache

FROM centos:7
RUN yum install httpd -y
EXPOSE 80
VOLUME ["/var/www/html"]
CMD ["/usr/sbin/httpd","-D","FOREGROUND"]

--> Now lets host our application on two containers app1 & app2
app1 --> port forwarded to 8081 --> mounted to volume /myapplication
app2 --> port forwarded to 8082 --> mounted to volume /myapplication

Download and host the appliation content to the folder "/myapplication"

wget -O /tmp/bluefreedom3.zip http://static.oswd.org/designs/3682/bluefreedom3.zip


unzip -d /myapplication/ /tmp/bluefreedom3.zip
cd /myapplication
mv bluefreedom3/* .
rm -rf bluefreedom3

--> Lets build our containers


docker run -d --name app2 -p 8082:80 -v /myapplication:/var/www/html myapp:v1
docker run -d --name app1 -p 8081:80 -v /myapplication:/var/www/html myapp:v1

--> Installing nginx on base machine :

amazon-linux-extras install nginx1.12 -y


systemctl enable nginx
systemctl restart nginx

Configure the nginx as below.

vim /etc/nginx/conf.d/default.conf

upstream containerapp {
server 54.149.202.98:8081;
server 54.149.202.98:8082;
}

server {
listen *:80;

server_name 54.149.202.98;
index index.html index.htm index.php;

access_log /var/log/nginx/localweb.log;
error_log /var/log/nginx/localerr.log;

location / {
proxy_pass http://containerapp;
}
}
systemctl restart nginx

Final Test:
Login to the browser & hit the ipaddress you should able to see the blueocean page
from the container1 or 2 (app1/app2)

Bringdown the container one app1 & show the output to audiance.
Bringdown other container as well app2 & show the output to audiance.

Example 2::

2. Migrating on prem Java Based applications to Microservices ::

Explain in details about the java based application on the physical server/vm.
install java,mvn,tomcat,git & finally run the application manually. Once done
slowly convert the things to
containers

Virtual Machine ::

Installing Java:

yum install java-1.8.0-openjdk-devel -y

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk

Make the variable permenent make sure you are having below content in ~/.bashrc
file

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk
export PATH=$PATH:$JAVA_HOME

Installing maven :

You will find all the mvn release in the below link

mvn mirrors:
http://mirror.olnevhost.net/pub/apache/maven/binaries/

export Mvn_Version=3.6.3
wget https://downloads.apache.org/maven/maven-3/${Mvn_Version}/binaries/apache-
maven-${Mvn_Version}-bin.tar.gz
tar xvfz apache-maven-${Mvn_Version}-bin.tar.gz
mkdir /usr/local/apache-maven/apache-maven-${Mvn_Version} -p
mv apache-maven-${Mvn_Version}/* /usr/local/apache-maven/apache-maven-$
{Mvn_Version}/
export M2_HOME=/usr/local/apache-maven/apache-maven-${Mvn_Version}
export M2="${M2_HOME}/bin"
export PATH=$PATH:$M2

Note: Make them permenent by adding them to ~/.bashrc file

verification:

[root@ip-172-31-93-176 apache-maven-3.2.2]# mvn -version


Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/apache-maven/apache-maven-3.6.3
Java version: 1.8.0_242, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-
1.8.0-openjdk-1.8.0.242.b08-0.amzn2.0.1.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.14.171-136.231.amzn2.x86_64", arch: "amd64", family:
"unix"
[root@ip-172-31-93-176 apache-maven-3.2.2]#

Installing Tomcat:

1. perform the below steps

mkdir /opt/tomcat/ -p
export Tomcat_Version=8.5.59
wget http://www-eu.apache.org/dist/tomcat/tomcat-8/v${Tomcat_Version}/bin/apache-
tomcat-${Tomcat_Version}.tar.gz
tar xvfz apache-tomcat-${Tomcat_Version}.tar.gz
rm -f apache-tomcat-${Tomcat_Version}.tar.gz
mv apache-tomcat-${Tomcat_Version}/* /opt/tomcat/.
cd /opt/tomcat/webapps/manager/META-INF/

2. Now replace context.xml file with the below data.

<?xml version="1.0" encoding="UTF-8"?>


<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software


distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<Context antiResourceLocking="false" privileged="true" >
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|
Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\
$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>

3. similarly replace tomcat users with below content & change the password based on
the class.
cd /opt/tomcat/conf/

[root@ip-172-31-16-183 tomcat]# cat tomcat-users.xml


<tomcat-users>
<!--
<role rolename="tomcat"/>
<role rolename="role1"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="role1" password="tomcat" roles="role1"/>
-->

<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="dvsdevops" password="dvsdevops" roles="manager-
gui,manager-script"/>

</tomcat-users>

4. once done check the tomcat with the below command.

/opt/tomcat/bin/catalina.sh run

5. Now lets clone our code and build the artifacts.

yum install git -y


git clone https://github.com/shan5a6/myweb.git
cd myweb/
mvn clean install

Once done with the above, now if you are observing we got our "war" file. Now lets
deploy this to our tomcat

[root@ip-172-31-93-176 target]# pwd


/root/myweb/target
[root@ip-172-31-93-176 myweb]# cd target/
[root@ip-172-31-93-176 target]# ls -l *.war
-rw-r--r-- 1 root root 3058508 Jan 1 13:59 myweb-0.12.0.war

cp myweb-0.12.0.war /opt/tomcat/webapps/
/opt/tomcat/bin/catalina.sh run

Result:

now open the browser and try to hit on port 8080 you can able to see the
application is running.

Lets try to migrate this service to the microservices ...

Its very simple make sure you are having below things before you run docker build
command.

download the latest maven release


make sure you have context.xml & tomcat-users.xml files
clone the mywebcode
Or else download the entire code from

"https://github.com/shan5a6/javaDockerDeployment.git"

Base Image for the infrastructure ::

Dockerfile ::

FROM centos:7
# Installing Java
ENV JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk
ENV PATH=$PATH:$JAVA_HOME
RUN yum install java-1.8.0-openjdk-devel wget -y
EXPOSE 8080

# Installing Maven
ENV Mvn_Version=3.6.3
ENV M2_HOME=/usr/local/apache-maven/apache-maven-${Mvn_Version}
ENV M2="${M2_HOME}/bin"
ENV PATH=$PATH:$M2

RUN wget https://downloads.apache.org/maven/maven-3/${Mvn_Version}/binaries/apache-


maven-${Mvn_Version}-bin.tar.gz && \
tar xvfz apache-maven-${Mvn_Version}-bin.tar.gz && \
mkdir /usr/local/apache-maven/apache-maven-${Mvn_Version} -p && \
mv apache-maven-${Mvn_Version}/* /usr/local/apache-maven/apache-maven-$
{Mvn_Version}/

# Installing and configuring Tomcat

ENV Tomcat_Version=8.5.54
RUN wget http://www-eu.apache.org/dist/tomcat/tomcat-8/v${Tomcat_Version}/bin/
apache-tomcat-${Tomcat_Version}.tar.gz && \
tar xvfz apache-tomcat-${Tomcat_Version}.tar.gz && \
mkdir -p /opt/tomcat/ /opt/myapplication/ -p && \
mv apache-tomcat-${Tomcat_Version}.tar.gz /tmp/ && \
mv apache-tomcat-${Tomcat_Version}/* /opt/tomcat/.
COPY context.xml /opt/tomcat/webapps/manager/META-INF/
COPY tomcat-users.xml /opt/tomcat/conf/
CMD ["/opt/tomcat/bin/catalina.sh", "run"]

Application Image for developers ::

Dockerfile ::

FROM myappbaseimage
WORKDIR /opt/myapplication
RUN yum install git -y \
&& git clone https://github.com/shan5a6/myweb.git /opt/myapplication \
&& mvn clean install \
&& mv ./target/myweb*.war /opt/tomcat/webapps/app.war
CMD ["/opt/tomcat/bin/catalina.sh", "run"]

Multi Stage Docker Builds ::

FROM centos:7 as build


# Installing Java
ENV JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk
ENV PATH=$PATH:$JAVA_HOME
RUN yum install java-1.8.0-openjdk-devel wget git -y

# Installing Maven
ENV Mvn_Version=3.6.3
ENV M2_HOME=/usr/local/apache-maven/apache-maven-${Mvn_Version}
ENV M2="${M2_HOME}/bin"
ENV PATH=$PATH:$M2

RUN wget https://downloads.apache.org/maven/maven-3/${Mvn_Version}/binaries/apache-


maven-${Mvn_Version}-bin.tar.gz && \
tar xvfz apache-maven-${Mvn_Version}-bin.tar.gz && \
mkdir /usr/local/apache-maven/apache-maven-${Mvn_Version} /opt/myapplication/ -
p && \
mv apache-maven-${Mvn_Version}/* /usr/local/apache-maven/apache-maven-$
{Mvn_Version}/ && \
git clone https://github.com/shan5a6/myweb.git /opt/myapplication/
WORKDIR /opt/myapplication/
RUN mvn clean install

# Installing and configuring Tomcat


FROM centos:7
EXPOSE 8080
ENV Tomcat_Version=8.5.59

RUN yum install java-1.8.0-openjdk-devel wget -y

RUN wget http://www-eu.apache.org/dist/tomcat/tomcat-8/v${Tomcat_Version}/bin/


apache-tomcat-${Tomcat_Version}.tar.gz && \
tar xvfz apache-tomcat-${Tomcat_Version}.tar.gz && \
mkdir -p /opt/tomcat/ /opt/myapplication/ -p && \
mv apache-tomcat-${Tomcat_Version}.tar.gz /tmp/ && \
mv apache-tomcat-${Tomcat_Version}/* /opt/tomcat/.
COPY context.xml /opt/tomcat/webapps/manager/META-INF/
COPY tomcat-users.xml /opt/tomcat/conf/
COPY --from=build /opt/myapplication/target/myweb-0.12.0.war
/opt/tomcat/webapps/myapp.war
CMD ["/opt/tomcat/bin/catalina.sh", "run"]

--------------------------------- THE END


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

You might also like