Sunday, December 10, 2017

The Docker way...

What is Docker Container?



Docker is an open-source engine that automates the deployment of any application as a lightweight, portable, self-sufficient container that will run virtually anywhere.

Docker containers can encapsulate any payload, and will run consistently on and between virtually any server. The same container that a developer buildsand tests on a laptop will run at scale, in production*, on VMs, bare-metal servers, OpenStack clusters, public instances, or combinations of the above.


[Source: As in package description, Vendor: Fedora Project]








Difference between Docker and VM [Pic source, Internet ]


Let's try out few basic stuff using Docker (Tested on a CentOS-6x based VM);


1. Install epel repo to get Docker-io


[root@docker-vm ~]# yum install epel-release -y


2. Install docker-io package and start the service;


[root@docker-vm ~]# yum install docker-io -y


[root@docker-vm ~]# service docker start
Starting cgconfig service:           [  OK  ]
Starting docker:                        [  OK  ]

3. List if we have any Docker image avaialble locally;

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


4. Since images are not available locally, I would pull a CentOS-6.9 image and try out few things;



Pulling a specific Centos docker image version;

[root@docker-vm ~]# docker pull centos:6.9
6.9: Pulling from centos

055b9989266a: Pull complete

f1070d829305: Pull complete
e071bce628ba: Pull complete
Digest: sha256:e7bdc458659b6e644ae85694f2baaf3727c06ad82186fca80f4e3a8e88907cc3
Status: Downloaded newer image for centos:6.9

5. List images once more;

[root@docker-vm ~]# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE8.
centos              6.9                 e071bce628ba        5 weeks ago         194.7 MB

6. Now run our first container [docker run -it stands for docker run --interactive --tty, do a man docker-run for options] ;


[root@docker-vm ~]# docker run -it e071bce628ba /bin/bash
[root@bd93eb2b1ccf /]#

["e071bce628ba" is the IMAGE ID]


7. Now, within our container, let's install few packages [eg. iproute, httpd, nmap etc.];

[root@bd93eb2b1ccf /]# yum install iproute nmap httpd -y

8. Start HTTPD ;
[root@bd93eb2b1ccf /]# service httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.1 for ServerName                                                [  OK  ]
9. Open a new terminal on the Host system and check what containers are running;

[centos@docker-vm ~]$ sudo docker ps -a

CONTAINER ID        IMAGE       COMMAND             CREATED             STATUS              PORTS               NAMES
bd93eb2b1ccf        e071bce628ba        "/bin/bash"         17 minutes ago      Up 17 minutes                           prickly_tesla
So, we have one running container from previous steps;

10. Detach and attach from/to an Docker container;


A. Detach from a container;

Press Ctrl-P + Ctrl-Q to detach from the container;

[root@docker-vm ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
bd93eb2b1ccf        e071bce628ba        "/bin/bash"         24 minutes ago      Up 24 minutes                           prickly_tesla

B. Once detached, you can again connect to the running container. You need to provide the "CONTAINER ID" to attach to the reqd container;

[root@docker-vm ~]# docker attach bd93eb2b1ccf

[root@bd93eb2b1ccf /]#



11. Now, we will save our custom image with a different name [I named it as 
"centos6.9-httpd-bijit"]  with all our custom packages (remember we installed iproute, httpd, nmap etc)

Detach yourself [ctrl-p + ctrl-q] from the running container and save the container as new 
Docker image;

[root@docker-vm ~]# docker ps -a
CONTAINER ID        IMAGE          COMMAND         CREATED             STATUS              PORTS               NAMES
bd93eb2b1ccf        e071bce628ba        "/bin/bash"         36 minutes ago      Up 36 minutes prickly_tesla

[root@docker-vm ~]# docker commit bd93eb2b1ccf centos6.9-httpd-bijit


=================================
[root@docker-vm ~]# docker commit bd93eb2b1ccf centos6.9-httpd-bijit
dc988142a75d3f7d2308543223e987be89e31da7d6f579109f0f2bb4ee0c192a

[root@docker-vm ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos6.9-httpd-bijit   latest              dc988142a75d        48 seconds ago      320.7 MB
centos                  6.9                 e071bce628ba        5 weeks ago         194.7 MB
=================================

12. Run a container using the newly built image;

[root@docker-vm ~]# docker run -it centos6.9-httpd-bijit /bin/bash

[root@29c8a4bd34d1 /]# service httpd start

So, we have successfully ran a container using our custom Docker image. 

13. Let's stop our container from previous step (ctrl+d from within container would do the job, [ docker stop also does that)

14. Now, we will Publish the container port (80 in our case) and map that to the Host OS, also provide a custom name for the container that we are going to start;


[root@docker-vm ~]# docker run -it -p 172.16.20.4:80:80 --name tux-container centos6.9-httpd-bijit /bin/bash

[root@a3923d777eaf /]#

[root@a3923d777eaf /]# service httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.4 for ServerName


15. Now, try accessing Apache page using the Host IP address http://172.16.20.4/

[Remember our container IP is 172.17.0.4 while Host IP is 172.16.20.4 We are running HTTPD within the container and NOT on the Host systems]


============================================


List of basic commands;


============================================



Pushing an image to Docker Hub


A. Detach from an running Docker container and note down the container
id;

# docker ps
CONTAINER ID  IMAGE         COMMAND       CREATED       STATUS              PORTS               NAMES

3a8a3576695d  e071bce628ba  "/bin/bash"  11 minutes ago  Up 11 minutes                           tender_goldstine


B. Commit the changes using the following syntax;

docker commit /[:] to commit changes.

eg. in our case;


# docker commit 3a8a3576695d bijit/tuxb:centos6.9-httpd-bijit


4ff826f5c36ec9b6c452443ea1d759675456f53470fc30ea201b2be363e97627



C.    Now push this repository to the registry designated by its name or tag;

Syntax:  docker push /:


eg.  

====================
# docker push bijit/tuxb:centos6.9-httpd-bijit
The push refers to a repository [bijit/tuxb] (len: 1)
4ff826f5c36e: Image push failed

Please login prior to push:

Username: bijit
Password:
Email: xxxxxx@gmail.com
WARNING: login credentials saved in /root/.docker/config.json
Login Succeeded
The push refers to a repository [bijit/tuxb] (len: 1)
4ff826f5c36e: Image already exists
e071bce628ba: Image successfully pushed
f1070d829305: Image already exists
055b9989266a: Image successfully pushed

Digest: sha256:cce65ad4fc75b136f84b567ae3f281de2723115e13d21efa63660b36fbe53e42

====================



How to Pull the private Docker image?



1. Login to Docker Hub from command line.


docker login --username=yourhubusername --email=youremail@company.com


====================

[root@ins-2 ~]# docker login --username=bijit --email=xxxxx@gmail.com
Password:
WARNING: login credentials saved in /root/.docker/config.json
Login Succeeded
====================

2. Pull the image which was pushed to that private repository.

#docker pull bijit/tuxb:centos6.9-httpd-bijit


====================

[root@ins-2 ~]# docker pull bijit/tuxb:centos6.9-httpd-bijit
centos6.9-httpd-bijit: Pulling from bijit/tuxb
055b9989266a: Pull complete
f1070d829305: Pull complete
e071bce628ba: Pull complete
4ff826f5c36e: Already exists
Digest: sha256:cce65ad4fc75b136f84b567ae3f281de2723115e13d21efa63660b36fbe53e42
Status: Downloaded newer image for bijit/tuxb:centos6.9-httpd-bijit
====================

3. List the images available locally now;

# docker images

REPOSITORY          TAG                     IMAGE ID            CREATED             VIRTUAL SIZE
bijit/tuxb          centos6.9-httpd-bijit   4ff826f5c36e        10 hours ago        326.5 MB


4. Run a container using the image dowloaded from Private repository;


# docker run -it 4ff826f5c36e /bin/bash

[root@29c3296b8e48 /]#

["4ff826f5c36e" is the IMAGE ID ]




5. To stop the container either [ctrl+d] from within container or docker stop from Host

No comments: