Total Pageviews

Thursday, 1 December 2016

Docker and its Containers !!!

Docker project is an open source project by Docker Inc. By the name Docker should contain containers, here the containers are nothing but different applications which were build and ready to use. All we need to do, is to install Docker and initialize an API of our interest.

Docker has different API's images, an instance of such image is a Container. It provides a open platform for Developers and SysAdmins to build, ship and run applications. Developers do the code part for the Application Images, they will build an application. SysAdmins can run these images using Docker platform with the help few commands.

If client requires a Ubuntu machine, it will be readily deployed by an Admin with the help of Docker. All he needs to do is to install packages related to Docker and start its services, then download the repository of image related to Ubuntu. Last step is to initialize a Docker image of Ubuntu, thats it within few seconds we can have Ubuntu machine.

For easy understanding we can compare Docker platform as Android Play store and its Images as Apps in Play store. All we need to do, select the application, download and install it.

Let us see how easy the working part of Dockers.

[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)                         --- Docker is OpenSource, we can run it on any OS.
[root@localhost ~]#

[root@localhost ~]# docker -v
-bash: docker: command not found                            --- As of now, Docker is not installed on my machine
[root@localhost ~]#

[root@localhost ~]# docker version
-bash: docker: command not found
[root@localhost ~]#

Using YUM we can install packages of Docker,

[root@localhost ~]# yum install docker
Loaded plugins: fastestmirror
base                                                                                               | 3.6 kB  00:00:00
extras                                                                                             | 3.4 kB  00:00:00
updates                                                                                            | 3.4 kB  00:00:00
(1/4): base/7/x86_64/group_gz                                                                      | 155 kB  00:00:02
(2/4): extras/7/x86_64/primary_db                                                                  | 166 kB  00:00:02
(3/4): base/7/x86_64/primary_db                                                                    | 5.3 MB  00:00:08
(4/4): updates/7/x86_64/primary_db                                                                 | 8.4 MB  00:00:13
Determining fastest mirrors
 * base: mirrors.centos.webair.com
 * extras: mirrors.centos.webair.com
 * updates: mirrors.centos.webair.com
Resolving Dependencies
--> Running transaction check
---> Package docker.x86_64 0:1.10.3-46.el7.centos.14 will be installed
--> Processing Dependency: docker-common = 1.10.3-46.el7.centos.14 for package: docker-1.10.3-46.el7.centos.14.x86_64
--> Processing Dependency: oci-systemd-hook >= 1:0.1.4-4 for package: docker-1.10.3-46.el7.centos.14.x86_64
--> Running transaction check
---> Package docker-common.x86_64 0:1.10.3-46.el7.centos.14 will be installed

************** OUTPUT TRUNCATED *************

Dependencies Resolved

==========================================================================================================================
 Package                             Arch                Version                                Repository           Size
==========================================================================================================================
Installing:
 docker                              x86_64              1.10.3-46.el7.centos.14                extras              9.5 M

************** OUTPUT TRUNCATED *************

Transaction Summary
==========================================================================================================================
Install  1 Package (+13 Dependent packages)

Total download size: 12 M
Installed size: 53 M
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/audit-libs-python-2.4.1-5.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for audit-libs-python-2.4.1-5.el7.x86_64.rpm is not installed

************** OUTPUT TRUNCATED *************

Installed:
  docker.x86_64 0:1.10.3-46.el7.centos.14

Dependency Installed:
  audit-libs-python.x86_64 0:2.4.1-5.el7                     checkpolicy.x86_64 0:2.1.12-6.el7
  docker-common.x86_64 0:1.10.3-46.el7.centos.14             docker-selinux.x86_64 0:1.10.3-46.el7.centos.14

************** OUTPUT TRUNCATED *************

Complete!
[root@localhost ~]#

I have truncated so much from output, In short it checks for the repositories of CentOS, it checks for dependency packages and then installs all packages related to Dockers of CentOS.

[root@localhost ~]# docker -v
Docker version 1.10.3, build cb079f6-unsupported           ---- Since it is installed, now we can see the version.
[root@localhost ~]#

[root@localhost ~]# docker version                                   ---- For detailed info related to version.
Client:
 Version:         1.10.3                                                        
 API version:     1.22
 Package version: docker-common-1.10.3-46.el7.centos.14.x86_64
 Go version:      go1.6.3
 Git commit:      cb079f6-unsupported
 Built:           Fri Sep 16 13:24:25 2016
 OS/Arch:         linux/amd64
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
[root@localhost ~]#

As of now, we installed Docker thats it. We didn't start its services, Thats Why.......

[root@localhost ~]# docker info
Cannot connect to the Docker daemon. Is the docker daemon running on this host?

Let us start the Docker service,

[root@localhost ~]# systemctl status docker
? docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: http://docs.docker.com
[root@localhost ~]#

[root@localhost ~]# systemctl start docker
[root@localhost ~]#

[root@localhost ~]# systemctl status docker
? docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2016-10-20 21:25:19 EDT; 2s ago
     Docs: http://docs.docker.com
 Main PID: 2591 (docker-current)
   CGroup: /system.slice/docker.service
           +-2591 /usr/bin/docker-current daemon --exec-opt native.cgroupdriver=systemd --selinux-enabled --log-driver=...

************** OUTPUT TRUNCATED *************

Oct 20 21:25:19 localhost.localdomain docker-current[2591]: time="2016-10-20T21:25:19.120444318-04:00" level=info m...ock"
Oct 20 21:25:19 localhost.localdomain systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]#

Now the version command shows the complete output, 

[root@localhost ~]# docker version
Client:
 Version:         1.10.3
 API version:     1.22
 Package version: docker-common-1.10.3-46.el7.centos.14.x86_64
 Go version:      go1.6.3
 Git commit:      cb079f6-unsupported
 Built:           Fri Sep 16 13:24:25 2016
 OS/Arch:         linux/amd64

Server:
 Version:         1.10.3
 API version:     1.22
 Package version: docker-common-1.10.3-46.el7.centos.14.x86_64
 Go version:      go1.6.3
 Git commit:      cb079f6-unsupported
 Built:           Fri Sep 16 13:24:25 2016
 OS/Arch:         linux/amd64
[root@localhost ~]#

All the Docker commands will work now,

[root@localhost ~]# docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 1.10.3
Storage Driver: devicemapper
 Pool Name: docker-253:0-1416-pool
 Pool Blocksize: 65.54 kB
 Base Device Size: 10.74 GB
 Backing Filesystem: xfs
 Data file: /dev/loop0
 Metadata file: /dev/loop1
 Data Space Used: 11.8 MB
 Data Space Total: 107.4 GB

************** OUTPUT TRUNCATED *************

 Volume: local
 Network: host bridge null
Kernel Version: 3.10.0-327.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
Number of Docker Hooks: 2
CPUs: 1
Total Memory: 993 MiB
Name: localhost.localdomain
ID: HQ6K:NYHE:7ACO:SFVG:PO43:7E2Z:TJKA:RXUI:PWSK:D7PK:MM5D:ZJEC
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
Registries: docker.io (secure)
[root@localhost ~]#
[root@localhost ~]#

Command to check how many Docker processes are running....

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost ~]#

Now, let us initialize image of CentOS...

[root@localhost ~]# docker run -it centos /bin/bash
Unable to find image 'centos:latest' locally                       --- Since it is our first container
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
8d30e94188e7: Pull complete
Digest: sha256:2ae0d2c881c7123870114fb9cc7afabd1e31f9888dac8286884f6cf59373ed9b
Status: Downloaded newer image for docker.io/centos:latest      
                                  ---- For next CentOS container no need to download it, straight away initializes using this Image.
[root@3775b792faf0 /]#
                                                       Immediately after intializing, we logged into our instance.
[root@3775b792faf0 /]#
[root@3775b792faf0 /]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@3775b792faf0 /]#
[root@3775b792faf0 /]# uname -a
Linux 3775b792faf0 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@3775b792faf0 /]#

[root@3775b792faf0 /]# cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      3775b792faf0                                     ------- This is our Container ID
[root@3775b792faf0 /]#
[root@3775b792faf0 /]#
[root@3775b792faf0 /]# exit
exit
[root@localhost ~]#

Let us check the Docker processes now,

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
3775b792faf0        centos              "/bin/bash"         50 seconds ago      Exited (0) 4 seconds ago                       trusting_mayer
[root@localhost ~]#

Let us try initializing image of Ubuntu this time....

[root@localhost ~]# docker run -it ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
Trying to pull repository docker.io/library/ubuntu ...
latest: Pulling from docker.io/library/ubuntu
6bbedd9b76a4: Pull complete
fc19d60a83f1: Pull complete
de413bb911fd: Pull complete
2879a7ad3144: Pull complete
668604fde02e: Pull complete
Digest: sha256:2d44ae143feeb36f4c898d32ed2ab2dffeb3a573d2d8928646dfc9cb7deb1315
Status: Downloaded newer image for docker.io/ubuntu:latest
root@7104e28b790f:/#
root@7104e28b790f:/#

root@7104e28b790f:/# cd /etc/
root@7104e28b790f:/etc#
root@7104e28b790f:/etc#
root@7104e28b790f:/etc# cat os-release
NAME="Ubuntu"
VERSION="16.04.1 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.1 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
UBUNTU_CODENAME=xenial
root@7104e28b790f:/etc#

On top of any OS, we can run any other OS containers using Docker.

root@7104e28b790f:/etc# uname -a
Linux 7104e28b790f 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
root@7104e28b790f:/etc#                           ---- Even it is Ubuntu, still it shares our CentOS kernel.

root@7104e28b790f:/etc# cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      7104e28b790f
root@7104e28b790f:/etc#
root@7104e28b790f:/etc# exit
exit
[root@localhost ~]#

CentOS Kernel...

[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]#

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                     PORTS               NAMES
7104e28b790f        ubuntu              "/bin/bash"         About a minute ago   Exited (0) 4 seconds ago                       admiring_franklin
3775b792faf0        centos              "/bin/bash"         3 minutes ago        Exited (0) 2 minutes ago                       trusting_mayer
[root@localhost ~]#

Now we have images of both Ubuntu and CentOS ready with us, if we try to initialize a new CentOS or Ubuntu container it will be so easy...

[root@localhost ~]# docker run -it centos /bin/bash
[root@d3e4a6957462 /]#                                     ----- Within few seconds, our new CentOS container created.
[root@d3e4a6957462 /]#

[root@d3e4a6957462 /]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@d3e4a6957462 /]#
[root@d3e4a6957462 /]# uname -a
Linux d3e4a6957462 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@d3e4a6957462 /]#
[root@d3e4a6957462 /]# exit
exit
[root@localhost ~]#
[root@localhost ~]#

It is so simple working with Docker. So platform on which we are initializing APIs is Docker Engine and the instance of images are its Containers.

Let's get familiar with few other commands of Dockers... Let's just try with docker so it shows its usage along with options....

[root@localhost ~]# docker
Usage: docker [OPTIONS] COMMAND [arg...]
       docker daemon [ --help | ... ]
       docker [ --help | -v | --version ]

A self-sufficient runtime for containers.

Options:

  --config=~/.docker              Location of client config files
  -D, --debug                     Enable debug mode
  -H, --host=[]                   Daemon socket(s) to connect to
  -h, --help                      Print usage
  -l, --log-level=info            Set the logging level
  --tls                           Use TLS; implied by --tlsverify
  --tlscacert=~/.docker/ca.pem    Trust certs signed only by this CA
  --tlscert=~/.docker/cert.pem    Path to TLS certificate file
  --tlskey=~/.docker/key.pem      Path to TLS key file
  --tlsverify                     Use TLS and verify the remote
  -v, --version                   Print version information and quit

Commands:
    attach    Attach to a running container
    build     Build an image from a Dockerfile
    commit    Create a new image from a container's changes
    cp        Copy files/folders between a container and the local filesystem
    create    Create a new container
    diff      Inspect changes on a container's filesystem
    events    Get real time events from the server
    exec      Run a command in a running container
    export    Export a container's filesystem as a tar archive
    history   Show the history of an image
    images    List images
    import    Import the contents from a tarball to create a filesystem image
    info      Display system-wide information
    inspect   Return low-level information on a container or image
    kill      Kill a running container
    load      Load an image from a tar archive or STDIN
    login     Register or log in to a Docker registry
    logout    Log out from a Docker registry
    logs      Fetch the logs of a container
    network   Manage Docker networks
    pause     Pause all processes within a container
    port      List port mappings or a specific mapping for the CONTAINER
    ps        List containers
    pull      Pull an image or a repository from a registry
    push      Push an image or a repository to a registry
    rename    Rename a container
    restart   Restart a container
    rm        Remove one or more containers
    rmi       Remove one or more images
    run       Run a command in a new container
    save      Save an image(s) to a tar archive
    search    Search the Docker Hub for images
    start     Start one or more stopped containers
    stats     Display a live stream of container(s) resource usage statistics
    stop      Stop a running container
    tag       Tag an image into a repository
    top       Display the running processes of a container
    unpause   Unpause all processes within a container
    update    Update resources of one or more containers
    version   Show the Docker version information
    volume    Manage Docker volumes
    wait      Block until a container stops, then print its exit code

Run 'docker COMMAND --help' for more information on a command.
[root@localhost ~]#

We can name our container at the time of intializing it...

[root@localhost ~]# docker run --name firstdock -it ubuntu /bin/bash
root@34b3cc40449d:/#
root@34b3cc40449d:/#
root@34b3cc40449d:/# exit
exit
[root@localhost ~]#

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
34b3cc40449d        ubuntu              "/bin/bash"         17 seconds ago      Exited (0) 3 seconds ago             firstdock
d3e4a6957462        centos              "/bin/bash"         7 days ago          Exited (0) 7 days ago                         angry_euler
fa516d1acb13          centos              "/bin/ksh"          7 days ago           Created                                               stoic_booth
7104e28b790f        ubuntu              "/bin/bash"         7 days ago          Exited (0) 7 days ago                    admiring_franklin
3775b792faf0        centos                "/bin/bash"         7 days ago          Exited (0) 7 days ago                        trusting_mayer
[root@localhost ~]#

To start our container,

[root@localhost ~]#
[root@localhost ~]# docker start firstdock
firstdock
[root@localhost ~]#
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                  PORTS               NAMES
34b3cc40449d        ubuntu              "/bin/bash"         47 seconds ago      Up 5 seconds                                firstdock
d3e4a6957462        centos              "/bin/bash"         7 days ago          Exited (0) 7 days ago                       angry_euler
fa516d1acb13        centos              "/bin/ksh"          7 days ago          Created                                     stoic_booth
7104e28b790f        ubuntu              "/bin/bash"         7 days ago          Exited (0) 7 days ago                       admiring_franklin
3775b792faf0        centos              "/bin/bash"         7 days ago          Exited (0) 7 days ago                       trusting_mayer
[root@localhost ~]#

To stop our container,

[root@localhost ~]# docker stop firstdock
firstdock
[root@localhost ~]#
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                     PORTS               NAMES
34b3cc40449d        ubuntu              "/bin/bash"         About a minute ago   Exited (0) 2 seconds ago                       firstdock
d3e4a6957462        centos              "/bin/bash"         7 days ago           Exited (0) 7 days ago                          angry_euler
fa516d1acb13        centos              "/bin/ksh"          7 days ago           Created                                        stoic_booth
7104e28b790f        ubuntu              "/bin/bash"         7 days ago           Exited (0) 7 days ago                          admiring_franklin
3775b792faf0        centos              "/bin/bash"         7 days ago           Exited (0) 7 days ago                          trusting_mayer
[root@localhost ~]#

To rename our container,

[root@localhost ~]# docker rename firstdock dock1
[root@localhost ~]#
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
34b3cc40449d        ubuntu              "/bin/bash"         3 minutes ago       Exited (0) 2 minutes ago                       dock1
d3e4a6957462        centos              "/bin/bash"         7 days ago          Exited (0) 7 days ago                          angry_euler
fa516d1acb13        centos              "/bin/ksh"          7 days ago          Created                                        stoic_booth
7104e28b790f        ubuntu              "/bin/bash"         7 days ago          Exited (0) 7 days ago                          admiring_franklin
3775b792faf0        centos              "/bin/bash"         7 days ago          Exited (0) 7 days ago                          trusting_mayer
[root@localhost ~]#

To remove a container,

[root@localhost ~]# docker rm angry_euler
angry_euler
[root@localhost ~]#
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
34b3cc40449d        ubuntu              "/bin/bash"         4 minutes ago       Exited (0) 3 minutes ago                       dock1
fa516d1acb13        centos              "/bin/ksh"          7 days ago          Created                                        stoic_booth
7104e28b790f        ubuntu              "/bin/bash"         7 days ago          Exited (0) 7 days ago                          admiring_franklin
3775b792faf0        centos              "/bin/bash"         7 days ago          Exited (0) 7 days ago                          trusting_mayer
[root@localhost ~]#

To check the downloaded image repositories...

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu    latest              f753707788c5        2 weeks ago         127.1 MB
docker.io/centos    latest              980e0e4c79ec        7 weeks ago         196.7 MB
[root@localhost ~]#

So far this is what I worked for the concept of Dockers, Hope soon I will post some other posts related to Docker concept.

######################################################################################

No comments:

Post a Comment