A Laymen Guide to Docker – Part-I

This is a step by step guide for learning the basics of Docker. This guide starts with a fresh CentOS 7.0 host machine, talks about how to play around with images and containers, how to start and stop the containers, how to build containers through Dockerfile and finally how to build applications with number of containers working closely and together.

I assume that a fresh CentOS 7.0 machine is installed on the bare metal machine or VM. It’s better to take a VM (either ESXi or Virtualbox) and start experimenting with the VM.My CentOS 7.0 VM looks like:-

[root@localhost ~]# cat /etc/os-release
NAME=”CentOS Linux”
VERSION=”7 (Core)”
ID=”centos”
ID_LIKE=”rhel fedora”
VERSION_ID=”7″
PRETTY_NAME=”CentOS Linux 7 (Core)”
ANSI_COLOR=”0;31″
CPE_NAME=”cpe:/o:centos:centos:7″
HOME_URL=”https://www.centos.org/”
BUG_REPORT_URL=”https://bugs.centos.org/”

I assume that docker is already installed. Let’s verify it through the following command:-

[root@puppetmastr ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE

As of now, there is no Docker image present as shown below:

[root@localhost ~]# docker info
Containers: 20
Images: 0
Storage Driver: devicemapper
Pool Name: docker-253:1-18305751-pool
Pool Blocksize: 65.54 kB
Backing Filesystem: xfs
Data file: /dev/loop0
Metadata file: /dev/loop1
Data Space Used: 6.264 GB
Data Space Total: 107.4 GB
Data Space Available: 3.223 GB
Metadata Space Used: 8.335 MB
Metadata Space Total: 2.147 GB
Metadata Space Available: 2.139 GB
Udev Sync Supported: true
Data loop file: /var/lib/docker/devicemapper/devicemapper/data
Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
Library Version: 1.02.93-RHEL7 (2015-01-28)
Execution Driver: native-0.2
Kernel Version: 3.10.0-123.20.1.el7.x86_64
Operating System: CentOS Linux 7 (Core)
CPUs: 1
Total Memory: 1.955 GiB
Name: localhost.localdomain
ID: 62P4:6ZHB:CROF:SLXK:AY62:3P3A:J7HY:YRDH:GOX5:3FYN:OIFP:2DZT
Username: ajeetraina
Registry: [https://index.docker.io/v1/]

How does Docker directory structure look like?

[root@localhost containers]# pwd
/var/lib/docker/containers
[root@localhost containers]# ls
48c3873ab315135535f00da362bdc11462d16862ab5c8c111e458c21534a63d7
[root@localhost containers]#

How to pull image from Docker Hub repository?

Let’s pull a newer Ubuntu Image as shown below:

[root@localhost ~]# docker pull ubuntu:precise
Pulling repository ubuntu
ac6b0eaa3203: Pulling image (precise) from ubuntu, endpoint: https://registry-1.ac6b0eaa3203: Download complete
c50f7e0846e6: Download complete
fd248b999044: Download complete
9f91f850df24: Download complete
Status: Downloaded newer image for ubuntu:precise

Lets verify with Docker image command to see what images it pulled:

[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu 12.04 ac6b0eaa3203 2 weeks ago 132.4 MB
ubuntu precise ac6b0eaa3203 2 weeks ago 132.4 MB
ubuntu precise-20150427 ac6b0eaa3203 2 weeks ago 132.4 MB
ubuntu 12.04.5 ac6b0eaa3203 2 weeks ago 132.4 MB
blalor/centos latest f01c1b138488 10 months ago 322.4 MB

Till now, we dont have any container yet running. It is just an image which got pulled from the remote Docker hub registry.

Lets see what does Docker info shows now:
[root@localhost ~]# docker info
Containers: 3
Images: 7
Storage Driver: devicemapper
Pool Name: docker-253:0-130986-pool
Pool Blocksize: 65.54 kB
Backing Filesystem: extfs
Data file: /dev/loop0
Metadata file: /dev/loop1
Data Space Used: 3.115 GB
Data Space Total: 107.4 GB
Metadata Space Used: 2.499 MB
Metadata Space Total: 2.147 GB
Udev Sync Supported: true
Data loop file: /var/lib/docker/devicemapper/devicemapper/data
Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
Library Version: 1.02.89-RHEL6 (2014-09-01)
Execution Driver: native-0.2
Kernel Version: 2.6.32-504.16.2.el6.x86_64
Operating System: <unknown>
CPUs: 4
Total Memory: 1.833 GiB
Name: localhost.localdomain
ID: ZJOP:G67F:GC6E:UUAN:HDQD:VB7D:VKWE:56YF:2N2P:C4O6:XIZB:U52K
[root@localhost ~]#
How to attach to the container?
$docker attach ac6b0eaa3203
FATA[0000] Error response from daemon: No such container: ac6b0eaa3203
[root@localhost ~]#

Why the error? because the container is not in running state, we cannot attach to stopped container.

Lets start the container and provide a new name too. I would name it as collabnix.

docker run -it –name collabnix ac6b0eaa3203 /bin/bash
root@4e21f056056e:/#

Please remember that if you pressed Ctrl+D to come out of the container, the container stops running. To keep container running and if you want to come out of the container, press Ctrl+P+Q. This will keep it running in the background.

Lets check it here under docker ps command.
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
4e21f056056e ubuntu:12.04 “/bin/bash” About a minute ago Up About a minute
collabnix
[root@localhost ~]#

Hence it showed that new name collabnix has been put for the container.
You can attach it to a new running container now with the name collabnix as shown below:

[root@localhost ~]# docker attach collabnix

Running the command will take you inside the container. We can verify that we are inside the container through the following command:

Important Point: Say, I am already attached to the container and some application packages are being installed. While I try to attach again through the different terminal, the same window will get displayed. Did you remember screen command?
Yeh, it is somewhat the same.

root@4e21f056056e:/#
root@4e21f056056e:/# cat /etc/os-release
NAME=”Ubuntu”
VERSION=”12.04.5 LTS, Precise Pangolin”
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME=”Ubuntu precise (12.04.5 LTS)”

Hence we have a running container called collabnix which we can deattach and attach to successfully and in an easy manner.

Looking inside the container

Lets see how does the container look like:

root@4e21f056056e:/# df -h
Filesystem Size Used Avail Use%
Mounted on
rootfs 9.8G 168M 9.1G 2%
/
/dev/mapper/docker-253:0-130986-4e21f056056ed168a5518a8db46d99099f3b436d0bc59e9e72529b6bd62109cd 9.8G 168M 9.1G 2%
/
tmpfs 939M 0 939M 0%
/dev
shm 64M 0 64M 0%
/dev/shm
/dev/mapper/VolGroup-lv_root 12G 4.8G 6.0G 45%
/etc/resolv.conf
/dev/mapper/VolGroup-lv_root 12G 4.8G 6.0G 45%
/etc/hostname
/dev/mapper/VolGroup-lv_root 12G 4.8G 6.0G 45%
/etc/hosts
tmpfs 939M 0 939M 0%
/proc/kcore
root@4e21f056056e:/#

Checking the container memory:

root@4e21f056056e:/# free -m
total used free shared buffers cached
Mem: 1876 668 1208 0 14 545
-/+ buffers/cache: 108 1768
Swap: 4031 0 4031
root@4e21f056056e:/#

Checking the container kernel:

root@4e21f056056e:/# uname -arn
Linux 4e21f056056e 2.6.32-504.16.2.el6.x86_64 #1 SMP Wed Apr 22 06:48:29 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

How to Stop and start Container?

[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
4e21f056056e ubuntu:12.04 “/bin/bash” 9 minutes ago Up 9 minutes
collabnix
[root@localhost ~]# docker stop collabnix
collabnix
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
[root@localhost ~]# docker start collabnix
collabnix
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
4e21f056056e ubuntu:12.04 “/bin/bash” 12 minutes ago Up 4 seconds
collabnix
[root@localhost ~]#

How to restart the container?

[root@localhost ~]# docker restart collabnix
collabnix
[root@localhost ~]#

In our next post, we will discuss the advance topics on Docker containers.

6 thoughts on “A Laymen Guide to Docker – Part-I

  1. Youre so cool! I dont suppose Ive learn something like this before. So good to seek out anyone with some unique thoughts on this subject. realy thanks for starting this up. this web site is something that’s needed on the net, somebody with somewhat originality. useful job for bringing one thing new to the web! http://perfectslots2016.ru

  2. I think that is among the most significant info for me. And i’m glad studying your article. However wanna commentary on some basic issues, The web site style is perfect, the articles is actually great : D. Excellent task, cheers|

  3. I just want to tell you that I am new to blogging and seriously loved this web-site. Probably I’m want to bookmark your website . You certainly come with superb articles. Regards for revealing your web page.

  4. Heya i’m for the first time here. I found this board and I find It really useful & it helped me out much. I hope to give something back and aid others like you helped me.

  5. There’s definately a lot to know about this subject. I like all the points you have made.|

  6. Hiya, I’m really glad I’ve found this info. Nowadays bloggers publish just about gossips and internet and this is actually irritating. A good web site with interesting content, that’s what I need. Thank you for keeping this web site, I’ll be visiting it. Do you do newsletters? Can not find it.

Leave a Reply

Your email address will not be published. Required fields are marked *