Wednesday, 15 March 2017

Docker - Introduction





Docker - It's pa platform for developing, shipping and running applications using container virtualization technology


Tools as part of Docker platform:


  • Docker Engine
  • Docker Hub
  • Docker Machine
  • Docker Swarm
  • Docker Copmpose
  • Kitematic (GUI client instead CLI client)

Amazon webservices - ?
rackspace hosting - ?

Container based virtualization uses the kernel on the host's operating system to run multiple guest instances


Containers vs VMs :


  • Containers are more lightweight
  • No need to isntall guest OS
  • Less CPU, RAM, storage space required
  • More containers per machine than VMs
  • Greater portability


Docker Engine - It is the program that enables containers to be built, shipped and run.


Installing docker in Ubuntu:-


wget -qo- https://get.cocker.com/ | sh   -> It will give all the commands in the script to shell to run , it takes care of intalling tall the dependancies.


sudo docker run hello-world  -> to check whether docker installed seamlessly


to avoid using sudo before every command , add user to docker group with the following command


sudo usermod -aG docker johnnytu(user)

exit and relogin
now run docker run hello-world directly without sudo it works

this tells we have installed docker in Ubuntu


Docker daemon or Docker engine both are same.


In general we have Docker client an Docker daemon installed in the same host.

We can have Docker Client interacting with Docker Daemon installed in another host.

Client/Server architecture

Client takes user inputs and send them to the daemon
Daemon builds, runs and distributes containers
Client and daemon can run on the same host or on different hosts
CLI client and GUI(Kitematic)


sudo docker version -> to know client and server versions

Docker Hub -> It is the public registry that contains a large number of images available for your use

Docker Orchestration tools : Docker Machine, Docker Swarm and Docker Compose

Orchestration -> automated arrangements, coordination and management of complex computer systems and services

https://hub.docker.com/



docker run ubuntu:14.04 echo "Hello World"


docker images


docker run -i -t ubuntu:14.04 /bin/bash


root>adduser johnny

pwd: xxxx

root>add the user into sudo group


root>adduser johnny sudo


root>su johnny 


johnny> sudo apt-gat install vim   -> installing vim test editor

johnny> vim test   -> this tells vim got installed and operational.

When you container process got stopped you have actually stopped container too.


exit from johnny and then from root stops the bash process that also stops container

Now, if you run docker run -i -t ubuntu:14.04 /bin/bash again, you don't see the user Johnny since it's a new container.
su johnny
cat /etc/passwd

------


docker run ubuntu echo "hello world"   ->container has started echoed and then stopped


docker run -it ubuntu bash

root> ps -ef

Now bash has pid 1 inside the container


Now exit the container without stopping the container by pressing ctrl+P+Q


now check the processes with ps -ef


----


docker ps -> lists the containers up and running

docker ps -a -> lists the containers up and running along with stopped already

----

container running in detached mode i.e in the background
docker run -d centos:7 ping 127.0.0.1 -c 100

check container is running with

docker ps

docker logs <container id>  -> gives 100times ping command - it's a way to inspect the output of the that container 

docker logs -f <container id>  -> it's attached to log file, similar to tail -f

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

download tomcat

docker run -d -P tomcat:7

it's up and running now, you can check that with docker ps command

------------ part2


commit docker images


docker run -it ubuntu:14.04 bash  -> opens bash 

root>curl 127.0.0.1
install curl with
root>apt-get install -y curl

exit bash means container


docker ps -a


docker commit <container-id> johnnytu/myapp:1.0



docker images

docker run -it johnnytu/myapp:1.0 bash
root> which curl  -> check curl is installed
root> curl 127.0.0.1


Dockerfile : It's a configuration file that contains instructions for building a Docker image.


--------

build an image from docker file

mkdir test

cd test
vim Dockerfile

FROM UBUNTU:14:04

RUN apt-get update
RUN apt-get unstall -y curl   
RUN apt-get install -y vim

save and exit


test> docker build -t johnnytu/testimage:1.0 .



docker images -> to verify image created


vim Dockerfile


FROM UBUNTU:14:04

RUN apt-get update && apt-get install -y curl \ vim

save and exit


docker build -t jonnytu/testimage:1.0 .


vim Dockerfile


FROM UBUNTU:14:04

RUN apt-get update && apt-get install -y curl \ vim
CMD ["ping","127.0.0.1","-c","30"]  ->it pings 30 times

save and exit

test>docker build -t johnnytu/testimage:1.1 .
docker run johnnytu/testimage:1.1 ->to run the new image
docker run johnnytu/testimage:1.1 echo "hello world"  -> override CMD instruction

-----

ENTRYPOINT

im Dockerfile


FROM UBUNTU:14:04

RUN apt-get update && apt-get install -y curl \ vim
ENTRYPOING ["ping"]

save and exit


test>docker build -t johnnytu/testimage:1.2 .

docker run johnnytu/testimage:1.2
docker run johnnytu/testimage:1.2 127.0.0.1 -c 5

-----

container start/stop

docker run -d nginx ->run in background

docker ps
docker stop <container-id>
docker ps -> now you can see there is no active containers
docker ps -a 
docker start condencending_jang<container-name>
docker ps -> now you can check container is running

--------

continer exec

docker run -d tomcat:7

docker ps
docker exec -it <container-id> bash
root>/usr/local/tomcat/> 
root> ps -ef
-------
docker rm

docker ps

docker stop lonely_goldstine
docker ps -a
docker rm lonely_goldstine
docker ps -a
----------
docker rmi

docker images

docker rmi johnnytu/testimage:1.1
docker images

docker rmi <image-id>


--------

docker push

docker push johnnytu/restimage:1.0

loging to repo to push with command interactive mode

it should fail because in repo it didn't find johnnytu/restimage:1.0 , to avoid it you should tag the image first.


docker tag johnnytu/testimage:1.0 trainingteam/testimage:1.0

docker images

docker push trainingteam/testimage:1.0 ->it's sending image to docker hub


-----------


Volumes - A Volume is a designated directory ina container, which is designed to persist data, independent of the container's life cycle.


docker run -d -P -v /www/website nginx

docker exec -it <container-id> bash
root> cd www/website
root>/www/website/> echo "hell" >>test
root>/www/website/> ls -> test
cat test -> hello
exit
docker stop <container-id>
docker commit <container-id> test:1.0
docker run -it test:1.0 bash
root> ls -> www\website -> ls --> there is no file
Volume data has been excluded when updating an image

----------

ports

docker run -d -p 8080:80 nginx

docker ps
----------
docker linking

docker run -d --name dbms postgres

docker ps
docker run -it --name website --link dbms:db ubuntu:14.04 bash
root> cat /etc/hosts
an entry will be created with db in hosts file with ip address of dbms container

check this with following command

docker inspect dbms | grep IPAddress -> it displays ip address

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

Docker in contentious Integration

In CI you can compile and run as part of Image


Docker Maven PlugIn:

<!-- https://mvnrepository.com/artifact/com.spotify/docker-maven-plugin -->
<dependency>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.4.13</version>

</dependency>

Home Page : https://github.com/spotify/docker-maven-plugin

https://github.com/VamshiKrishnaNemalikonda/deegeu-maven-java-docker-example

Create Docker Container:
mvn clean package docker:build (Used in development when you want to create a Docker container locally)

Create/Push Docker Container:

mvn clean package docker:build -DpushImage (Used in continuous integration tools when you want to deploy the resulting Docker container to Docker Hub or a private repository)



No comments:

Post a Comment