Docker
Intro
- Docker provides containerization concept on which we can install any application.
- You install docker on your O/S, then create a container using docker image. This image will have all required softwares & tools as a package.
- Image can be uploaded or downloaded from docker hub, this image will have all required o/s & tools as a package, when you run this image it will create a container and install applications.
- Example: secretsanta
- The above is java based application, to run the this application,
- Install linux o/s,
- install jdk,
- open port 8080,
- install maven,
- run mavn package to build artifact(.jar)
- run command java-jar secretsanta-0.0.1-SNAPSHOT.jar or to run on different port java -Dserver.port=8999 -jar secretsanta-0.0.1-SNAPSHOT.jar.
- open browser and run http://ip:8080 or http://ip:8999
- Anyone want to access this application required to install the above all, instead create an image using docker file in which define all these componenets.
- updload this image to docker hub where customer will download and run. wen it run it will create a container and install all required components.
- It will create an environment on any o/s and run the application.
- Docker is used for "process Isolation", which means it removes any dependency and run on top of any host o/s, dependency includes guest o/s and not required licensing of guet o/s.
- it is faster and used less hardware resources.
- Application can migrate from one o/s to another o/s by simply copying application. suppose an application (oracle) is installed on windows and want to migrate to my SQL on linux which can be done by simply copying docker container (oracle) to docker mySQL on linux.
- Docker comes in 2 flavours.
- Docker CE (Community edition is free)
- Docker EE (Enterprise edition paid)
- code
- code
- code
- code
Docker Installation
Ubuntu
- $sudo su - (root user access)
- $sudo apt install docker.io
- $docker pull hello-world (only root user can access docker, assign permission to user)
- When you install docker a group is created with the name docker, whoever added to this group can run the docker commands, by default root user is added.
- When you create a user a group of is also created with the same name of user, this group is primary group to this user. you can add additional group to this user
- $sudo usermod -aG docker abdul (-a is used to assign as a secondary group, -A is use for primary group, G is used for group, docker is group, abdul is user), lof off and log on. or run $newgrp docker (it will add user to docker)
- code
Windows
User Permission
code
code
code
code
Docker Container, Images, commands
Container
Images
- $docker images (list of images in local repository )
- $$docker pull sonarqube:lts-community (imagename:tag, where tag is used for version, it will check the local repository and if not found then go to docker repository)
- code
- code
Host
Client
Commands
-
Create Container |
$docker run -d --name xyz -p 9000:9000 image_name:tag |
create container using image in detach mode, using port 9000 with define version, two ports defined, first port is container port and second port is host port, container is installed within host docker. |
options used with run command |
|
--name |
give a name to container |
-it |
open interactive terminal in the container |
-d |
running container in detached/background mode |
-e |
pass an environment variable |
tag |
version of image |
-v |
attach an external directory or device as a volume |
--volume-from |
sharing volumes between containers |
-rm |
delete a container on exit |
-p |
port mapping , container port with host port, ex -p 8080:80 (8080 is host port) |
-P |
capital P, used for automatic port mapping, container port mapped with host port greater than 30000 |
--link |
linking of containers |
ctrl+p, ctrl+q |
To come out of a running container without exit |
$docker container ls or docker ps |
list of running container |
$docker container ps -a |
List of all containers including running and stopped container |
docker start cont_name / cont_id |
To start a stopped container |
docker stop cont_name / cont_id |
To stop a started container |
docker stop $(docker ps -aq) |
To stop all running containers |
docker restart cont_name / cont_id |
To restart a container |
docker restart -t 10 cont_name / cont_id |
To restart after 10 seconds |
docker rm cont_name / cont_id |
To delete a stopped container |
docker rm -f cont_name / cont_id |
To delete a running container forcefully |
docker rm $(docker ps -aq) |
To delete all stopped containers |
docker rm -f $(docker ps -aq) |
To delete all running and stopped containers |
docker inspect cont_name / cont_id |
To get detailed information about container |
docker logs cont_name / cont_id |
To get logs of a container |
docker port cont_name / cont_id |
To view ports open on a container |
|
|
|
|
|
|
Image |
docker image --help |
help on image command |
docker pull Image_name:tag |
Download a docker image |
docker search image_name |
search a docker image |
docker list images docker image ls |
List of all docker images |
docker push image_name docker push image_Id |
Upload docker image |
docker rmi image_name |
Delete a single docker image |
docker system prune -a |
Delete all images |
docker commit container_name/container_id image_name |
To create a docker image from container |
docker build -t image_name . |
To create a docker image from dockerfile |
docker inspect image_name |
get detailed information of image |
docker image save image_nae tarfile_name |
To save an image as tar file |
docker image load tarfile |
to extract an image from tar file |
|
|
|
|
|
- code
- code
Daemon
code
dockerfile
code
code
code
code
code
code
code
5
code
code
code
code
code
code
code
Projects
Secretsanta: create an image of secretsanta java based application so it can be run on any environment(windows, linux, mac etc)
- Create Environment:
- create ubuntu VM
- Install jdk:
- Install docker:
- $sudo su - (root user access)
- $sudo apt install docker.io
- $docker pull hello-world (only root user can access docker, assign permission to user)
- When you install docker a group is created with the name docker, whoever added to this group can run the docker commands, by default root user is added.
- When you create a user a group of is also created with the same name of user, this group is primary group to this user. you can add additional group to this user
- $sudo usermod -aG docker abdul (-a is used to assign as a secondary group, -A is use for primary group, G is used for group, docker is group, abdul is user), lof off and log on. or run $newgrp docker (it will add user to docker)
- code
- code
code
code
code
code
code
code
Docker Hub Repository
Public Repostiory
- Create an account and upload your images, it does not required password and anyone can download and use it.
- seach any sort of images by visiting docker hub/explore and search.
- pull image: $docker pull sonarqube:lts-community (imagename:tag, where tag is used for version, it will check the local repository and if not found then go to docker repository)
Private Repository
- Create an account and upload your images, it required username and password to download images.
- code
- code
alpine Repository
- In this repository images are in very low size while size of official images are very big.
- code
- code
code
code
code
code
8
code
code
code
code
code
code
code
9
code
code
code
code
code
code
code
10
code
code
code
code
code
code
code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
- code
intro, |
hub.docker.com (username = aziz27uk or aziz27uk@yahoo.co.uk, pass : Y......! |
Docker installation: Windows, Linux, user permission, |
Link two containers |
Docker Host, Client, Images, Container, Daemon, Commands, |
wordpress and mysql link |
create container tomcat, jenkins, ubuntu, nginx, httpd, mysql, |
CICD tier3 architecture (jenkins, tomcat, tomcat) (dev, test, prod) |
multi container link, --link, Docker Compose, |
jenkins master and slave, |
Deploy tomcat with docker compose, |
LAMP Architecture, |
Deploy wordpress and mysql with docker compose, |
Testing environment with selenium hub, |
Deploy jenkins master / slave with docker compose, |
Simple Docker Volume, Sharing docker volume/volume container, |
LAMP Architecture with docker compose, |
volume cotainer, |
CICD environment with docker compose, |
docker custom image: docker commit, docker file, scenario1, scenario2, scenario3(cache Busting), scenario4, |
Docker Registry, |
Image Layers, |
Docker swarm, |
|
Docker Introduction:
- Docker can be installed on
- Docker desktop for Windows (windos 10 prof 64 bit, windows 2016 server edition): https://docs.docker.com/docker-for-windows/install/
- Once docker is installed it activates hyper-v and you cannot run another hypervisor.
- once it is installed use powershell to run docker commands
- Linux:
Docker Installation on Windows:
- Docker desktop installation on Windows 10 pro:
- Docker installation on Windows 2016 server:
Docker installation on Linux: Ubuntu (www.get.docker.com)
- Create Ubuntu instance (t2 micro)
- $sudo su - (root user access)
- #curl -fsSL https://get.docker.com -o get-docker.sh #ls
- #sh get-docker.sh
- #docker --version
User permission to access docker: Assgine a user permission to access docker or run docker commands.
- create a user and set password and PasswordAuthentication set to yes
- create a group #sudo groupadd docker
- add user to the group #sudo usermod -aG docker usernam
Docker Images:
- It is a software given by docker, It is a combination of binaries and libraries which are required for an application/software to run. Docker has packaged important/required binaries and libraries of software and called it as docker image. You install that image in a container
Docker Container:
- Docker image installed in a container to access. you can create n number of containers from image.
- After required customization and configuration of container, you can create custom image.
Docker Host: The host o/s on which docker is installed is called docker host.
Docker Client:Terminal which is used to access docker is called docker client, when you install docker a docker client is also installed and it runs in the background, which is responsible for taking commands and pass it to another background process called daemon.
Daemon: Daemon will analyse the type of command and route it to the following.
-
- Docker Images
- Docker Containers
- Docker Registry: it is where docker images are saved.
- public registry: Maintained by hub.docker.com
- private registry: it is private in nature
Docker Commands:
Container |
docker run image_name |
To create a container |
options used with run command |
|
--name |
give a name to container |
-it |
open interactive terminal in the container |
-d |
running container in detached mode |
-e |
pass an environment variable |
-v |
attach an external directory or device as a volume |
--volume-from |
sharing volumes between containers |
-rm |
delete a container on exit |
-p |
port mapping , container port with host port, ex -p 8080:80 (8080 is host port) |
-P |
capital P, used for automatic port mapping, container port mapped with host port greater than 30000 |
--link |
linking of containers |
ctrl+p, ctrl+q |
To come out of a running container without exit |
docker container ls |
list of running container |
docker container ps -a |
List of all containers including running and stopped container |
docker start cont_name / cont_id |
To start a stopped container |
docker stop cont_name / cont_id |
To stop a started container |
docker stop $(docker ps -aq) |
To stop all running containers |
docker restart cont_name / cont_id |
To restart a container |
docker restart -t 10 cont_name / cont_id |
To restart after 10 seconds |
docker rm cont_name / cont_id |
To delete a stopped container |
docker rm -f cont_name / cont_id |
To delete a running container forcefully |
docker rm $(docker ps -aq) |
To delete all stopped containers |
docker rm -f $(docker ps -aq) |
To delete all running and stopped containers |
docker inspect cont_name / cont_id |
To get detailed information about container |
docker logs cont_name / cont_id |
To get logs of a container |
docker port cont_name / cont_id |
To view ports open on a container |
|
|
|
|
|
|
Image |
docker image --help |
help on image command |
docker pull Image_name |
Download a docker image |
docker search image_name |
search a docker image |
docker list images docker image ls |
List of all docker images |
docker push image_name docker push image_Id |
Upload docker image |
docker rmi image_name |
Delete a single docker image |
docker system prune -a |
Delete all images |
docker commit container_name/container_id image_name |
To create a docker image from container |
docker build -t image_name . |
To create a docker image from dockerfile |
docker inspect image_name |
get detailed information of image |
docker image save image_nae tarfile_name |
To save an image as tar file |
docker image load tarfile |
to extract an image from tar file |
|
|
|
|
|
Create Container: Two step process, one step process
Tomcat installation: two step process
- Step1: pull image : search required image on hub.docker.com and get image name.
- #docker pull tomee (if version is not defined, it will download the latest version)
- location of image on docker host: /var/lib/docker/image/overlay2/imagedb/content/sha256/
- #docker image ls or #docker images
- Step2: run image:
- #docker run --name mytomcat -p 7070:8080 tomee (container mytomcat will be created with custom port defined 7070)
- docker container will be creatd but terminal will be busy in generating logs. open another terminal to run commands.
- Access tomcat http://publicIP:7070
- #docker container ls (running container will display)
- #docker ps -a (all stop and running container displayed)
- # docker stop mytomcat (stop container)
- #docker rm mytomcat (delete container)
- #docker run --name mytomcat -p 7070:8080 -d tomee (-d =detach mode, terminal will be free to use)
Jenkins:
Step1: #docker pull jenkins/jenkins (The jenkins image has been deprecated for over 2 years in favor of the jenkins/jenkins:lts image provided and maintained by the Jenkins Community as part of the project's release process. Use image:>docker pull jenkins/jenkins>
docker run -p yourportNo:8080 --name=jenkins-master -d jenkins/jenkins)
Step2: #docker run --name myjenkins -p 9090:8080 -d jenkins/jenkins
Step3: access jenkins: http://publicIP:9090
Step4: Recover password : more details click here.
- Get into jenkins and get bash prompt as you are in docker
- #docker exec - it myjenkins bash
- #cat /var/jenkins_home/secrets/initialAdminPassword (password will be displayed)
- You can acces jenkins home directory either mapping jenkins home directory (/var/jenkins_home) to your machine's local file system or
- specify --volume option in the run command (--volume jenkins-data:/var/jenkins_home), can access through terminal or
- run #docker container exec -it containername bash and then go to /var/jenkins_home/secrets and run cat initialAdminPassword.
One Step Process: If image is not present locally then it download image and create container.
Ubuntu: -it (interactive mode) can be used with o/s, it creates ubuntu container and get into ubuntu terminal.
- #docker run --name myubuntu -it ubuntu:version
- Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu (hub.docker.com)
- ubuntu terminal : root@241c229d7296:/#
nginx: create container and assing port automatically using -P (capital P) in detach mode and pull image. Port mapping is done for appications which can be access through browser.
- #docker run --name mynginx -P -d nginx (docker will do port mapping of default port to any port >30,000, if you use small p then define port number of your choice)
- docker container ls (take default port number )
- Access: http://publicIP:49153
httpd: Apache Webserver
- #docker run --name webserver -P -d httpd (docker will do port mapping of default port to any port >30,000)
- #docker container ls (get the mapped port number)
mysql: It required environment varibale
- #docker run --name mysql -d -e MYSQL_ROOT_PASSWORD=India123 mysql:5 (it download the image mysql version 5 and create container)
- To open interactive terminal in bash
- #docker exec -it mysql bash
- you will be in mysql container and to connect to mysql database
- #mysql -u username -p
- you wil get mysql prompt
- mysql> show databases; (it will show databases information_schema, mysql, performance_schema, sys)
- mysql> use sys;
- create a database (google.com search emp and dept table for mysql (https://justinsomnia.org/2009/04/the-emp-and-dept-tables-for-mysql/) copy code
- paste it so it will create two tables.
- mysql> select * from emp;
- mysql> select * from dept;
Multi container link: Linking between containers can be done by
- docker --link
- docker compose
- docker networking
- python script
docker run --link: create two containers (busybox, it is an linux o/s flavour) and create link between them.
Scenario 1:
- Step1: #docker run --name bb1 -it busybox
- to come out of interactive terminal without exit/stop container, ctrl p than ctrl q
- Step2: #docker run --name bb2 -it --link bb1:bb1link busybox
- ping bb1 (You are terminal of bb2 and pinging to bb1)
- To ping bb1 to bb2
Scenario 2: create a Development Environment for a wordpress container with mySQL database,
- create 2 containers one of mySQL and another wordpress, create link betwen them,
- wordpress is a php based application used by developers to create a website and it is integrated with database to store clients input.
- Step1: #docker run --name mysql -d -e MYSQL_ROOT_PASSWORD=India123 mySQL:5
- hub.docker.com search mysql, click mysql and in description check einvirnment variable defined.
- click tag to get the versions of mySQL, in the above we are using tag/version 5
- Step2: Get into mySQL container:
- #docker exec -it mysql bash
- Step3: connect to mySQL (check description in mySQL for commands)
-
- #mysql -u username -p
- you wil get mysql prompt
- mysql> show databases; (it will show databases information_schema, mysql, performance_schema, sys)
- mysql> use sys;
- create a database (google.com search emp and dept table for mysql(justinsomnia) and get the code
- copy the emp and dept code and paste it so it will create two tables.
- mysql> select * from emp;
- mysql> select * from dept;
- Step4: create wordpress container and linked with mysql (define port as wordpress can be accessed on browser)
- #docker run --name starwordpress -d -p 9090:80 --link mysql:mysqldatabase wordpress
- Step5: access wordpress in any browser http://publicIP:9090
- database information provide
- submit
Scenario3: Create CICD 3 tier architecture environment in docker container.
- Step1: create jenkins container
- #docker run -- name development -d -p 5050:8080 jenkins/jenkins
- check any browser http://publicIP:5050
- to get password, get into jenkins
- #docker exec -it development bash
- cat /var/jenkins_home/secrets/initialAdminPassword
- Step2: create tomcat container and linked with jenkins (development)
- #docker run --name testing -d -p 6060:8080 --link development:jenkins tomcat:9.0
- Step3: create tomcat container and liniked with jenkins
- #docker run --name production -d -p 7070:8080 --link development:jenkins tomcat:9.0
- #docker container ls (3 conainers running, 1 jenkins and 2 tomcat)
- check testing server on any browser http://publicIP:6060
- check testing server on any browser http://publicIP:7070
Error: Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists or 404 error
Resolve:
- Get into tomcat container #docker exec testing -it bash
- #cd /usr/local/tomcat
- #cp -r webapps.dis/* webapps
- Now access the tomcat in the browser.
Scenario 4: Create jenkins master and jenkins slave: Refer here
- Step1: #docker run --name master -d -p 6060:8080 jenkins/jenkins
- Step2: #docker run --name slave -it --link master:jenkins ubuntu
- Step3: download slave.jar file from master.
- #wget master:6060/jnlpJars/slave.jar (wget command not found) install wget
- #apt -get update
- #apt-get install wget
- #wget master:8080/jnlpJars/slave.jar
- Step4: Login to Jenkins and install plugin docker
- manage jenkins > manage plugin > Available > docker
LAMP Architecture:
- LAMP architecture environment can be created for developers who are building a website using open source technologies.
- L = Linux o/s
- A = Application Development using php
- M = backend database should run mySQL
- P = Application server run on Apache Tomcat.
- On Linux machine install mySQL, php and tomcat containers and linked with each other.
LAMP Architecture Lab:
- Step1: Install Linux ubuntu instance
- loginto ubuntu instance and install docker
- Step2: create mySQL container
- Step3: create tomcat container and linked with mySQL
- Step4: create php container and linked with mySQL & tomcat
- #docker run --name myphp -d --link mydb:mysql --link apache:tomcat php:7.2-apache
- #docker container ls (3 containers running)
Testing environment with selenium hub: Create selenium hub container, and link it with two node containers(chrome and firefox etc..).
- Testers should be able to run selenuim automation programs for testing the application on multiple browsers.
- hub.docker.com and search selenium/hub image and click on selenium/hub link to get more details.
- # docker run --name hub -d -p 4444:4444 selenium/hub
- In hub.docker.com search selenium/node-chrome-debug ( It is ubuntu container with chrome)
- # docker run --name chrome -d -p 5901:5900 --link hub:selenium selenium/node-chrome-debug
- In hub.docker.com search - selenium/node-firefox-debug ( It is ubuntu container with firefox)
- # docker run --name firefox -d -p 5902:5900 --link hub:selenium selenium/node-firefox-debug
Note: Containers with firefox and chrome are GUI containers. To see the GUI interface to chrome / firefox containers install VNC viewer and access these containers with hostpublicIP:port
- Download and install vnc viewer
- public_ip_dockerhost:5901 for chrome and public_ip_dockerhost:5902 for firefox
Docker Compose:
- Creating multi container architecutre using YAML file in docker compose. To configure/deploye multiple containers and linked each other in one attempt, can be peformed using YAML file. you can define all commands in yaml file and execute.
- Intstall docker compose in docker host: https://docs.docker.com/compose/install/
- # curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- # chmod +x /usr/local/bin/docker-compose
- #docker-compose --version
Deploy a tomcat container with docker compose:
- #vim docker-compose.yml (if you create with other name aziz.yml)
-
--- version: '3' services: mytomee: image: tomee ports: - 5050:8080 ... |
- To check above defined keypair value is correct, go to http://www.yamllint.com
- #docker-compose up -d
- #docker-compose -f aziz.yml up -d (if you have created with other name )
- #docker-compose -f aziz.yml down (delete containers)
Deploy a mysql and wordpress and link each other with docker compose:
- #vim wordpress.yml
-
--- version: '3' services: mydb: image: mysql:5 environment: MYSQL_ROOT_PASSWORD: India123
mywordpress: image: wordpress ports: - 6060:80 links: - mydb:mysql ...
|
- #docker-compose -f wordpress.yml up -d (it will deploy mysql and wordpress and linked each other)
- http://publicIP:6060 (access wordpress)
- #docker -exec -it root_mydb_1 bash (check container name )
- #docker-compose -f wordpres.yml down (delete all containers)
Depoy Master / Slave of jenkins through docker-compose:
- #vim masterslave.yml
- stdin_open: true and tty: true (ubuntu will not be exited)
-
--- services: master: image: jenkins/jenkins ports: - "5050:8080" slave: image: ubuntu stdin_open: true tty: true version: "3"
|
- Access jenkins with publicIP:5050
- Retrieve Administrator pasword:
- #docker exec -it root_master_1 bash
- #cat /var/jenkins_home/secrets/initialAdminPassword
- Passwordless connectivity between master and slave.
Deploy LAMP architecture with docker compose:
L: Linux
A: Appliction Development( php)
M: Backend Database (MySQL)
p: Application Server (Apache Tomcat)
Linux is already installed on AWS instance or docker host, install docker and docker compose on docker host, create a yaml file to deploy php, mysql and apache tomcat.
#vim lamp.yml
--- version: '3'
services: mydb: image: mysql:5 environment: MYSQL_ROOT_PASSWORD: India123
apache: image: tomee ports: - 6060:8080 links: - mydb:mysql
php: image: php:7.1-apache links: - mydb:mysql - apache:tomcat ...
|
- #docker-compose -f lamp.yml up -d
Deploy CICD environment using docker compose where jenkins container linked with two tomcat containers.
Docker volume:
- Simple Docker Volume
- Docker volume container (sharable)
Simple Docker Volume:
- Data of docker container can be store on docker host and can retrieve data after deleting container.
- Step1: create a folder on root:
- #mkdir /dockervolume (it will be a mount point not a location of data store on docker host, like in windows when you attach a usb you get drive D, E etc.. and when you remove usb and attach another usb you get the same drive d or e, so this folder dockervolume is like drive and data storing could be in different location which can be retrieved from docker inspect command.
- Step2: create a container and attach volume
- #docker run --name myubuntu -it -v /dockervolume unubtu
- ctrl p ctrl q
- Step3: logon to ubuntu container and create some files in the mounted /dockervolume. only data store in volume folder will be saved.
- #touch aziz11 aziz12 aziz13 aziz14
- Step4: docker container inspect myubuntu
- locate mount and copy location. data is stored in this location
- Step5: Delete container and go to above location and you can find data files.
Sharing the volume between different containers: we have created docker volume and it will shared with other containers
- #docker run --name myubuntu1 -it --volumes-from myubuntu ubuntu
- #cd dockervolume
- #ls (you will see files aziz11 aziz12 aziz13 aziz14)
- create new files #touch abdul11 abdul12 abdul 13 abdul14
Create another docker container sharing the same volume:
- #docker run --name myubuntu2 -it --volumes-from myubuntu1 ubuntu
- #cd dockervolume
- #ls (you will see files aziz11 aziz12 aziz13 aziz14, and abdul11 abdul12 abdul 13 abdul14)
- #touch mohammed11 mohammed12 mohammed13 mohammed14
connect to myubuntu or myubuntu1 or myubuntu2 and you will get all files in the volume as volume is shred among them, and you will see all files aziz11..... abdul11.....mohammed11...... these files are stored in /ubuntuvolume and will be available in all shared volume.
- #docker container attach containerID (make sure you are in /)
- #cd /dockervolume
- #ls (al files is there aziz11..... abdul11.....mohammed11......)
Before deleting docker containers get the path from myubunt as its volume has been shared, By using path you can retrieve data.
Create volume container/sharable volume container:This volume should be attached with container which has some data.
- #docker volume create myvolume
- #docker inspect myvolume
- "Mountpoint": "/var/lib/docker/volumes/myvolume/_data", (location)
- copy/create any file in the myvolume location
- create a tomcat container and attach myvolume in /temp folder.
- #docker run --name mytomee1 -d -P -v myvolume:/tmp tomee
- #docker exec -it mytomee1 bash
- #cd /tmp
- #ls
- #docker volume rm myvolume (to delete volume)
- #docker volume prune (to delete all volumes)
Docker custom image: Custom image can be created with the following methods:
- Docker commit command
- Docker file
Docker Commit: You create an image of an existing docker container with all software/application installed so it can be used later to create container.
- Step1: Create a container:
- #docker run --name myubuntu -it ubuntu
- #apt-get update
- #apt-get install git -y
- #git --version
- Step2: create an image/snapshot of the above container myubuntu
- docker commit myubuntu ubuntugit (image of myubuntu will be created with the name ubuntugit, this image has git )
- #docker images (ubuntugit image is created)
- Step3: create a container using ubuntugit image.
- #docker run --name starubuntu -it ubuntugit
- #git --version (git is installed in the image)
Docker file: It is a smiple text file in which you define the following keywords(case sensitive):
- FROM: Used to specify the base image from which the docker file has to be created.
- MAINTAINER: This represents name of the organization or the author who created this docker file.
- CMD: This is used to specify the initial command that should be executed when the container starts.
- ENTRYPOINT: Used to specify the default process that should be executed when container starts. It can also be used for accepting arguments from the CMD instruction.
- RUN: Used for running linux commands within the container. It is generally helpful for installing the software in the container.
- USER: Used to specify the default user who should login into the container.
- WORKDIR: Used to specify default working directory in the container.
- COPY: Copying the files from the host machine to the container.
- ADD: Used for copying files from host to container, it can also be used for downloading files from remote servers.
- ENV: Used for specifying the environment variables that should be passed to the container.
- EXPOSE: Used to specify the internal port of the container.
- VOLUME: Used to specify the default volume that should be attached to the container.
- LABEL: Used for giving label to the container.
- STOPSIGNAL: Used to specify the key sequences that have to be passed in order to stop the container.
Scenario1: create a custom image of nginx.
- Create a dockerfile by taking nginx as the base image(image from docker hub) and specify the maintainer as aziz.
- $ sudo su -
- # vim dockerfile
-
FROM nginx MAINTAINER aziz |
- :wq!
- Construct an image from the above dockerfile.
- #docker build -t aziznginx . ( t stands for tag, . stands for current working dir aziznginx is the new image name )
Scenario2: create a custom image of ubuntu and git
Scenario3: cache busting
- Whenever an image is build from a dockerfile, docker reads its memory and checks which steps/instructions were already executed. These steps will not be re-executed.
It will execute only the new instructions. This is a time saving mechanism provided by docker.
- The disadvantage is that as it will not re execute previous steps so any updated package will not be run so we can end up installing software packages from a repository which is updated long time back. (we run apt-get update before installing any package so it will not be updated).
- && will be used re execute previous installed package.
create a docker file and define code to create a ubunutu container, update repository and install git.
FROM ubuntu MAINTAINER logiclabs RUN apt-get update RUN apt-get install -y git |
- build image: #docker build -t myubuntu .
- All steps 1,2,3,4 are performed by pulling details from docker hub.
- Amend docker file and add RUN apt-get install tree
FROM ubuntu MAINTAINER aziz RUN apt-get update RUN apt-get install -y git RUN apt-get install tree |
- build image: #docker build -t myubuntu1 .
- Observe the output, Step 2, 3, 4 is using cache. Only step 5 is executed freshly.
- suppose if you are installing tree package after few months then it is drawback that it will not update repository and install tree on previously updated repository. To avoid this we use && and define in the dockerfile whichever step you want to execute again.
FROM ubuntu MAINTAINER aziz RUN apt-get update && apt-get install -y git tree |
- build image: #docker build -t myubuntu2 .
Scenario4: for CICD environment we install JAVA, jenkins, Git and Maven for java based project, JAVA is mandatory for jenkins but git and maven is the requirement for the code.
- #docker run --name myjenkins -d -P jenkins (it will create jenkins container)
- you can access jenkins home page using publicIP and port number, to get password run exec in bash terminal
- now git and maven is not installed in the container, if you try to install it you will get error do not have permission, if you use sudo you get error sudo command not found.
- To run sudo command user must be in visudo (sudoers ) file. #whoami (add user to sudoers file).
- visudo (error command not found)
- #su - root
- prompt for password for root (password is not set)
- exit to come to docker host (aws instance)
Image Layers:Docker Images are the combination of layers, when you pull an image from docker hub a number of layers downloaded.
- When ou first pull an image in a fresh aws instance a number of layers will be downloaded.
- When you pull second image less number of layers will be downloaded and it goes on further pulls, if layers of first pull image is simlar in second pull image then it will not download in second image and it will only download new layers of second image.
- When you delete first image it will only delete those layers which are not dependant to other images.
Docker Registry: Registry is a location where docker images are saved.
- Public Registry : hub.docker.com
- Private Registry: it is paid version registry.
Create a customize image from container running ubuntu and install git, create an image from container and uploaded into public registry.
- #docker run --name myubuntu -it ubuntu
- #apt-get update
- #apt-get install git
- #exit
Convert container into an image.
- #docker commit myubuntu aziz27uk/starubuntu (image name should start with userID of docker hub, image name is aziz27uk/starubuntu)
- Image is available in docker host which need to upload into docker hub.
- Login to docker hub from terminal.
- #docker login (enter login name and password)
- #docker push aziz27uk/starubuntu
- login to docker hub and check your images in repositories.
Docker Container Orchestration (Docker Swarm):
- It is the process of running docker containers on multiple docker host machines in a distributed environment.
- A single service runs on all containers but thier host machines could be different.
- Docker swarm is the tool used for performing container orchestration.
- create 4 instances and install docker in each machine.
- # curl -fsSL https://get.docker.com -o get-docker.sh
- # sh get-docker.sh
- Change hostname of all machines
- #hostnamectl set-hostname newname
- #bash
- or
- #vim /etc/hostname (remove ip address and enter name)
- #init 6 (restart )
- Initialize docker swam service on Manager machine
- docker swarm init --advertise-addr 172.31.42.135
- it will generate a token which need to run on every worker machine.
- docker swarm join --token SWMTKN-1-27lf3n7xxqy2u3gb61mvfybk51uqjq9hj4m5uwdd4lcgtgafth-0hmrtwzrcyqv4h7cl3euekq68 172.31.44.50:2377
- on manager run : #docker node ls