Thursday, October 11, 2018

Docker


workshop105 – docker
 INSTALLING DOCKER 
Point 1: On centos, you can install it by
1
sudo yum -y install docker
Point 2:To start docker
1
sudo systemctl start docker
Point 3:To enable docker
1
sudo systemctl enable docker
Point 4:Verify the installation with
1
sudo docker info
Point 5:Login to the docker in terminal
1
sudo docker login --username=user --email=

CREATING DOCKER REPO
Point 1:First setup docker hub account
Step1:Create your account with email id and give a password
Point 2:Verifying email activation
Step 1:Check your email account for message to complete account activation,then sign into the site.
Point 3:Creating Docker hub repository
Step 1:From the docker hub dashboard click ‘Create Repository’
Step 2:Fill out the repo details (either public or private) then click create.
Point 4:Setting repo Team access
Step 1:To set acces to your repo by team,click ‘Collaborators’
Point 5:Accessing your docker hub repo from command line
1
sudo docker login
CREATING DOCKER FILE
Point 1:Create a file called Docker file using vi editor
1
sudo vi Dockerfile
Point 2:Build the dockerfile as per format.First line has to start with FROM keyword.
Step 1:FROM centos7
Point 3:The RUN instruction will run a command in your image environment.
1
2
3
run yum -y install epel-release
run yum -y update
run yum -y install nginx  
Point 4:The ADD instruction copies files to image system.
Step 1:ADD index.html /usr/share/nginx/index.html
Point 5:The EXPOSE instruction tells docker which network ports the container will listen on.
1
EXPOSE 80/tcp
Point 6:Finally we need to tell docker what command to run when the container is started up with CMD instruction.
Step 1:CMD [“executable”, “param1″,”param2”]

BUILDING AND RUNNING THE CONTAINER
Point 1:Write a Docker file according to the format
 FROM debian:jessie
RUN apt-get update
RUN apt-get install -y git
RUN apt-get install -y vim
Point 2:Build docker image
1
docker build -t user/image name
Point 3:Now you can see your latest build using ‘docker images’ command
1
docker images
Point 4:Run your new docker file
1
docker run -d user/image name
Point 5:Check the browser with ip:port no.

 CREATING DOCKER VOLUME
Point 1:Create a volume and then configure the container to use it
Point 2:Command to create a docker volume
1
docker volume create hello
Point 3: Command to run a docker volume
1
docker run <span class="nt">-d</span> <span class="nt">-v</span> hello:/world busybox <span class="nb">ls</span> /world
 Point 4:Volumes names must be unique among drivers.We cannot use the same volume name with 2 different drivers.If we attempt
1
docker
Point 5:It returns an error
1
A volume named  "hello"  already exists with the "some-other" driver. Choose a different volume name.

DEPLOY STATIC HTML WEBSITE AS A CONTAINER
Point 1:Create your Dockerfile for building your image by copying the contents below into the editor.
Step 1: FROM nginx:alpine
Step 2:COPY . /usr/share/nginx/html
Point 2:Build our static HTML image using the build command below.
1
docker build -t webserver-image:v1
(The built image will have the name webserver-image with a tag of v1.)
Point 3:TO view a list of all the images on the host.
1
docker images.
Point 4:Launch our newly built image providing the friendly name and tag. As it’s a web server, bind port 80 to our host using the -p parameter.
1
docker run -d -p 8080:80 webserver-image:v1
Once started, you’ll be able to access the results of port 80 via curl docker
Point 5:You now have a static HTML website being served by Nginx.
1
Ip-Address:80

 INTEGRATING DOCKER WITH JENKINS
Point 1:Installing docker on jenkins
1
2
3
4
5
sudo yum -y install docker
sudo systemctl start docker
sudo systemctl start docker
sudo systemctl enable docker
sudo docker info 
Point 2:Adding docker to jenkins
1
2
3
4
sudo groupadd docker
sudo usermod -aG docker jenkins
sudo systemctl restart jenkins
sudo systemctl restart docker 
Point 3:Configuring docker in jenkins
Step 1:Access jenkins in browser
Step 2:click on manage jenkins
Step 3:click on configure system
Step 4:Environment variable section(Name:prod_ip,value:ip address [another server])
Point 4:Adding docker hub credentials
Step 1:Go to jenkins main page
Step 2:click on credentials
Step 3:Add credential
>username=xxxxxxxx
>password=docker password
>ID=docker_hub
>Description=docker hub
Step 4:add it
Point 5:Commiting changes
Step 1:goto github repository
Step 2:make changes in jenkins file
Step 3:Commit changes.

LOAD BALANCING CONTAINERS
Point 1:In this scenario, we want to have a NGINX service running which can dynamically discovery and update its load balance configuration when new containers are loaded.
Step 1:Add Nginx Repository
1
sudo yum install epel-release
step 2:Install NGINX.
1
sudo yum install nginx
Step 3:Start Nginx
1
sudo systemctl start nginx
step 4:If you are running a firewall, run the following commands to allow HTTP and HTTPS traffic:
1
2
3
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload   
step 4:Access in your Web browser.
1
http://server_domain_name_or_IP:80
step 5:launch nginx-proxy
1
docker run -d -p 80:80 -e DEFAULT_HOST=proxy.example -v /var/run/docker.sock:/tmp/docker.sock:ro --name nginx jwilder/nginx-proxy
Point 2:Single Host
Step 1: For Nginx-proxy to start sending requests to a container you need to specify the VIRTUAL_HOST environment variable.
1
docker run -d -p 80 -e VIRTUAL_HOST=proxy.example katacoda/docker-http-server
Point 3 :Cluster
Step 1: Launch a second container using the same command as we did before.
1
docker run -d -p 80 -e VIRTUAL_HOST=proxy.example katacoda/docker-http-server
Point 4 :Generated NGINX Configuration.
Step 1:To check the Automated Generated configuration file.
1
docker exec nginx cat /etc/nginx/conf.d/default.conf
Step 2:To know the Additional Information about configuration.
1
docker logs nginx

GETTING STARTED WITH SWARM MODE
Point 1 : Initialise Swarm Mode
-> Create Swarm Mode Cluster
1
2
$ docker swarm --help
$ docker swarm init
Point 2 : Join Cluster
1
2
3
$ token=$(docker -H 172.17.0.100:2345 swarm join-token -q worker) &amp;&amp; echo $token
$ docker swarm join 172.17.0.100:2377 --token $token
$ docker node ls 
Point 3 : Create Overlay Network
1
$ docker network create -d overlay skynet
Point 4 : Deploy Service
1
2
3
4
$ docker service create --name http --network skynet --replicas 2 -p 80:80 katacoda/docker-http-server
$ docker service ls
$ docker ps
$curl docker
Point 5 : Inspect State
1
2
3
4
$ docker service ps http
$ docker service inspect --pretty http
$ docker node ps self
$ docker node ps $(docker node ls -q | head -n1)
-> scale service
1
2
3
4
$ curl docker
$ docker service scale http=5
$ docker ps
$ curl docker


dvps06vc01-lxa-Docker Certified Associate Prep Course-IT1

Docker Kubernetes Implementation Plan
Plan Follows the below steps:
1.Installation of docker on Centos
2.Storage Driver
3.Docker Swarm
4.Configure Universal Control Plane (UCP)
5.Setting up DTR(Docker Trusted Repository )
6. Pull image, Run a Container and tag an image
7.Docker Services
8.Replicate Service globally
9.Stack Deployment
10.Docker Storage Volumes
11.Networking

Installation of docker on Centos
Device Mapper:
It is used for storage of sub-systems of the Containers.
Step 1:  Installing device-mapper-persistent data Package.
1
$ yum install -y yum-utils device-mapper-persistent-data lvm2
Step 2: Installing the Stable or Production version repo of Docker
1
$ sudo -E yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo   #Downloading &amp; adding Repo to the Docker CE(Comunity Edition)
Step 3: Update the machine
1
$ yum update
Step 4: Install the docker-ce
1
$ yum install docker-ce   #It installs the docker and all dependencies.
Step 5: Enable,Start & Check the status of the docker
1
2
3
$ systemctl enable docker
$ systemctl start docker
$ systemctl status docker
Step 6: Checking docker images as a root
1
$ docker images         #It will shows the docker images present in the root user
Step 7: Running the Docker Commands in Non-root user
In Order to check the docker images as non-root user,add the particular user to docker group ,so that we will checkthe any info.
1
2
3
$ usermod -aG docker user          #Add the user
$ ssh user@Ip       #Logging to the user then we will access the any info
$ docker images
Add a Storage Driver
Selecting the Docker Storage Driver:
Containers support to be abstract & Portable
Step 1: Checking the Docker info
1
$ docker info
Step 2: Checking the storage driver of the docker
1
$ docker info| grep Storage
Storage Driver: Overlay
Step 3: Creating the .json file under /etc/docker directories
1
$ vim daemon.json       #Enter some code
{
“storage-driver” = ”devicemapper”
}          #In CentOs officially supports the device-mapper storage driver
–> Once we change the storage driver The docker images no longer will live (or) it will goes exited state (or) sometimes the images are get deleted when stop & start dokcer after changing the storage-driver
Step 4: Checking the docker images are available or not
1
$ docker images         # It will displays some images before restarting the docker
Step 5: After Creating the json file ,Restart the docker
1
2
$ systemctl stop docker &amp;&amp; systemctl start docker
$ docker images          # All images will removed after  restarting the docker
Step 6: Goto the directories
1
$ cd /var/lib/docker      #You can find the device-mapper directory
Docker Swarm
Setting of Swarm(Configure Mnaagers):
–> This is the first step to making a Docker Swarm available for use. Let’s configure our first Swarm Manager and learn how to display the commands and tokens for joining other managers and workers to our cluster.
–> Docker swarm contains manager and node.
–> Node does the work and manager manages the nodes.
Step 1: Initializing the swarm
1
$ docker swarm init --autolock=true
Step 2: Generating a key/token
1
$ docker swarm init  --advertise-addr IP-Address    #Generates a token
–> This step generate the token number, and the current node acts as a manger in swarm
NOTE: Make sure To have a backup of key.
Step 3:  Adding Worker Node
1
$ docker swarm join-token worker        #This generates a token,copy that token and run in to the Worker nodes
Step 4: Add another Manager Node
1
$ docker swarm join-token manager
–> This will generate the token copy the token and run it in a node which you wants to add as a manager.
Step 5: Check whether nodes and managers are added in swarm
–> Run it in manager node:
1
$ docker node ls       # It will display the nodes and managers

                                         Configure Universal Control Plane (UCP)
NOTE: Add Hostname and Ip’s(private ip of linux academy server) in both manager and worker nodes
1
$ vi /etc/hosts
private_ip hostname
private_ip alias_ucp_name
private_ip hostname
Private_ip alias_dtr_name
Example:-
172.31.17.105 ncodeit6.mylabserver.com
172.31.17.105 ucp.example.com
172.31.26.79 ncodeit2.mylabserver.com
172.31.26.79 dtr.example.com~
NOTE: Add public_ip’s of linux academy in local PC
1
$ vi /etc/hosts
public_ip aliasname_ucp
public_ip aliasname_dtr
step 1: Create UCP Container
1
$ docker container run --rm -it --name ucp -v /var/run/docker.sock:/var/run.docker.sock docker/ucp:2.2.4 install –host-address IP-addr --interactive
# it asks for create user name and password:
Admin username:
Admin password:
Confirm Admin Password:
it prompts to create alias, create alias (this alias is used to login on to the UCP)
now you can access in browser on alias name or ip.
                    https://aliasname/login
login with username:
password:
Step 2:   Create a organization, Team, User and add user to a team
–> User Management → users → create user
set the user name and password for the user.
–>Organization &Teams  –> create organization
set the name for organization  and click on create button
–> Click on created organization name –> create a Team –> set Team Name –> click on create team
–> Add a user to the Team
select   organization –> team –> add user –> select the user from the list.

 Setting DTR(Docker Trusted Repository )
Step 1: Run DTR container
1
$ docker run -it --rm docker/dtr install --ucp-node dtr_hostname --ucp-username admin --ucp-url https://ucp_hostname --ucp-insecure-tls
–> Access UCP portal on web- browser
https://ucp_aliasname.com
–> Admin settings –> UCP nodes –> select the node to install the DTR
–> Select Disable TLS Certificate
–> Copy and Run the Command On The Node to Install DTR
–> Check  DTR console on the browser
https://dtr_aliasname.com
–> Goto UCP console
Admin settings → Docker Trusted Repository  # we can see the nodes

Pull an Image, Run a container and Tag image
step 1: Pull from docker hub
1
$ docker pull httpd
step 2: Run a container
1
$ docker run -d --name testweb httpd
step 3: Tag an image
1
$ docker tag httpd myhttpd

Docker Service
Step 1: Pull an image from central hub
1
$ docker pull httpd
Step 2: Run a httpd container
1
$ docker run --d --name testweb httpd
Step 3: Check IPAddress of the container
1
$ docker container inspect testweb | grep IPAddress
Step 4: Check the whether container is running
1
$ elinks http://IP-Address:port
Step 5: Create a docker service
1
2
$ docker service create --name testweb -p 80:80 --replicas 3 httpd           # creates a service on  3 nodes on 80 port
$ docker node ls          # displays all nodes linked with swarm
Step 6: Check Services are up and running.
1
2
$ docker service ls      # displays all services
$ docker service ps testweb   # displays running service on n number of nodes
Step 7: Check whether services are running in all swarm nodes
1
2
3
$ elinks http://node1
$ elinks http://node2
$ elinks http://node3

Replicate Service globally
step 1: create service to all tasks
1
$ docker service create --name testweb -p 5901:80 --mode global --detach=false httpd

step 2: list service in all swarm nodes
1
$ docker service ps testweb

Stack Deployment
Stack Deployment:
Running a container  on different ports in a swarm
Step 1: Creating a DockerFile
1
2
3
$ mkdir Dockerfile
$ cd Dockerfile
$ vi Dockerfile
–> Copy the following code
#testFROM centos:7
LABEL maintainer “itzgeek.web@gmail.com”
RUN yum makecache
RUN yum install -y httpd
RUN echo “This Page Designed by ITzGeek, for Docker Build”
CMD [“apache2″,”-DFOREGROUND”]
CMD service httpd restart && /bin/bash
ENTRYPOINT service httpd restart && /bin/bash
ENTRYPOINT [ “/usr/sbin/httpd” ]
CMD [“-D”, “FOREGROUND”]
EXPOSE 80
–> Save and Close.
Check all images:
1
$ docker images
Step 2: Build a docker image using docker file
1
$ docker build -t myhttpd:v1 # its builds a docker image by using dockerfile version v1
Step 3: Run a container using custom image
1
2
$ docker run -d --name testweb -p 80:80 myhttpd:v1
$ elinks http://node       # it executes testweb on ip
Step 4: First we need to install docker compose in cluster
1
2
3
$ sudo yum install epel-release
$ sudo yum install python-pip                  #Pip is used to upgrade the service
$ sudo pip install docker-compose
–> Here docker compose installs with including pythonpackages
Step 5: Create a docker-compose.yml file
1
$ vi docker-compose.yml
–> Add the below content to the file
version: '3'
services:
  web:
     image: httpd:latest
     deploy:
          replicas: 3
     ports:
          - "6901:80"
web2:
    image: httpd:latest
    deploy:
        replicas: 3
    ports:
         - "6902:80"
–> Save & Close
Step 6: Stack deployment on nodes
1
2
$ docker-compose up --d    #It will up the containers that are running on the host
$ docker stack deploy --compose-file docker-compose.yml mycustomstack     #It will deploy the customimage on all the nodes in a swarm
Storage Volumes
Storage Volume:
To integrate the local host directory to the directory in the container
Step 1: Creating a local volume
1
2
3
4
5
6
$ docker volume ls
$ docker volume create my-mount    #It creates a volume
$ docker volume inspect my-mount |more      #to know in-detail of my-mount
$ cd var/lib/docker/volumes/my-mount/_data
$ Echo “hello from the host” &gt; hostfile.txt     #It creates a hostfile in _data directory
$ Echo “hello world” &gt; internal-mount
Step 2: Creating a Docker volume and mount to the local volume
1
2
3
4
$ docker service create --name testweb --p 80:80 --mount source=my-mount,target=/internal-mount --detach=false --replicas 3 httpd        #Here we are sending that volume tot the httpd inside docker internal mount path
$ docker exec -it httpd id /bin/bash
$ cd internal-amount
$ exit
Networking
Step 1: Search for the networks
1
$ docker ntework ls
Step 2: Run any container
1
$ docker run --name testweb --P 80:80 httpd      #Example:httpd
Step 3: To know on which port the httpd is running
1
$ docker ps a
Step 4: To know on which ip httpd/testweb is running
1
$ docker container inspect testweb |grep IP-Adress
Step 5: Check on the browser whether container is running or not
$ elinks http://IP:PORT
Step 6: Check on which ip network is running or to know particular container running on which IP
1
$ docker container inspect –format=”{{.NetworkSettings.Networks.bridge.IPAddress}}” testweb
Create a New Network:
Step 7: Creating a new network  on the name of devei0
1
2
$ docker network create -d bridge devel0
$ docker network ls    # shows the newly created network
Step 8: Now connect a container to a newly added network
–> This following command connects httpd/testweb to the new network
1
$ docker network connect --ip=IP-Address devel0 testweb    #Give  devel0 IP address here
Step 9: Check whether network is connected to a container or not
1
2
3
$ docker ps a
$ docker container inspect --format=”{{.NetworkSettings.Networks.devel0.IPAddress}}” testweb
$ elinks http://IP
If you want to disconnect from the network:
1
$ docker network disconnect bridge testweb
How to configure docker to use external DNS:
1
$ cat /etc/resolv.conf      #we see the IP-Address of DNS
–> Check the DNS IP in the Container
1
2
$ docker exec --it testweb /bin/bash
$ cat /etc/resov.conf
NOTE: resolv.conf only supports 3 DNS
To run on diff DNS IP’s:
–> Running container on different DNS servers
1
2
$ docker run --name testweb -- dns=8.8.8.8 --dns=8.8.4.4 httpd
$ docker exec --it testweb /bin/bash
Another Method to overwrite DNS:
1
$ sudo vim /etc/docker/daemon.json
{
“dns” = [“8.8.8.8”,8.8.4.4”]
}
–> Save and Close
1
$ sudo systemctl restart docker
You can find the total implementation of Docker associate preparation course in the following URLs:

No comments:

Post a Comment