Docker StudentNotes
Docker StudentNotes
Introduction to Docker:
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.
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!!
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)
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.
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 :)
Many companies already have this concept before but Docker hit the right spot in
right
time. PFB companies which are already using this concept.
Commands:
Installation :
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
For explainig this concept you can install package telnet & create user
called devops.
Dockerfile :
FROM centos
RUN yum install telnet -y && useradd devops
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 ::
Execution :
docker build -t "cmd:v1" .
ENTRYPOINT Execution ::
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.
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:
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
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
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
Example1:
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
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::
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:
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
verification:
Installing Tomcat:
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/
http://www.apache.org/licenses/LICENSE-2.0
3. similarly replace tomcat users with below content & change the password based on
the class.
cd /opt/tomcat/conf/
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="dvsdevops" password="dvsdevops" roles="manager-
gui,manager-script"/>
</tomcat-users>
/opt/tomcat/bin/catalina.sh run
Once done with the above, now if you are observing we got our "war" file. Now lets
deploy this to our tomcat
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.
Its very simple make sure you are having below things before you run docker build
command.
"https://github.com/shan5a6/javaDockerDeployment.git"
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
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"]
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"]
# 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