Introduction to Docker

A fast introduction to what is growing in the public interest. Docker is climbing the attention of the developers concerning the virtualization. In this article I’ll show you an easy example of how it works in windows OS.

As I said above, Docker is becoming very popular in the articles, tweets and conferences that it seems very strange haven’t heard about looking for some virtualization system.

From Google trend:trend

So, I guess it’s worth to get some information about Docker…

Once you’re convinced of that, let’s look at what is Docker. You can find a lot of definition of Docker in Internet. My favorite is the once which declares Docker as “Lightweight virtualizer”.

Docker lets make you different environments on your host machine and manage that in very similar way like a traditional Virtual Machine.

The difference is that you don’t have to install a complete OS guest and then deploy your solution on it. Docker is very smart in this approach, and probably this is the greatest feature of the solution.

Docker has the concept of Images and Containers.

Images are the base of the Docker’s containers. Images contain the Software(s) that are installable in docker environment and are used to create the Containers. A list of available Docker images is at Docker Hub Central.

Containers are instance of Docker’s images downloaded at the Guest OS system. Containers will be running at the Docker runtime system.

I try to illustrate the idea in this picture.

docker

In the Host OS, four Containers are installed. It contains two instance of Apache Tomcat, one is Apache Web Server and the last is a MySql Database.

Let’s start installing Docker on Windows.

Check out this link (https://docs.docker.com/docker-for-windows/) for the last valid Docker installation package and install it on you OS.

Once installed the .msi file you can enter at Docker system using PowerShell. Check your installation through these commands:

PS C:\> docker --version
Docker version 1.12.3, build 6b644ec

Now that you’ve checked the correct running of Docker, it’s time to download (pull) an image, configure it and run!

The idea is to install an instance of Apache WebServer. So, pull the image from repository central through this command.

PS C:\> docker pull httpd:2.2
2.2: Pulling from library/httpd
75a822cd7888: Pull complete
22ab16045130: Pull complete
d8e1407a0210: Pull complete
315798392cd3: Pull complete
c843c77de927: Pull complete
Digest: sha256:f3f1dfb2b45b6a4fb90f4644a505262f631a18f83c3acf7d426621b592a1411f
Status: Downloaded newer image for httpd:2.2

You can check the operation result listing the local image repository.

PS C:\> docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
httpd               2.2                 c8a7fb36e3ab        6 days ago          169.9 MB

Now you can run the Apache WebServer by using this command.

PS C:\> docker run -p 81:80 -d -v C:/Progra~1/Docker/Apache:/usr/local/apache2/htdocs --name apache httpd:2.2
c85e3b1f948d83246666df7f8c08942ad5c1a6d2552e1678ccc8a1e8223a8d15

Briefly:
Docker is the command to access at the Docker environment.
run is the “verb” or the operation to apply.
-p is the parameter for port mapping; we’re mapping the Docker Apache port 80 to the port 81 of the Guest OS.
-v is the parameter for mounting the Guest OS location to the Docker container location.
–name is the parameter for assigning the container’s name (apache) starting from the image name (<image name>:<version>).

That’s all! You can check the container deployed through this command.

PS C:\> docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                   PORTS                NAMES
c85e3b1f948d        httpd:2.2           "httpd-foreground"       2 days ago          Up 2 seconds             0.0.0.0:81->80/tcp   apache

The container named apache has been running for 2 seconds! Browsing the location http://localhost:81/ you’ll see “Index of /”. Let’s try to copy a file under the location C:/Progra~1/Docker/Apache and you’ll see the file listing.

You can start and stop the container through the command:

PS C:\> docker [start/stop] apache

You’d like to check the Apache activities looking at the access log

PS C:\> docker exec -it apache more logs/access_log
172.17.0.1 - - [20/Dec/2016:16:01:26 +0000] "GET / HTTP/1.1" 200 273 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7
.0; rv:11.0) like Gecko"
172.17.0.1 - - [20/Dec/2016:16:01:26 +0000] "GET / HTTP/1.1" 200 273 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7
.0; rv:11.0) like Gecko"
172.17.0.1 - - [20/Dec/2016:16:01:26 +0000] "GET / HTTP/1.1" 200 273 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7
.0; rv:11.0) like Gecko"

Or you’d like to remove the container from Docker

PS C:\> docker rm -f apache
apache

And, why not, remove the image from the local repository.

PS C:\> docker rmi a2cbbfeec0f9
Untagged: httpd:latest
Untagged: httpd@sha256:0b4197f3d56251e2861247b58a5a912ca92bb998795a8c3c93584182a6b7f373
Deleted: sha256:a2cbbfeec0f9a27b98634a81f0aea9952190d7ab9d833bde7dc86c5ca63258c2
Deleted: sha256:eb2dfa67d9e59d64eed63032ea1e50152fc6067b09b48932b420f2627deef0e0
Deleted: sha256:f3f8be21c9738f3fddabbd92d2eef33bde4d4e116a9d0ce33f78617b7bef7ae4
Deleted: sha256:26cf3b6ebaf1196dc84608226aefb900fc21ea45db077ec84625e7c29822d3ff
Deleted: sha256:54bfe7bbd9524de57333daceacd9b56da2acf713089ff8004c329d96a09197ea

So far so good.
Now, we want to be able to deploy the container to different Docker location in order to replicate the environment system to other OS.

To achieve that, the dockerfile helps us to put all the commands required into an image which, once has been built, can be used in different OS.

The dockerfile is a configuration file with instruction documented at https://docs.docker.com/engine/reference/builder/

A simple dockerfile which:

  • Download Tomcat 8
  • Deploy a Webapp

is the following:

FROM tomcat:8.0.20-jre8
RUN mkdir /usr/local/tomcat/webapps/recognito
COPY recognito/ /usr/local/tomcat/webapps/recognito/
EXPOSE 8080

The Web application “recognito” is deployed under Tomcat directory and it’s exposed under the port 8080.

Build the application

PS C:\Program Files\Docker\myapp> dir
Directory: C:\Program Files\Docker\myapp
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       13/12/2016     15:07                recognito
-a----       20/12/2016     17:09            396 docker.txt
-a----       23/12/2016     14:27            138 Dockerfile
PS C:\> docker build -t myapp .

And then run it!

docker run -p 8888:8080 -d --name myapp myapp

The Web application has been deployed under the tomcat webApp folder.

PS C:\Program Files\Docker\myapp> docker exec -it myapp ls -la webapps/
total 32
drwxr-xr-x  9 root root  4096 Dec 13 14:10 .
drwxr-sr-x 14 root staff 4096 Dec 13 14:42 ..
drwxr-xr-x  3 root root  4096 Mar 31  2015 ROOT
drwxr-xr-x 14 root root  4096 Mar 31  2015 docs
drwxr-xr-x  6 root root  4096 Mar 31  2015 examples
drwxr-xr-x  5 root root  4096 Mar 31  2015 host-manager
drwxr-xr-x  5 root root  4096 Mar 31  2015 manager
drwxr-xr-x  5 root root  4096 Dec 13 14:10 recognito

Concluding, this has been only a little introduction about what Docker can do. A combination of Docker with micro service is increasingly visible in the common solution pattern. I think it’s very good for the developer environment. Unfortunately, I still haven’t had the opportunity to test Docker in a production environment.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s