Docker
=============
It is a platform for implementing containerization
Virtualisation
In virtualisation we have a bare metal (H/w) on which we install our host os. on this host os we install hypervisor (vm ware ,oracle virtual box etc).
On the hypervisor we install our guest os and on these guest os we can install our applications. The disadvantage of the above is architectureis the applications running on guest os has to pass through multiple layers in order to accessthe hardware resources.
This will result in downgraded performance of the applications
Containers
===============
In docker we have a bare metal on which host os is installed and on the host machine we install a s/w called docker engine.
on the docker engine we can install any number of docker containers. These containers are not allocated any fixed amount of hardware resources.
When the containers are running docker engine decides how much hardware shouldbe allocated for each container depending on the processes running in the container Docker performs process isolation and enbles it to run seperately in userspacewithout bothering about the underlying hardware and os.
Installing docker on linux
========================
1. open http://get.docker.com
2. Copy the first 2 commands and paste them in a linux terminal
curl -fsSL get.docker.com -o get-docker.sh
sh get-docker.sh
3. To check the version of docker running on our machine
docker –version
Installing docker on windows
==============================
1. Open
https://docs.docker.com/engine/installation/#supported-platforms
2. Go to desktops section
3. Install docker for windows
Note: docker for windows activates an application called Hyper-V.
If this hyper-v is active it will not allow any virtualization software to run (vm ware, virtual box)
Docker Images and Container
An image is a collection of bin/lib that are necessary for an application to run. All the docker images are present in the cloud site of docker called
hub.docker.com
A Container is a running instance of the image. It is the individual application or process that docker has created in our user space
Docker Components:
———————
Dcoker Host: This is the machine where docker is installed and all the docker images are downloaded.
Docker Client: This is the terminal of docker where we can fire the docker commands
Docker Daemon: This is a background process which takes the commands fired by docker client and sends them to the docker images or container’s or docker registry
Docker Registry: This is the cloud site of docker where all the docker images are present.
hub.docker.com
Docker hub is cloudbased registry where we can have docker images.we can pull are push the images and we can create the repositories.
We can also create a private registry which can be accessed only by our organisation
Important Docker commands
—————————-
Docker image commands:
1. To see all the images present in our docker host
$ docker images or docker image ls
2. To search for images on hub.docker.com
$ docker search imagename
3. To download an image from docker hub
$ docker pull imagename
4. To remove an image from docker host
$ docker rmi imagename
5. To upload an image into docker hub First login into dockerhub docker login
$ docker push imagename
6. To rename a docker image
$ docker tag image1 image2
Dokcer Container commands:
7. To see the list of running container
$ docker container ls
8. To see the list of all container(running and stopped)
$ docker ps -a
9. To find detailed info about a container
$ docker inspect containername/containerid
10. To stop a container
$ docker stop containername/containerid
11. To start a container
$ docker start containername/containerid
12. To restart a container
$ docker restart containername/containerid
To restart a container after 20 seconds
$ docker restart -t 20 containername/containerid
13. To initialise a container
$ docker run imagename
Run command options:
-it Opens an interactive terminal in the containe where we can fire linux commands.
-d Start the container in detached mode as a background process(deamon).
-p Used for port mapping it it will bind the port of docker host with the port of the container.
Dcoker host port is called external port, container port is called internal port .
Eg: -p 8080:80 Here 8080 is external port ie docker host port 80 is internal port ie docker container port -P Will publish the port numbers.ie docker will map the internal port of the container with an external port on the docker host and this external port will be some number greater than 30000
-v Used for mounting volumes into the container –volumes-from Used for creating reusable volumes that can be shared between multiple containers
–network Used for associating a container with a specific network
–name Used for assigning a name for a container
-e Used for passing environment variables to the containers
-rm Used for deleting the container on exit from the container
14. To Remove a stopped container
$ docker rm containername/containerid
15. To remove a running container forcebly
$ docker rm -f containername/containerid
16. To stop all running container
docker stop $(docker ps -aq)
17. To remove all stopped containers
docker rm $(docker ps -aq)
18. To remove all running and stopped containers
docker rm -f $(docker ps -aq)
19. To find the logs generated by a container
$ docker logs containername/containerid
20. To find the ports used by a container
$ docker port containername/containerid
Docker Networking commands:
====================
21. To see the list of docker networks
$ docker network ls
22. To create a new network
$ docker network create networkname
23. To remove a network
$ docker network rm networkname/networkid
24. To find detailed info about a network
$ docker network inspect networkname/networkid
25. To attach a running container to a network
$ docker network attach networkname/networkid containername/containerid
26. To dethatch a container from a network
$ docker network disconnect networkname/networkid containername/containerid
27. To open a terminal in an already running container
$ docker exec -it containername/containerid bash
28. To go into a container which has moved into background
$ docker attach containername/containerid
UseCase1:
Start a ningx container, run in detached mode(daemon),give it a name webserver and it should run on external port 8080 and internal port 80
$ docker run -d –name webserver -p 8080:80 nginx
The above commands checks for nginx in our machine (docker host).
If it is present it will start it. if it is not present it will download from docker hub and start it it as a containerit will assign a name webserver and also run it in detachedmode ie as a deamon. external port 8080 of the host machine is mapped with internal port 80 of the container To see the home page of the docker container
launch any browser localhost:8080
usecase2:
Start a httpd container, run in detached mode (deamon), give it a name mywebserver and it should run on external port 9090 and internal port 80
$ docker run –name mywebserver -p 9090:80 -d httpd
usecase3:
start an ubuntu container and name it myubuntu. go into the ubuntu containers terminal and fire some linux commands. exit from the container
$ docker run –name myubuntu -it ubuntu
In the container fire linux commands ls etc exit Start mysql as a container. use exec and go into the bash shell of the mysql container. login into the mysql db and create some tables
$ docker run -d -p 3308:3306 -e MYSQL_ROOT_PASSWORD=******** — name mydb mysql
To go into the running mysql container
$ docker exec –it mydb bash
In the container shell to login into db
mysql -u root –p enter password Fire sql queries show databases;use sys;
Create emp and dept table by copying the code from
https://justinsomnia.org/2009/04/the-emp-and-dept-tables-for-mysql/
Customizing docker images:
============================
We can create or customize docker images in the following 2 ways
1. Download a base image and install some software’s in it and save it(commit)
2. Use dockerfile
Use case:
Run ubunutu as a container and name it myubuntu. Go into the containers terminal and install some packages. exit from the container and save it as an image. Use this image for creating more containers and all these new containers should all the software we have installed
$ docker run –name myubuntu -itubuntu
Data Volume
===============
By default the data inside the containser is not persistence once we can remove are crashed we cant persistent storage we cant get by aataching.
$ docker run -d\ –name=ngnix\ –mount source=ngnix -vol,destination=/usr/share/ngnix/html\ngnix:latest
Docker Volumes
Data volume’s can be implemented in 2 types
1. data volumes
2. data volume containers
To implement a data volume we should first create a directory Outside the container and mount that directory inthecontainer
UseCase
Create a directory called /data.Start an ubuntu container and mount it on the /data folder.Create some files in this /datafolder fom within the container and check if those are available even after removing the contianer.
1 $ mkdir /data2 $ docker run -it –name myubuntu -v /data ubunutu
In the ubuntu container ls we should find data folder cd data create some files in data folder touch file1 file2 file3 exit3
To find the location where the data created in container is stored
$ docker inspect myubuntu
The above command will display detailed info about the container in json file format Search for “mounts” in that file and it will show the path where /data is saved
4 .remove the container
$ docker rm -f myubuntu
5. cdpath_of_data_copied_from_docker_inspectcommand 6 we will find ALL the files created by the container
Note: The data created by the previosly deleted container can be accessed only by the host machine and not other containers.If we want other containers to access that data we should usethe concept of data volume containers
Implementing CI-CD using docker
Start a jenkins container and name it jenkinsserverstart 2 tomcat container and name them qaserver andprodserver. Link both these tomcat containers with jenkins server.Implement ci-cd
docker run -p 8080:8080 -p 50000:50000 –name jenkinsserver -d jenkins
docker run -p 8888:8080 –name qaserver -d –link jenkinsserver:jenkins tomcat
docker run -p 9999:8080 –name prodserver -d –link jenkinsserver:jenkins tomcat
Docker Compose
===============
To start multiple containers and link them with each other we have to run multiple docker commands. Instead we can place all the contianers info in one file called as docker comopse file and when we start this compose file it will start all the containers specified in that file
https://docs.docker.com/compose/install/#install-compose
Click on linux tab Copy and paste the below 2 commands
$ sudo curl -L https://github.com/docker/compose/releases/download/1.17.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker compose
To check if docker compose is installed or not
$ docker-compose –version
Docker file:
This is a text file whcih uses docker commands for customising the docker images
Keywords in docker file
FROM This is used to specify the base image from which we are creating our images.
MAINTAINER The name of the person who is creating this image .
CMD Used for firing linux commands when a container starts.
ENTRYPOINT Used to pass arguemtns to CMD in docker file .
RUN Used for installing, unistalling or updating softwares in docker images .
COPY Used to copy files from docker host to the container.
EXPOSE Used for port mapping ie exposing internal port of docker to external port of host machine
USER Used to specify the the user previlages to run a contaienr .
VOLUME Used for attaching external volumes
ENV Used for passing environment variables.
Usecase1
Docker file to create an ubuntu image and execute a linux command whenever we start the container
vim dockerfile
FROM ubuntu
MAINTAINER name
CMD [“date”]
CMD [“ls”,”-la”]
Save and quit (Esc :wq)
To build a docker image from the above file
$ docker build -t myubuntu .
Usecase2:
Create a docker file for using entry point to display the content of /etc/passwd file when the container starts
vim dockerfile
FROM ubuntu
MAINTAINER name
ENTRYPOINT [“/bin/cat”]
CMD [“/etc/passwd”]
Save and quit (Esc :wq)
To build a docker image from the above file
$ docker build -t myubuntu .
Docker multistage build:
Multi-stage builds are a new feature higher on the daemon and client. Multistage builds are useful to anyone who has struggled to optimize Dockerfiles while keeping them easy to read and maintain.
FROM centos
MAINTAINER name
RUN mkdir /opt/tomcat/
RUN yum -y install wget
RUN yum -y install unzip
WORKDIR /opt/tomcat
RUN curl -O http://apachemirror.wuchna.com/tomcat/tomcat-8/v8.5.51/bin/apache-tomcat-8.5.51-windows-x64.zip
RUN unzip apache-tomcat-8.5.51-windows-x64.zip
RUN mv apache-tomcat-8.5.51/* /opt/tomcat/.
RUN yum -y install java
RUN java -version
WORKDIR /opt/tomcat/webapps
RUN wget https://tomcat.apache.org/tomcat-7.0-doc/appdev/sample/sample.war
EXPOSE 8080
CMD [“/opt/tomcat/bin/catalina.sh”, “run”]