Skip to main content

How to Play with docker

For folks who want to get started with Docker, there is the initial hurdle of installing Docker. Even though Docker has made it extremely simple to install Docker on different OS like Linux, Windows and Mac, the installation step prevents folks from getting started with Docker. With Play with Docker, that problem also goes away. Play with Docker provides a web based interface to create multiple Docker hosts and be able to run Containers. This project is started by Docker captain Marcos Nils and is an open source project. Users can run regular containers or build Swarm cluster between the Docker hosts and create container services on the Swarm cluster. The application can also be installed in the local machine. This project got me interested in trying to understand the internals of the Docker host used within the application. I understood that Docker hosts are implemented as Docker in Docker(Dind) containers. In this blog, I have tried to cover some details on Dind and Play with Docker.

Docker in Docker(Dind)

Docker in Docker(Dind) allows Docker engine to run as a Container inside Docker. This link is the official repository for Dind. When there is a new Docker version released, corresponding Dind version also gets released. This link from Jerome is an excellent reference on Docker in Docker that explains issues with Dind, cases where Dind can be used and cases where Dind should not be used.
Following are the two primary scenarios where Dind can be needed:
  1. Folks developing and testing Docker need Docker as a Container for faster turnaround time.
  2. Ability to create multiple Docker hosts with less overhead. “Play with Docker” falls in this scenario.
Following picture illustrates how Containers running in Dind are related to Containers running in host machine.
dind1
Dind, C1 and C2 are containers running in the host machine. Dind is a Docker container hosting Docker machine. C3 and C4 are containers running inside Dind Container.
Following example illustrates Dind:
I have Docker 1.13RC version as shown below:
$ docker version
Client:
 Version:      1.13.0-rc2
 API version:  1.25
 Go version:   go1.7.3
 Git commit:   1f9b3ef
 Built:        Wed Nov 23 06:24:33 2016
 OS/Arch:      linux/amd64

Server:
 Version:             1.13.0-rc2
 API version:         1.25
 Minimum API version: 1.12
 Go version:          go1.7.3
 Git commit:          1f9b3ef
 Built:               Wed Nov 23 06:24:33 2016
 OS/Arch:             linux/amd64
 Experimental:        false
Lets start Dind Container. Its needed to run this in privileged mode since its mounts system files from host system.
docker run --privileged --name dind1 -d docker:1.8-dind
We can look at Docker version inside Dind:
# docker version
Client:
 Version:      1.8.3
 API version:  1.20
 Go version:   go1.4.2
 Git commit:   f4bf5c7
 Built:        Mon Oct 12 18:01:15 UTC 2015
 OS/Arch:      linux/amd64

Server:
 Version:      1.8.3
 API version:  1.20
 Go version:   go1.4.2
 Git commit:   f4bf5c7
 Built:        Mon Oct 12 18:01:15 UTC 2015
 OS/Arch:      linux/amd64
Even though host machine is running Docker 1.13RC version, we can test Docker 1.8.3 inside the Container using above example.
For Continuous integration(CI) use cases, it is needed to build Containers from CI system. In case of Jenkins, it is needed to build Docker containers from Jenkins master or Jenkins slave. Jenkins master or slave run as Container themselves. For this scenario, it is not needed to have Docker engine running within Jenkins Container. It is needed to have Docker client in Jenkins container and use Docker engine from host machine. This can be achieved by mounting “/var/run/docker.sock” from host machine.
Following diagram illustrates this use-case:
dind2
Jenkins runs as a Container. C1 and C2 are containers started from host machine. C3 and C4 are Docker containers started from Docker client inside Jenkins. Since Docker engine on host is shared by Jenkins, C3 and C4 are created on same host and share same hierarchy as C1 and C2.
Following is an example of Jenkins Container that mounts /var/run/docker.sock from host machine.
docker run --rm --user root --name myjenkins -v /var/run/docker.sock:/var/run/docker.sock -p 8080:8080 -p 50000:50000 jenkins
Following command shows the Docker version inside Jenkins container:
# docker version
Client:
 Version:      1.13.0-rc4
 API version:  1.25
 Go version:   go1.7.3
 Git commit:   88862e7
 Built:        Sat Dec 17 01:34:17 2016
 OS/Arch:      linux/amd64

Server:
 Version:      1.13.0-rc2
 API version:  1.25 (minimum version 1.12)
 Go version:   go1.7.3
 Git commit:   1f9b3ef
 Built:        Wed Nov 23 06:24:33 2016
 OS/Arch:      linux/amd64
 Experimental: false
1.13RC4 is Docker client version installed inside Jenkins and 1.13RC2 is Docker server version installed in the Docker host.

“Play with Docker”

The application is hosted in public cloud and can be accessed as SaaS service using the following link. The application can also be run in the local machine. Following are some capabilities that I have tried:
  • Run traditional non-service based containers.
  • Create Swarm mode cluster and run services in the Swarm cluster.
  • Exposed ports in the services can either be accessed from localhost or can be accessed externally by tunneling with ngrok.
  • Create bridge and overlay networks.
Following is a screenshot of the application hosted in public cloud where I have created a 5 node Swarm cluster with 2 masters and 3 slaves.
dind3.PNG
To create a 5 node cluster, the typical approach would be to use 5 different hosts or VMs which is a huge burden on resources. Using
Play with Docker”, we create 5 node cluster with 5 Dind containers. For non-production testing scenarios, this saves lot of resources.
Following are some limitations in the SaaS version:
  • There is a limit of 5 nodes.
  • Sessions are active only for 4 hours.
  • The usual Dind limitations applies here.
Lets start a simple web server with 2 replicas:
docker service create --replicas 2 --name web -p 8080:80 nginx
Following output shows the service running:
$ docker service ps web
ID            NAME   IMAGE         NODE   DESIRED STATE  CURRENT STATE           ERROR  PORTS
dmflqoe67pr1  web.1  nginx:latest  node3  Running        Running 56 seconds ago
md47jcisfbeb  web.2  nginx:latest  node4  Running        Running 57 seconds ago
The service can either be accessed from Dind host using curl or by tunneling the application using ngrok and accessing using internet.
Following is an example of exposing the service to outside world using ngrok:
docker run --net host -ti jpetazzo/ngrok http 10.0.15.3:8080
This will return an URL which can be accessed from internet to access the nginx service that we started earlier.
“Play with Docker” can also be installed in local machine. The advantage here is that we can tweak the application according to our need. For example, we can install custom Docker version, increase the number of Docker hosts, keep the sessions always up etc.
Following are some internals on the application:
  • Base machine needs to have Docker 1.13RC2 running.
  • The application is written in GO and is run as a Container.
  • Dind Containers are not the official Docker Dind container. “franela/Dind” is used.
  • GO container that runs the main GO application does a volume mount of “/var/run/docker.sock”. This allows Dind Containers to run in the base machine.
Following picture shows the container hierarchy for this application.
dind4
“Golang” container is in the same hierarchy as Dind Containers. Dind containers simulate Docker hosts here. C1-C4 are user created containers created on the 2 Docker hosts.
To install “Play with Docker” in my localhost, I followed the steps below:
Installed docker 1.13.0-rc2
git clone https://github.com/franela/play-with-docker.git
installed go1.7
docker swarm init
docker pull franela/dind
cd play-with-docker
go get -v -d -t ./...
export GOPATH=~/play-with-docker
docker-compose up
My localhost is Ubuntu 14.04 VM running inside Windows machine.
Following is the 2 node Swarm cluster I created:
$ docker node ls
ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
p6koe4bo8rn7hz3s4y7eddqwz *  node1     Ready   Active        Leader
yuk6u9r3o6o0nblqsiqjutoa0    node2     Ready   Active
Following are some problems I faced with local installation:
  • When I started docker-compose, the application crashed once in a while. I was able to work around this problem by restarting docker-compose.
  • For swarm mode services, I was not able to access exposed service using host port number. For regular containers, I was able to access exposed host port.
I did not face the above 2 problems when I accessed the application as SaaS.
Thanks to Marcos Nils for helping me with few issues faced during my local installation.

References

Popular posts from this blog

How to read or extract text data from passport using python utility.

Hi ,  Lets get start with some utility which can be really helpful in extracting the text data from passport documents which can be images, pdf.  So instead of jumping to code directly lets understand the MRZ, & how it works basically. MRZ Parser :                 A machine-readable passport (MRP) is a machine-readable travel document (MRTD) with the data on the identity page encoded in optical character recognition format Most travel passports worldwide are MRPs.  It can have 2 lines or 3 lines of machine-readable data. This method allows to process MRZ written in accordance with ICAO Document 9303 (endorsed by the International Organization for Standardization and the International Electrotechnical Commission as ISO/IEC 7501-1)). Some applications will need to be able to scan such data of someway, so one of the easiest methods is to recognize it from an image file. I 'll show you how to retrieve the MRZ information from a picture of a passport using the PassportE

How to generate class diagrams pictures in a Django/Open-edX project from console

A class diagram in the Unified Modeling Language ( UML ) is a type of static structure diagram that describes the structure of a system by showing the system’s classes, their attributes, operations (or methods), and the relationships among objects. https://github.com/django-extensions/django-extensions Step 1:   Install django extensions Command:  pip install django-extensions Step 2:  Add to installed apps INSTALLED_APPS = ( ... 'django_extensions' , ... ) Step 3:  Install diagrams generators You have to choose between two diagram generators: Graphviz or Dotplus before using the command or you will get: python manage.py graph_models -a -o myapp_models.png Note:  I prefer to use   pydotplus   as it easier to install than Graphviz and its dependencies so we use   pip install pydotplus . Command:  pip install pydotplus Step 4:  Generate diagrams Now we have everything installed and ready to generate diagrams using the comm

Python questions and answers part 3

Q1).What is Python? Ans1:   Python  is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it h as fewer syntactical constructions than other languages. Q2).Name some of the features of Python. Ans2:  Following are some of the salient features of  python It supports functional and structured programming methods as well as OOP. It can be used as a scripting language or can be compiled to byte-code for building large applications. It provides very high-level dynamic data types and supports dynamic type checking. It supports automatic garbage collection. It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java. Q3).Do you have any personal projects? Really? Ans3: This shows that you are willing to do more than the bare minimum in terms of keeping your skillset up to date. If you work on personal projects and