Join our Discord Server
Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Distinguished Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 700+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 9800+ members and discord server close to 2600+ members. You can follow him on Twitter(@ajeetsraina).

Getting Started with Docker – Part-1

4 min read

This is a step by step guide for getting started with 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:-

[simterm]
$ 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/”

[/simterm]

Update your CentOS System using the below command:

[simterm]

$yum update

[/simterm]

Let us install Docker on this system:

[simterm]

$yum install docker

[/simterm]

To verify if Docker is properly installed or not:

[simterm]

$docker version

[/simterm]

The above command should display docker server and client version.

Next, we need to start docker daemon:

[simterm]

$systemctl start docker

[/simterm]

We haven’t pulled any images yet from Dockerhub. hence, the below command shows empty:

[simterm]
# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE

[/simterm]

The command `docker info` too can help you in displaying docker information:

[simterm]
[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/]

[/simterm]

How does Docker directory structure look like?

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

[/simterm]
How to pull image from Docker Hub repository?

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

[simterm]
[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

[/simterm]

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

[simterm]
[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

[/simterm]

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:

[simterm]
[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 ~]#

[/simterm]

How to attach to the container?

[simterm]
$docker attach ac6b0eaa3203
FATA[0000] Error response from daemon: No such container: ac6b0eaa3203
[root@localhost ~]#
[/simterm]

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.

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

[/simterm]

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.

[simterm]
[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 ~]#

[/simterm]

Hence it showed that new docker container with name “collabnix” is up and running.
You can attach it to a new running container now as shown below:

[simterm]
[root@localhost ~]# docker attach collabnix

[/simterm]

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.

[simterm]
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)”

[/simterm]

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:

[simterm]
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:/#

[/simterm]

Checking the container memory:

[simterm]
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:/#
[/simterm]

Checking the container kernel:

[simterm]
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

[/simterm]

How to Stop and start Container?

[simterm]
[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 ~]#

[/simterm]

How to restart the container?

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

[/simterm]

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

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Distinguished Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 700+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 9800+ members and discord server close to 2600+ members. You can follow him on Twitter(@ajeetsraina).
Join our Discord Server