Thursday, 25 September 2014

Managing container - Docker command line #Redhat 7 / CentOS 7

In previous article I had written about the Linux Container archtecture and hereby, I am letting you know on how docker as a management interface could be used as a command line.

Ensure that the docker package is installed on your system else install.

#yum install docker

After you successfully installed the application, use the usual system ctl commands to start docker and to make it run automatically at boot time.

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

Search for an existing image which you are looking for, the command searches the Index which is currently the main public registry for sharing repository images where few of them are marked as trusted which means officially checked.

# docker search centos
NAME                                            DESCRIPTION                                     STARS     OFFICIAL   TRUSTED
centos                                          The official build of CentOS.                   463            [OK]       

To download a selected image from the remote registry to your local machine pull repository_name/image_name

# docker pull centos

Pulling repository centos
70214e5d0a90: Download complete 
5a1ebaa356ff: Download complete 
68eb857ffb51: Download complete 
511136ea3c5a: Download complete 
34e94e67e63a: Download complete 

Listing the images 

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              centos5             5a1ebaa356ff        2 weeks ago         484 MB
centos              centos7             70214e5d0a90        2 weeks ago         224 MB
centos              latest              70214e5d0a90        2 weeks ago         224 MB
centos              centos6             68eb857ffb51        2 weeks ago         212.7 MB

To remove one or more images from your system, 
#docker rmi image_name

Managing Containers

To creating a new container, replace the given container name and specify the image on top of which will the container run. The docker run command lets you say which command to run in a container. Once the container is running, you can manage [ start/stop/restart] accordingly. you could also remove if no longer needed. 

The command that you pass on the docker run will see to it that the command runs inside the container as its running environment, hence very little can be seen from the host system. 

# docker run --rm --name=dockertest centos ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
7: eth0: <NO-CARRIER,BROADCAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 4a:69:d0:e6:b7:2e brd ff:ff:ff:ff:ff:ff
    inet scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::4869:d0ff:fee6:b72e/64 scope link tentative 
       valid_lft forever preferred_lft forever

If you want to make a directory from the host available to the container, map network ports from the container to the host which can be done by docker run command line.

# mkdir -p /docker/centos7
# echo "Docker testing" >/docker/centos7/dockertest.txt
# docker run -d -p 8080:6000 --name="dockerwebserver" -w /docker -v /docker/centos7:/docker centos /bin/python -m SimpleHTTPServer 6000

Detach (-d) the container so it runs in the background
Map (-p) TCP port 6000 on the container to port 8080 on the host
Map Working directory(-w /docker) in the container when the command runs.
Map directory from the host (-v /docker/centos7) to the container
HTTPserver module(-m) with /bin/python command.
# netstat -tupln | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      2006/docker         

To execute commands inside of a running container, you need to connect to it through a command-line interface. T he docker attach command is not suitable for this, since it only lets you observe the standard output of the application currently running in the container. Instead, use the nsenter command to enter the container namespace. T his command requires the ID of the container as it appears on the host system.

# docker ps -a

CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                    NAMES
acbc99f8b64b        centos:centos7      /bin/python -m Simpl   11 minutes ago      Up 11 minutes>6000/tcp   dockerwebserver     

# docker inspect -f {{.State.Pid}} acbc99f8b64b

# nsenter -m -u -n -i -p -t 4526 /bin/sh
sh-4.2# cat /etc/centos-release 
CentOS Linux release 7.0.1406 (Core) 

sh-4.2# ps -ef
root         1     0  0 13:35 ?        00:00:00 /bin/python -m SimpleHTTPServer 6000
root         6     0  0 13:49 ?        00:00:00 /bin/sh
root         9     6  0 13:50 ?        00:00:00 ps -ef
sh-4.2# uptime
13:50:33 up  2:16,  0 users,  load average: 0.00, 0.01, 0.05

sh-4.2# free -m
             total       used       free     shared    buffers     cached
Mem:           994        318        676          6          2        123
-/+ buffers/cache:        192        802
Swap:         1023          0       1023
sh-4.2# df -h
Filesystem                                                                                         Size  Used Avail Use% Mounted on
/dev/mapper/docker-253:3-3148627-acbc99f8b64b8f3472a6740a2d22ba10ab42ff2615c6adda08ca52236293e136  9.8G  280M  9.0G   3% /
tmpfs                                                                                              498M     0  498M   0% /dev
shm                                                                                                 64M     0   64M   0% /dev/shm
/dev/mapper/VolGroup-varlv                                                                         6.0G  1.5G  4.6G  25% /etc/hosts
/dev/mapper/VolGroup-rootlv                                                                        4.9G  102M  4.8G   3% /docker
tmpfs                                                                                              498M     0  498M   0% /proc/kcore

List/stop/start/restart/Monitoring the containers

# docker ps
CONTAINER ID        IMAGE               COMMAND                     CREATED             STATUS              PORTS               NAMES
acbc99f8b64b        centos:centos7      /bin/python -m Simpl   20 minutes ago      Up 20 minutes>6000/tcp   dockerwebserver     

# docker stop dockerwebserver

# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

# docker start dockerwebserver

# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                    NAMES
acbc99f8b64b        centos:centos7      /bin/python -m Simpl   22 minutes ago      Up 2 seconds>6000/tcp   dockerwebserver     

# docker restart dockerwebserver

# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                    NAMES
acbc99f8b64b        centos:centos7      /bin/python -m Simpl   22 minutes ago      Up 3 seconds>6000/tcp   dockerwebserver     

Commit containers

To create a new image from changes made in the running container,syntax as below 
docker commit container_name [repository_name:tag] 

# docker commit dockerwebserver centos:dockerwebserver_V1.0

# docker images
REPOSITORY          TAG                    IMAGE ID            CREATED             VIRTUAL SIZE
centos              dockerwebserver_V1.0   f7c53eac7879        10 seconds ago      224 MB
centos              centos5                5a1ebaa356ff        2 weeks ago         484 MB
centos              centos7                70214e5d0a90        2 weeks ago         224 MB
centos              latest                 70214e5d0a90        2 weeks ago         224 MB
centos              centos6                68eb857ffb51        2 weeks ago         212.7 MB

View containers

To view an overall information on how Docker is configured on your system

# docker info
Containers: 1
Images: 6
Storage Driver: devicemapper
 Pool Name: docker-253:3-3148627-pool
 Data file: /var/lib/docker/devicemapper/devicemapper/data
 Metadata file: /var/lib/docker/devicemapper/devicemapper/metadata
 Data Space Used: 1365.8 Mb
 Data Space Total: 102400.0 Mb
 Metadata Space Used: 1.4 Mb
 Metadata Space Total: 2048.0 Mb
Execution Driver: native-0.2
Kernel Version: 3.10.0-123.el7.x86_64

To display a detailed information about an image or a container
# docker inspect acbc99f8b64b
    "ID": "acbc99f8b64b8f3472a6740a2d22ba10ab42ff2615c6adda08ca52236293e136",
    "Created": "2014-09-25T12:35:20.804560542Z",
    "Path": "/bin/python",

Archive containers/images

Archive files are useful for backing up or restoring containers and images. Note that you can not backup 
data volumes this way since they are external to containers. 
To export the contents of a container file system as an archive in tar compress format.

# docker ps 
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                    NAMES
acbc99f8b64b        centos:centos7      /bin/python -m Simpl   46 minutes ago      Up 23 minutes>6000/tcp   dockerwebserver     

# docker export dockerwebserver > centos-webserver-docker-7.0-x86_68
# tar -cvf centos-webserver-docker-7.0-x86_68.tar centos-webserver-docker-7.0-x86_68
# gzip centos-webserver-docker-7.0-x86_68.tar
# du -sh rhel-server-docker-7.0-21.4-x86_64.tar.gz 

Conversely, you can import an content from an URL or an tar archive as below
#docker import <source>

On conclusion, summarizing the docker command reference.

Docker commands Descriptions
docker version Find the version number currently installed
docker search <name> Search for existing images
docker pull repository_name/image_name Download a selected image from remote to your local machine
docker images Lists all locally installed image
docker push Copy image or repository to a remote location
docker rmi image_name Remove one or more images from your system
docker commit container_name create a new image from changes made in the running container
docker stop container_name stop the running container gracefully
docker kill container_name stop a container that is not responding
docker start container_name start a previously stopped container
docker restart container_name Restart a running container
docker rm container_name remove a container
docker ps Listing containers
docker info overall information on how Docker is configured on your system
docker top container_name dynamic view of processes currently running inside of a certain container
docker export container_name export the contents of a container file system as an archive in tar compress format