Getting Started with Docker


How To Install and Use Docker

Docker is an open-source project that makes creating and managing Linux containers really easy. Containers are like extremely lightweight VMs – they allow code to run in isolation from other containers but safely share the machine’s resources, all without the overhead of a hypervisor.

Docker-linux-interfaces

While a Virtual Machine is a whole other guest computer running on top of your host computer (sitting on top of a layer of virtualization), Docker is an isolated portion of the host computer, sharing the host kernel (OS) and even its bin/libraries if appropriate & it makes it possible to get far more apps running on the same old servers and it also makes it very easy to package and ship programs.

Container Vs VM’s 

docker-vm-container

 

Pickup this image for official docker’s website.

Prerequisites:

1. Physical machine or a VM running Fedora 21.
2. Yum should be configured.
3. Enough System resources.
4. Update all packages to the latest versions available.

In my scenario: 

I am using MacBook Air as Physical Machine & for Virtulization using VMWare Fusion & on top of it Fedora 21 as guest OS, where i am about to play with docker; You can see the beauty of Virtuliztaion, it always helping me to try the hell of the new technologies with minimum huddles & maximum outcome.

Getting Started with Docker.

Docker has a CLI Interface which provide us more flexibility & agility to play with containers.

Step 1: Installation of Docker

[root@fedora21 ~]# yum remove docker -y 
[root@fedora21 ~]# yum install docker-io -y
[root@fedora21 ~]# systemctl start docker 
[root@fedora21 ~]# systemctl enable docker
[root@fedora21 ~]# systemctl status docker 
 docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled)
Active: active (running) since Sun 2015-02-15 22:42:38 IST; 1h 14min ago
Docs: http://docs.docker.com
Main PID: 2187 (docker)
CGroup: /system.slice/docker.service
└─2187 /usr/bin/docker -d --selinux-enabled
Feb 15 23:38:52 localhost.localdomain docker[2187]: time="2015-02-15T23:38:52+05:30" level="info" msg="+job containers()"
Feb 15 23:38:52 localhost.localdomain docker[2187]: time="2015-02-15T23:38:52+05:30" level="info" msg="-job containers() = OK (0)"
Feb 15 23:39:14 localhost.localdomain docker[2187]: time="2015-02-15T23:39:14+05:30" level="info" msg="DELETE /v1.16/containers/2cf3d2fc409a"
Feb 15 23:39:14 localhost.localdomain docker[2187]: time="2015-02-15T23:39:14+05:30" level="info" msg="+job rm(2cf3d2fc409a)"
Feb 15 23:39:15 localhost.localdomain docker[2187]: time="2015-02-15T23:39:15+05:30" level="info" msg="+job log(destroy, 2cf3d2fc409ad00cf84fbfc41ae491a613de445aeebbe5f50354b22097ac4288, fedora:21)"Feb 15 23:39:15 localhost.localdomain docker[2187]: time="2015-02-15T23:39:15+05:30" level="info" msg="-job log(destroy, 2cf3d2fc409ad00cf84fbfc41ae491a613de445aeebbe5f50354b22097ac4288, f...21) = OK (0)"
Feb 15 23:39:15 localhost.localdomain docker[2187]: time="2015-02-15T23:39:15+05:30" level="info" msg="-job rm(2cf3d2fc409a) = OK (0)"Feb 15 23:39:17 localhost.localdomain docker[2187]: time="2015-02-15T23:39:17+05:30" level="info" msg="GET /v1.16/containers/json?all=1"
Feb 15 23:39:17 localhost.localdomain docker[2187]: time="2015-02-15T23:39:17+05:30" level="info" msg="+job containers()"
Feb 15 23:39:17 localhost.localdomain docker[2187]: time="2015-02-15T23:39:17+05:30" level="info" msg="-job containers() = OK (0)"
Hint: Some lines were ellipsized, use -l to show in full.
[root@fedora21 ~]#
[root@fedora21 ~]# docker version
Client version: 1.4.1
Client API version: 1.16
Go version (client): go1.3.3
Git commit (client): 5bc2ff8/1.4.1
OS/Arch (client): linux/amd64
Server version: 1.4.1
Server API version: 1.16
Go version (server): go1.3.3
Git commit (server): 5bc2ff8/1.4.1
[root@fedora21 ~]#

 

Step 2:  Download a Docker Container or Launching a Container 

Launching a container is simple as docker run + the image name you would like to run + the command to run within the container. If the image doesn’t exist on your local machine, docker will attempt to fetch it from the public image registry.

[root@fedora21 ~]#docker run -i -t ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
ubuntu:latest: The image you are pulling has been verified
27d47432a69b: Pulling fs layer 
27d47432a69b: Downloading [======>                                            ] 27d47432a69b: Pull complete 
5f92234dcf1e: Pull complete 
51a9c7c1f8bb: Pull complete 
5ba9dab47459: Pull complete 
Status: Downloaded newer image for ubuntu:latest
root@b531b2c59e2a:/# 
root@b531b2c59e2a:/# uname -a 
Linux b531b2c59e2a 3.18.5-201.fc21.x86_64 #1 SMP Mon Feb 2 21:00:58 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
root@b531b2c59e2a:/# cat /etc/*release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.1 LTS"
NAME="Ubuntu"
VERSION="14.04.1 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.1 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
root@b531b2c59e2a:/# exit

Command Syntax

1. docker run - Run a container
2. -t - Allocate a (pseudo) tty
3. -i - Keep stdin open (so we can interact with it)
4. ubuntu/fedora - use the Ubuntu base image
5. /bin/bash - Run the bash shell

Step 3: Running a container as per the requirement

Example 1:  Running a container in interactive mode. 

[root@fedora21 ~]#docker run -i -t fedora /bin/bash
bash-4.3# ls -ltr
total 52
drwxr-xr-x.   2 root root 4096 Aug 16  2014 srv
lrwxrwxrwx.   1 root root    8 Aug 16  2014 sbin -> usr/sbin
drwxr-xr-x.   2 root root 4096 Aug 16  2014 opt
drwxr-xr-x.   2 root root 4096 Aug 16  2014 mnt
drwxr-xr-x.   2 root root 4096 Aug 16  2014 media
lrwxrwxrwx.   1 root root    9 Aug 16  2014 lib64 -> usr/lib64
lrwxrwxrwx.   1 root root    7 Aug 16  2014 lib -> usr/lib
drwxr-xr-x.   2 root root 4096 Aug 16  2014 home
lrwxrwxrwx.   1 root root    7 Aug 16  2014 bin -> usr/bin
drwx------.   2 root root 4096 Dec  3 00:56 lost+found
drwxr-xr-x.   2 root root 4096 Dec  3 00:56 run
drwxr-xr-x.  12 root root 4096 Dec  3 00:56 usr
drwxr-xr-x.  18 root root 4096 Dec  3 00:56 var
dr-xr-xr-x.   3 root root 4096 Dec  3 00:56 boot
drwxrwxrwt.   7 root root 4096 Dec  3 00:58 tmp
dr-xr-x---.   2 root root 4096 Dec  3 00:58 root
dr-xr-xr-x.  13 root root    0 Feb 15 12:10 sys
drwxr-xr-x.  47 root root 4096 Feb 15 14:11 etc
dr-xr-xr-x. 149 root root    0 Feb 15 14:11 proc
drwxr-xr-x.   5 root root  380 Feb 15 14:11 dev
bash-4.3# echo "Hello Fedora Docker Container"  > /etc/motd
bash-4.3#

 Example 2:  Running a container in detached mode. 

[root@fedora21 ~]# docker run -d ubuntu /bin/ping 192.168.13.1
f74539d082e5721f46c389e27ac9a431af4fa529b180e9c09dec1a1302b859fb

To ensure that weather the ping command is working or not, i had run the following command on my base node; Guess what got the +tv results

[root@fedora21 ~]# tcpdump ip proto \\icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on docker0, link-type EN10MB (Ethernet), capture size 262144 bytes
23:18:22.551399 IP 172.17.0.13 > 192.168.13.1: ICMP echo request, id 1, seq 206, length 64
23:18:22.551909 IP 192.168.13.1 > 172.17.0.13: ICMP echo reply, id 1, seq 206, length 64
23:18:23.552167 IP 172.17.0.13 > 192.168.13.1: ICMP echo request, id 1, seq 207, length 64
23:18:23.552595 IP 192.168.13.1 > 172.17.0.13: ICMP echo reply, id 1, seq 207, length 64
23:18:24.552610 IP 172.17.0.13 > 192.168.13.1: ICMP echo request, id 1, seq 208, length 64
23:18:24.553088 IP 192.168.13.1 > 172.17.0.13: ICMP echo reply, id 1, seq 208, length 64

Step 4: Check the status of running container 

[root@fedora21 ~]# docker ps 
CONTAINER ID        IMAGE                                          COMMAND                CREATED             STATUS              PORTS               NAMES
5928bf0b2f69        registry.amitvashist.com:5000/apache1:latest   "/bin/ping 192.168.1   36 seconds ago      Up 35 seconds                           stupefied_sinoussi   
2710f6001dc9        fedora/welcome_message_motd:latest             "/bin/bash"            15 minutes ago      Up 15 minutes                           clever_sinoussi      
[root@fedora21 ~]#

Step 5: List all the available  container Images. 

[root@fedora21 ~]# docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu                     latest              5ba9dab47459        2 weeks ago         192.7 MB
ubuntu                     14.04               8eaa4ff06b53        6 weeks ago         192.7 MB
fedora                     21                  834629358fe2        6 weeks ago         250.2 MB
fedora                     latest              834629358fe2        6 weeks ago         250.2 MB

Step 6: Committing  a container.

[root@fedora21 ~]# docker ps
CONTAINER ID        IMAGE                                          COMMAND                CREATED             STATUS              PORTS               NAMES
a27ccfca80ee        ubuntu:latest                                  "/bin/ping 192.168.1   4 minutes ago       Up 4 minutes                            kickass_lovelace     
2710f6001dc9        fedora/welcome_message_motd:latest             "/bin/bash"            23 minutes ago      Up 23 minutes                           clever_sinoussi      
[root@fedora21 ~]# docker commit a27ccfca80ee ubuntu/ping 
5eecea7c9b00e24ee2257a2816f929ddf720c0936d29301d3b7cbd4cdc1b2647
[root@fedora21 ~]#

 Step 7: Tacking the changes in container, it work like subversion/ Git / CVS – for software/ change management.  

[root@fedora21 ~]# docker ps 
CONTAINER ID        IMAGE                                          COMMAND                CREATED             STATUS              PORTS               NAMES
a27ccfca80ee        ubuntu:latest                                  "/bin/ping 192.168.1   8 minutes ago       Up 8 minutes                            kickass_lovelace      
2710f6001dc9        fedora/welcome_message_motd:latest             "/bin/bash"            28 minutes ago      Up 28 minutes                           clever_sinoussi
[root@fedora21 ~]# docker diff 2710f6001dc9
C /etc
C /etc/gshadow-
C /etc/group-
C /etc/gshadow
C /etc/group
C /etc/shadow-
C /etc/shadow
C /etc/passwd
C /home
A /home/amitvashist
A /home/amitvashist/.bash_logout
A /home/amitvashist/.bash_profile
A /home/amitvashist/.bashrc
C /var
C /var/log
C /var/log/lastlog
A /var/test1-usr
C /var/spool
C /var/spool/mail
A /var/spool/mail/amitvashist
A /amitvashist
A /amitvashist/test1
C /usr
A /usr/test1-usr
[root@fedora21 ~]#

Step 7: Attach to a running container.

[root@fedora21 ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
a27ccfca80ee        ubuntu:latest       "/bin/ping 192.168.1   29 minutes ago      Up 29 minutes                           kickass_lovelace    
[root@fedora21 ~]# 

[root@fedora21 ~]# docker attach a27ccfca80ee
64 bytes from 192.168.13.1: icmp_seq=1891 ttl=63 time=0.296 ms
64 bytes from 192.168.13.1: icmp_seq=1892 ttl=63 time=0.293 ms
64 bytes from 192.168.13.1: icmp_seq=1893 ttl=63 time=0.299 ms
64 bytes from 192.168.13.1: icmp_seq=1894 ttl=63 time=0.319 ms

 Step 7: To stop/start the container.

[root@fedora21 ~]# docker stop 9932068eace4

[root@fedora21 ~]# docker start 9932068eace4
[root@fedora21 ~]# docker ps 
CONTAINER ID        IMAGE                                COMMAND             CREATED             STATUS              PORTS               NAMES
9932068eace4        fedora/welcome_message_motd:latest   "/bin/bash"         5 minutes ago       Up 3 seconds                            adoring_leakey      
[root@fedora21 ~]#

 Step 8: Check the history of an image

[root@fedora21 ~]# docker history ubuntu/ping
IMAGE               CREATED             CREATED BY                                      SIZE
943aad725b0e        7 minutes ago       /bin/ping 192.168.13.1                          0 B
5ba9dab47459        2 weeks ago         /bin/sh -c #(nop) CMD [/bin/bash]               0 B
51a9c7c1f8bb        2 weeks ago         /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$/   1.895 kB
5f92234dcf1e        2 weeks ago         /bin/sh -c echo '#!/bin/sh' > /usr/sbin/polic   194.5 kB
27d47432a69b        2 weeks ago         /bin/sh -c #(nop) ADD file:62400a49cced0d7521   192.5 MB
511136ea3c5a        20 months ago                                                       0 B
[root@fedora21 ~]#

Step 9:  Return low-level information on a container

[root@fedora21 ~]# docker inspect f3c24e745e58
[{
  "AppArmorProfile": "",
   "Args": [
             "192.168.13.1"
                            ],
"Config": {
"AttachStderr": false,
"AttachStdin": false,
"AttachStdout": false,
"Cmd": [
"/bin/ping",
 "192.168.13.1"
                ],

Step 10:  To remove a container

[root@fedora21 ~]# docker rm 81ba74620eb5
81ba74620eb5
[root@fedora21 ~]#

Step 11:  To remove an image 

[root@fedora21 ~]# docker rmi 5eecea7c9b00
Deleted: 5eecea7c9b00e24ee2257a2816f929ddf720c0936d29301d3b7cbd4cdc1b2647
[root@fedora21 ~]#

Step12 : In order to remove all the containers

[root@fedora21 ~]# docker rm $(docker ps -a -q)
Error response from daemon: You cannot remove a running container. Stop the container before attempting removal or use -f
de3b5b0e20d0
afef4470e5e9
9932068eace4
e807c9827932
67867ed15600
a3385cea9e77
e3ed676fbc6e
a27ccfca80ee
5928bf0b2f69
2710f6001dc9
1f480ed879a7

 Other useful docker commands:

attach    Attach to a running container
build     Build a container from a Dockerfile
commit    Create a new image from a container's changes
cp        Copy files/folders from the containers filesystem to the host path
diff      Inspect changes on a container's filesystem
events    Get real time events from the server
export    Stream the contents of a container as a tar archive
history   Show the history of an image
images    List images
import    Create a new filesystem image from the contents of a tarball
info      Display system-wide information
insert    Insert a file in an image
inspect   Return low-level information on a container
kill      Kill a running container
load      Load an image from a tar archive
login     Register or Login to the docker registry server
logs      Fetch the logs of a container
port      Lookup the public-facing port which is NAT-ed to PRIVATE_PORT
ps        List containers
pull      Pull an image or a repository from the docker registry server
push      Push an image or a repository to the docker registry server
restart   Restart a running 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 to a tar archive
search    Search for an image in the docker index
start     Start a stopped container
stop      Stop a running container
tag       Tag an image into a repository
top       Lookup the running processes of a container
version   Show the docker version information
wait      Block until a container stops, then print its exit code

The best way to understand Docker is to try it!

Online Tutorial Link : https://www.docker.com/tryit/

Happy learning 🙂 🙂

And Please do share your comments 🙂

Cheers!!!

Advertisements

About Amit Vashist

Amit Vashist is someone who brings with him a treasure full of experience of over 8 years in open source technologies. When it comes to virtualization he has single handedly managed end-to-end migration projects in KVM and Xen that involved right from sizing the systems to P2V of existing physical servers. He understands what can go wrong in virtualized world and how to take care of it. He also has root level knowledge on Red Hat platforms and has commissioned & Lamp; Provides Corporate Training over Red Hat HA clusters. Now days Supporting Telecom Giant Billing & Support system, gaining experience on JBoss, Splunk, SQL, Nagios, Apache & Fuse ..etc.
This entry was posted in Linux, Virtualization and tagged , , , , , . Bookmark the permalink.

Please share your valuable suggestions/comments..!!!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s