Wednesday, January 24, 2018

Docker

What Is Docker And Concept Of Containers With Virtualization?
Docker, a booming technology replacing bars of application deployment and environment dependency. Most of the people wanted to know what actually/really DOCKER is? This Series will clear most of your doubts and will help you to learn Docker a bit.
What is Docker?
Docker is a Container management service, which was initially launched in year Ma
rch 2013. As this article picture says, Docker is basically an idea based on three keywords which are: develop, ship and run anywhere. The concept of Docker and the idea behind this technology was: develop the application, ship them as containers and deploy these containers anywhere.


Note: Docker needs kernel version 3.8 and higher.

 

Benefits Of Docker:

Ø  Docker actually reduces the size of development environment by giving smaller traces of the operating system with the help of containers.
Ø  Containers made the task easy for different the teams working on different area/units like development, QA, and Operations.
Ø  The best part of Docker(containers) is that you can deploy containers anywhere and anywhere means cloud VMs and physical machines.
Ø  These Docker containers are lightweight and are very easily scalable. Also, they take a fraction of a second to get a restart.
Ø  Docker Uses host OS as these are light weighted VMs
Ø  You don’t need to pre-allocate any RAM in containers.

We are talking a lot about containers to its very important to know container before moving forward.

What are Containers?

Well, Containers are extremely lightweight virtual machines (VMs). these VMs are footprints/traces of a real operating system. Well, this technology is not a competitor of virtualization. It is actually using the concept of virtualization on an upper level. In fact, containers are to complement VMs.

Containers isolate the application from accessing the resources as these are VMs. Now here is a fact, containers concept is not really new to our world. we have been using the same concept around for a while in Solaris zones, FreeBSD jails and chroot. All of them were containers.
Execution of containers is governed by Container engines (The Docker Engine) and share the OS kernel and required libraries. Docker uses cgroups(control groups is a Linux kernel feature that limits, accounts for, and isolates the resource usage) and namespaces in the Linux kernel which impose resource isolation. Once Docker is installed and services are started, the Docker daemon is responsible for building, running and distributing the Docker containers.
The Docker client interacts with the Docker daemon via sockets or through a RESTful API to do all the tasks.

Now Let’s Know More About Docker and its Components

Components Of Docker:

Internal components

There are three internal components that need to be understood
1.    Docker Images: Docker images are read-only templates; these images contain the operating system with the required libraries/tools and the applications.
2.    Docker Registries: Docker registries hold these images. These registries are like public/private repositories. The public Docker registry is called Docker Hub and has a huge collection of images with various applications embedded.
3.    Docker Containers: A Docker container holds everything that is needed for an application to execute. Containers are created from a Docker image. Similar to virtual machines, containers can be started, stopped, moved and deleted. Each container provides an isolated and secure environment for applications to run.

Useful Information About Docker:

Following are several other components of Dockers depending upon OS and other factors.
1.    Docker for Mac − It allows one to run Docker containers on the Mac OS.
2.    Docker for Linux − It allows one to run Docker containers on the Linux OS.
3.    Docker for Windows − It allows one to run Docker containers on the Windows OS.
4.    Docker Engine − It is used for building Docker images and creating Docker containers.
5.    Docker Hub − This is the registry which is used to host various Docker images.
6.    Docker Compose − This is used to define applications using multiple Docker containers.

When Docker runs a container from an image, it adds a read-write layer to the image by using the Union file system.
The Docker registries contain many base images either created by Docker Inc. or the community. These base images are used to build custom images with applications injected. Docker images can be built using a recipe file called Dockerfile.

Docker Vs VM:

 

                          As I said containers are based on the technology of virtualization but on an upper level but VMs and Containers differ on quite a few dimensions. Containers provide a way to virtualize an OS in order for multiple workloads to run on a single OS instance, whereas with VMs, the hardware is being virtualized to run multiple OS instances. Containers’ speed, agility, and portability make them yet another tool to help streamline software development like containers are based on virtualizing an OS in order for multiple workloads to run on a single OS instance, while in the case of virtualization, the hardware is virtualized to run multiple OS instances.Also, Containers’ are good in speed, agility, and portability which makes them another tool to help streamline software development. This Image will help you a lot to understand the fact.

How To Install Docker On CentOS/RHEL 6/7 And Learn Docker HUB Registration:

                                  Docker can be installed easily and it is available for almost every Linux distro’s and other OS(s) like Windows or Mac. Docker can also be compiled on the local system itself with the use of source code present on GitHub. Also, there are pre-compiled packages for Docker. It can be installed on the Host Machine OS or on a Virtual Machine.
                            As I mentioned that Docker can be installed on the Host Machine OS or on a Virtual Machine so it’s up to you what you gonna do. But before moving further with installation process you may like read about Docker, its components, and benefits
Scenario :
Host OS: CentOS 7 x86_64
RAM: 4GB memory,
Network Port:  1GB/s
HDD: 1000GB of disk space.

Prerequisite:
1.    Disabled the SELinux and FirewallD services
2.    We will pull the Docker-relevant packages from the repo, so provide proxy details.

 Note: –  If the system is behind the proxy, then ensure that HTTP_PROXY and HTTPS_PROXY are defined in the configuration file/etc/sysconfig/docker

Note: – This tutorial is for the scenario mentioned above but commands and steps for RHEL/CentOS 6/7 are same.

Step 1: Install EPEL Repository
                     For the installation of Docker, we required EPEL (Extra Packages for Enterprise Linux) repository pre-configured on CentOS/RHEL 6. But, If you are running a CentOS/RHEL 7 then you don’t need to install EPEL because CentOS 7 already have all packages required for Docker installation.
For RHEL/CentOS 6 32-64 Bit

#wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm#
#rpm -ivh epel-release-6-8.noarch.rpm





For RHEL/CentOS 6 64-Bit

#wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
#rpm -ivh epel-release-6-8.noarch.rpm

Step 2:  Perform Docker Installation
                  We have completed and finished our dependencies and now we can move forward with Docker installation with following commands
For CentOS/RHEL 7

# yum install docker 
# yum install device-mapper-event-libs

For RHEL 6

# yum install docker-io
# yum install device-mapper-event-libs


Step 3: Start/Enable Docker Service
                     As we have successfully installed Docker and service, we need to start its service. For starting docker we need will use the following sets of command for CentOS/RHEL 6/7. Also we ill enable the service for current runlevel(s).
For RHEL/CentOS 7

# systemctl start docker.service
# systemctl status docker.service
# systemctl enable docker.service


For RHEL/CentOS 6

# service docker start
# service docker status
# chkconfig docker on

Step 4: Verify Installation
                    As we have a successful installation and a running service of Docker, we can verify its presence with two methods
1 . Checking Version 

# docker -v
Docker version 1.12.6, build 1398f24/1.12.6
2. Saying Hello  To Docker

# docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://cloud.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/

If you could See this then Your Docker installation is a success and its verified.
                     Now if you want to pull images(CentOS, Ubuntu etc.) from the Docker Hub then you need to complete a registration process on Docker HUB.  After successful registration, those credentials will be used for several other purposes too, other just pulling images from HUB. Once the registration is done, use the command docker login to log into the hub. (We will use this in Next Article)

But For Now A Quick Guide for  Docker Registration
                  Docker Hub is a registry service on the cloud that allows you to download Docker images that are built by other communities. You can also upload your own Docker built images to Docker hub.
Step 1: Follow the Link For Docker HUB  (https://hub.docker.com/) And Complete The Form


Step 2:- Active Your Account after clicking on the link you got in your Activation Email on your Email ID
Step 3: – After Logging in you Docker HUB Interface Be like this

Learn To Install, Run And Delete Applications Inside Docker Containers


1. Start Container

             First, start the container and with the help of the following command get the ContainerID
[root@localhost ~]# docker run -it centos /bin/bash   ------------ You Started Container
[root@7d81872f6a2b /]# exit      ------------ Exit Container
exit
[root@localhost ~]# docker ps -l ----------- Get ContainerID
CONTAINER ID IMAGE    COMMAND    CREATED                  STATUS                                PORTS    NAMES
7d81872f6a2b      centos    "/bin/bash"     About a minute ago   Exited (0) 7 seconds ago                    desperate_kare

As you have got a ContainrID but as you can see it’s in Exited State.

2. Install A Service In Container

           To install a service or the packages we need to start this container and get attached to it. We get access inside container we can do lots of work. For this example, we will install Apache Deamon. First Start The Docker Container Using Container ID
[root@localhost ~]# docker start 7d81872f6a2b
7d81872f6a2b
[root@localhost ~]# docker ps -l    ----------- Get ContainerID
CONTAINER ID      IMAGE    COMMAND          CREATED             STATUS       PORTS    NAMES
7d81872f6a2b      centos   "/bin/bash"     About 2 minute ago   Up 7 seconds         desperate_kare

Now As you can see Docker Container has started and now we will do our service installation.

[root@localhost ~]# docker attach 7d81872f6a2b
[root@7d81872f6a2b /]#  ----------This Shows You are connected with Container
[root@7d81872f6a2b /]# yum install httpd -y
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirror.nbrc.ac.in
* extras: mirror.nbrc.ac.in
* updates: mirror.nbrc.ac.in
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-45.el7.centos.4 will be installed
--> Processing Dependency: httpd-tools = 2.4.6-45.el7.centos.4 for package: httpd-2.4.6-45.el7.centos.4.x86_64
--> Processing Dependency: system-logos >= 7.92.1-1 for package: httpd-2.4.6-45.el7.centos.4.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-45.el7.centos.4.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-45.el7.centos.4.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-45.el7.centos.4.x86_64
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
===============================================================================================================================================================================================
Package Arch Version Repository Size
===============================================================================================================================================================================================
Installing:
httpd x86_64 2.4.6-45.el7.centos.4 updates 2.7 M
Installing for dependencies:
httpd-tools x86_64 2.4.6-45.el7.centos.4 updates 84 k
mailcap noarch 2.1.41-2.el7 base 31 k
 
Transaction Summary
===============================================================================================================================================================================================
Install 1 Package (+5 Dependent packages)
 
Total download size: 24 M
Installed size: 32 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/apr-1.4.8-3.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for apr-1.4.8-3.el7.x86_64.rpm is not installed
(1/6): apr-1.4.8-3.el7.x86_64.rpm | 103 kB 00:00:00
Public key for httpd-tools-2.4.6-45.el7.centos.4.x86_64.rpm is not installed 0% [ ] 0.0 B/s | 103 kB --:--:-- ETA
(2/6): httpd-tools-2.4.6-45.el7.centos.4.x86_64.rpm | 84 kB 00:00:00
(3/6): mailcap-2.1.41-2.el7.noarch.rpm | 31 kB 00:00:00
(4/6): apr-util-1.5.2-6.el7.x86_64.rpm | 92 kB 00:00:00
(5/6): httpd-2.4.6-45.el7.centos.4.x86_64.rpm | 2.7 MB 00:00:09
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 743 kB/s | 24 MB 00:00:33
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
 
Installing : httpd-tools-2.4.6-45.el7.centos.4.x86_64 3/6
 
Installing : mailcap-2.1.41-2.el7.noarch 5/6
Installing : httpd-2.4.6-45.el7.centos.4.x86_64 6/6
Verifying : httpd-tools-2.4.6-45.el7.centos.4.x86_64 4/6
Verifying : httpd-2.4.6-45.el7.centos.4.x86_64 5/6
Verifying : centos-logos-70.0.6-3.el7.centos.noarch 6/6
 
Installed:
httpd.x86_64 0:2.4.6-45.el7.centos.4
 
Dependency Installed:
apr.x86_64 0:1.4.8-3.el7 apr-util.x86_64 0:1.5.2-6.el7 centos-logos.noarch 0:70.0.6-3.el7.centos httpd-tools.x86_64 0:2.4.6-45.el7.centos.4 mailcap.noarch 0:2.1.41-2.el7
 
Complete!
[root@7d81872f6a2b /]# exit

We have Successfully Installed A service Inside The Container.

3.   Commit Changes To The Container

 

                   As we a have container with Apache installed inside. Now we will commit this container a new Image and we will save this as our new Docker Image. First, We need to get exit from this Container and then follow more steps.
[root@localhost ~]# docker commit 7d81872f6a2b centos-apache
sha256:c260cf77d362b89cc00bed107250166ecd548752d50ef78d5c9dcd0f7c114bdb -- Output

Here, 7d81872f6a2b is Container ID and centos-apache is our new Image. Now we can confirm that we have a new image by the following command.

[root@localhost ~]# docker images
REPOSITORY             TAG    IMAGE ID     CREATED            SIZE
centos-apache          latest c260cf77d362 About a minute ago 327.5 MB
docker.io/hello-world  latest 1815c82652c0 2 weeks ago        1.84 kB
docker.io/centos       latest 3bee3060bfc8 3 weeks ago        192.5 MB

 

4. Verifying Image With Installed Apache

                  Now we can test our newly created image and check that all the changes we did commit inside. In this case, Apache service has been installed), so execute the following command in order to generate a new container, and tell us that we have installed Apache
[root@localhost ~]# docker run centos-apache /bin/bash -c "whereis httpd" 
 
httpd: /usr/sbin/httpd /usr/lib64/httpd /etc/httpd /usr/share/httpd

 

 

5. Removing A Container

                  Now we will remove this container because we don’t need it and to do the same, we require contained ID so run following command to get ContainerID and delete that container.
[root@localhost ~]# docker ps -a   -- To Get Container ID
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95ddcedfbea4  centos-apache "/bin/bash -c 'wherei" 17 seconds ago Exited (0) 10 seconds ago furious_brahmagupta
[root@localhost ~]# docker rm 95ddcedfbea4 


6. Running A Sevice Inside Container

                   Now we have our own image and we want to run Apache server from our new docker image centos-apache and to do that first thing we need to do is to create a new container now along with we will map host-container ports and enter container shell by issuing following command.

[root@localhost ~]# docker run -it -p 81:80 centos-apache /bin/bash 
[root@3a5c5be3e947 /]# httpd &
[1] 15    -----------  Press CTRL+p then CTRL+q

-p option is to open host port to container port( host port can be arbitrary and no other host services should listen to it), the container port must be exactly the port that the inside daemon is listening to. Ctrl-p + Ctrl-q keys are used to detach container from console.
Now we can check work and get the state of your running container with following commands
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a5c5be3e947 centos-apache "/bin/bash" 9 minutes ago Up 9 minutes 0.0.0.0:81->80/tcp silly_hawking

OR

[root@localhost ~]# netstat -tlpn | grep :81
tcp6 0 0 :::81 :::* LISTEN 9157/docker-proxy-c

 

7. Check Your Work

                       In order to visit the page served by the Apache Container, open a browser your LAN and type the IP address of your machine using the HTTP protocol.
Here You Can see its working on port 81

8. Stop A Container

                 We have learned most of the basic operation on Docker now this is the final step To stop the container. To do that run the following command followed by container ID or name.
[root@localhost ~]# docker ps ---- Getting Container Name Or ContainerID
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a5c5be3e947 centos-apache "/bin/bash" 24 minutes ago Up 24 minutes 0.0.0.0:81->80/tcp silly_hawking
[root@localhost ~]# docker stop silly_hawking  ---- Stopping Container By Name
silly_hawking
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES   ---- No Container Is Running

Getting Familiar With Docker Commands Docker Terms: Docker

So Let’s dig out Command in Docker

              In Docker, everything is based on Images. An image is a combination of a file system and parameters. So most of our command will move around images.

Command 1: Create an Instance of Container

This command is used to create an instance of a container from an image.
#docker run hello-world
·         The Docker command tells the Docker program on the Operating System that something needs to be done.
·         The run command is used to mention that we want to create an instance of an image, called a container.
·          “hello-world” represents the image from which the container is made.


Command 2: Docker Information

This Command will show all the detailed information.
[root@localhost ~]# docker info
Containers: 3
Running: 0
Paused: 0
Stopped: 3
Images: 1
Server Version: 1.12.6
Storage Driver: devicemapper
.................................................................
.............................................................
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Insecure Registries:
 127.0.0.0/8
Registries: docker.io (secure)

 

Command 3: Docker Version

 

This command will help you to check out the running version of Docker of client end and server end.
[root@localhost ~]# docker version
Client:
 Version: 1.12.6
 API version: 1.24
 Package version: docker-1.12.6-28.git1398f24.el7.centos.x86_64
 Go version: go1.7.4
 Git commit: 1398f24/1.12.6
 Built: Fri May 26 17:28:18 2017
 OS/Arch: linux/amd64
 
Server:
 Version: 1.12.6
 API version: 1.24
 Package version: docker-1.12.6-28.git1398f24.el7.centos.x86_64
 Go version: go1.7.4
 Git commit: 1398f24/1.12.6
 Built: Fri May 26 17:28:18 2017
 OS/Arch: linux/amd64

 

Comand 4: List Docker Commands

           To get a list of all the command that works on docker on the console, you may hit below command on terminal. All the command and arguments will get populated on the terminal.
[root@localhost ~]# docker

Note: You Cab Use Run ‘docker COMMAND –help‘ for more information on a command

Command 5: Login Docker HUB or Docker Registry

                 Docker Registry or Docker HUB is used for searching and to pull Docker images(like CentOs, Ubuntu etc.) from the Docker HUB. Also, you can push and update your images to Docker HUB for future use and there are other many things you can do with Docker HUB.
You need an account on Docker HUB to get logged in. If you don’t have account you can create Following is the Command to Login into Docker HUB or Docker Registry:

[root@localhost ~]# docker login
Username: <Your Docker Login Username>
Password: <Type In Your Password>
Login succeeded.

 

Command 5: To Seach Images With Doker HUB


               Soon After logging into Docker HUB, you are eligible for PULL, PUSH, Search, Update docker image from Docker HUB. These images can be used for creating containers. Docker Hub has lots of community free images created for you like Ubuntu image mongo image and a much more great deal of free images from its repositories. Following command is used to search centOS images from the repository.

[root@localhost ~]# docker search centos
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/centos The official build of CentOS. 3432 [OK] 
docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.9 x86_64 / CentOS-7 7.3.1611 x8... 74 [OK]
docker.io docker.io/nimmis/java-centos This is docker images of CentOS 7 with dif... 26 [OK]
docker.io docker.io/gluster/gluster-centos Official GlusterFS Image [ CentOS-7 + Glu... 19 [OK]
docker.io docker.io/kinogmt/centos-ssh CentOS with SSH 15 [OK]
docker.io docker.io/egyptianbman/docker-centos-nginx-php A simple and highly configurable docker co... 9 [OK]

Command 6: Download Docker image

         After logging in and searching out images from docker you can pull these image i=on yu local system using the following command.
[root@localhost ~]#docker pull centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ... 
latest: Pulling from docker.io/library/centos
d5e46245fe40: Pull complete 
Digest: sha256:aebf12af704307dfa0079b3babdca8d7e8ff6564696882bcb5d11f1d461f9ee9

 

Command 7: List Docker Images On Local System

      Now when you have lots of downloaded docker images you can look all the docker images present on your local system by the following command.
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hello-world latest 1815c82652c0 13 days ago 1.84 kB
docker.io/centos latest 3bee3060bfc8 3 weeks ago 192.5 MB
TAG − This is used to logically tag images.

Image ID − This is used to uniquely identify the image.

Created − The number of days since the image was created.

Virtual Size − The size of the image.

 

Command 8: Remove Docker Image

             When you have lots of running which are obsolete or you no longer need any Docker image then you can remove that image using the following command.
Syntex: docker rmi ImageName
[root@localhost ~]# docker rmi ubuntu
Untagged: ubuntu:latest
Untagged: docker.io/ubuntu@sha256:a0ee7647e24c8494f1cf6b94f1a3cd127f423268293c25d924fbe18fd82db5a4
Deleted: sha256:d355ed3537e94e76389fd78b77241eeba58a11b8faa501594bc82d723eb1c7f2
Deleted: sha256:dd864b96a38e849779c42a04159bbb39c7ab47253bf222049b471d8f26b60d14
Deleted: sha256:80e85c818fa0447c96a42501ca7457ad83e5834aa76f22c366342106889b7411
Deleted: sha256:11a2a269cf6ec2cefcb4e24370b8b2d7a4875450bafd3a70bd42eb787481d798
Deleted: sha256:1118f33a0ee7a874a04318248a886b2bdaf44cba286644ab7ded870aefe64b62
Deleted: sha256:cb11ba6054003d39da5c681006ea346e04fb3444086331176bf57255f149c670

 

Command 9: Run A Docker Container

The concept is little catchy, whenever a command is sent for execution in the Docker Image, a container in obtained. When this command execution is finished,  the container gets stopped (a non-running or exited container state). It Means at every command execution into the same image a new container is created again and again and exited.
All these created containers remain on host filesystem. You can delete them by using the docker rm command. To run or create a container use the following command:
Syntex: docker run [local image] [command to run into container]
[root@localhost ~]# docker run centos cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)

 

Command 10: List Running Docker Containers

     Whenever a command execution is performed on a Docker Image a container is created and gets stopped after execution but it remains in exited or non-running state. The following command will display a list of the running and stopped (non-running) containers:
[root@localhost ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a636b6753136 centos "cat /etc/redhat-rele" 5 minutes ago Exited (0) 4 minutes ago hungry_mayer
Note: ContainerID will be used in many actions. This command also gives you information like CONTAINER ID, IMAGE, COMMAND, CREATED, STATUS, PORTS, NAMES.

 

Command 11: Start A Container

      
                Now We have a ContainerID so we can start the container again with the command that was used to create it, by using the following command
Syntex: docker start ContainerID
[root@localhost ~]# docker start a636b6753136
a636b6753136

 

Command 12: Listing of Containers (Expended)

                 In a production environment there are many running containers and to list them we have command. This command is used to get the currently running containers.
[root@localhost home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7e0cab13b5f3 centos "/bin/sh" 12 seconds ago Up 5 seconds sleepy_wing

               This Can also be used with ‘-a’ argument and this command will list all of the containers on the system

[root@localhost home]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7e0cab13b5f3 centos "/bin/sh" 43 seconds ago Up 35 seconds sleepy_wing
05bd33e0b71e centos "/bin/sh" 3 hours ago Exited (0) 3 hours ago tiny_swartz
2b8b51d81869 centos "/bin/sh" 3 hours ago Exited (0) 3 hours ago infallible_bhaskara
 

 

Command 13: Stop A Docker

          Now if our container is working/running and we got its ID by issuing docker ‘ps’ command previously. Now to stop that container following command

Syntex: docker stop container ID or auto-generated name.
[root@localhost ~]# docker stop hungry_mayer



Command 14: Add Name To Docker Container On Local

                consider a situation when you have good numbers of containers and you can’t remember so you don’t have to remember the container ID. You can give a unique name for every container using the –name option as this following example shows:
Syntex: docker run –name unique_name Image Command
[root@localhost ~]# docker run --name unique_name centos cat /etc/redhat-release

 

Command 15: Use of Name To (start, stop, remove, top, stats)

           Now as we have given a unique name to the container so we can do a lot of work with container unique name like following.
[root@localhost ~]# docker start unique_name 
[root@localhost ~]# docker stats unique_name 
[root@localhost ~]# docker top unique_name

Note: Sometimes there is no output because the command was in execution which was used to create the container.

 

Command 16: Start Docker Container Interactive Shell

         To start an interactive session with into a container shell, and want to run commands as you do on any other Linux session, please run following command
[root@localhost ~]# docker run -it centos /bin/sh
sh-4.2# bash
[root@3e278670bbc1 /]#

Note: Run bash command to get bash shell
Here, Centos is the name of the image we download from Docker Hub
-it is used to mention that we want to run in interactive mode.
/bin/bash is used to run the bash shell once CentOS is up and running.

 

Command 17: Quit Docker Container Interactive Shell

           Now if you want to run quit this interactive session from the container with the host. You can do this by typing exit in an interactive shell. The exit command terminates all the container processes and stops it.
[root@3e278670bbc1 /]# exit
exit
sh-4.2# exit
exit
[root@localhost ~]#

 

Command 18: Keep Docker Shell Session Active

                      As the previous command kill all the running process of a container after exiting interactive shell mode. Now if you don’t want to kill the running container process and want to keep the container in running state. Also, want to exit the interactive shell session and return to the Host Terminal, you can do this by pressing Ctrl+p and Ctrl+q keys.

[root@d15866d0dc29 /]#   "<Press Ctrl+p> and then <Press Ctrl+q>"
[root@localhost ~]#

 

Command 19: To Reconnect With Running Container

          Well with the previous command we kept a docker running even after exiting the interactive shell now to reconnect that interactive shell we need the container ID or name of that container. Use the ‘docker ps’ command to get the ConatinerID or name and use the following command to get reconnected.

[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d15866d0dc29 centos "/bin/sh" 10 minutes ago Up 10 minutes naughty_montalcini
Now Use this ContainerID or Name to Reconnect Container.
Syntex: docker attach <container id>
[root@localhost ~]# docker attach d15866d0dc29
[root@d15866d0dc29 /]#



Command 20: To Stop A Running Container

          To stop and kill a docker from the host, use the following command

Syntax: docker kill <container id>
[root@localhost home]# docker kill 9ac0ec22a0a2
9ac0ec22a0a2

 

 

Command 21: Inspect A Docker

                    This command is used see the details of an image or container. This Command provides lots of information about the Docker Image.

Syntax: docker inspect Repository

[root@localhost ~]# docker inspect centos
[
 {
 "Id": "sha256:3bee3060bfc81c061ce7069df35ce090593bda584d4ef464bc0f38086c11371d",
 "RepoTags": [
 "docker.io/centos:latest"
 ],
 "RepoDigests": [
.....................
........................
 },
 "RootFS": {
 "Type": "layers",
 "Layers": [
 "sha256:dc1e2dcdc7b6ff86d785fa16cf97464d263d04346a191c57b5ca8a66b4155861"
 ]
 }
 }
]

Repository − This is the name of the Image.

Command 22: Docker Logs

               When there is a production, we always need logs for data analysis and get logs of container from the host machine we need to run following command but to do this we require a ContainerID
Syntex: docker logs ContainerID
[root@localhost ~]# docker logs 2b8b51d81869

 

 

Command 23: Get Image ID’s Only

        If you require image ID only for any purpose you may issue the following command to get IDs
[root@localhost home]# docker images -q
1815c82652c0
3bee3060bfc8

As you can see this command is used to return only the Image IDs of the images.
q− It tells the Docker command to return the Image ID’s only

Command 24: Docker History

                docker history is the command to check all the commands which were fired/ran with an image via a container.
Syntax: docker history ImageID
This command will show all the commands that were run with the CentOS image.
[root@localhost home]# docker history centos
IMAGE CREATED CREATED BY SIZE COMMENT
3bee3060bfc8 3 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B 
<missing> 3 weeks ago /bin/sh -c #(nop) LABEL name=CentOS Base Ima 0 B 
<missing> 3 weeks ago /bin/sh -c #(nop) ADD file:d22a9c627d1d1f32a8 192.5 MB

ImageID − This is the Image ID for which you want to see all the commands that were run against it.

No comments:

Post a Comment