Monday, June 29, 2015

Part 1: Starting with Docker

If you haven’t heard about Docker by now, you are probably hanging out with the wrong crowd. As we speak, Docker is next thing after Cloud computing and if you don’t understand yet how significant they are, last April they joined the Unicorn Club.

And what this post is all about? It is a first in a series of posts about Docker usage. I’ll start with how to start using Docker (YA newbie tutorial) in a common infrastructure. Next posts will concentrate on more advanced Docker usage.


Basics. What is Docker?

Docker is an open platform for building, shipping and running distributed applications. It gives programmers, development teams and operations engineers the common toolbox they need to take advantage of the distributed and networked nature of modern applications
Docker.io
Basically Docker allows to use application packaged in containers on machine to simplify deployment. More simple explanation is that you can run virtual OS on any Linux and soon Microsoft powered machine. This not exact explanation, since container is not a real VM, you just receive ready to use virtual environment. What is a Container? Container is a Linux Kernel level virtual environment. Sort of evolution of chroot jail environments.
If you want to compare LXC/Docker to any other VM that you can run on your computer that the biggest difference will be the lightweight of the containers. Each VM need it’s own disk space and run full OS on it while the containers use shared OS and layered FS(AuFS). So it’s lighter, faster and easier.

Why Docker?

The news that docker worth more than 1B$ is not the reason to use, but the Moby Dock whale logo may just be :). Seriously, Docker is open source platform and if you’re at this blog you know the benefits of it. They are not the first to make the service, but they are working on it for two years and at this point Docker leadership become a de-facto standard. Docker can run on any Linux OS and it’s has native support of big cloud providers - AWS, Google Engine, OpenStack, ProfitBricks and more are joining every day. Deploying Docker is really easy, I’ve started using Docker while I was going on train with bad WiFi and after 1 hour I had small application cloud with web server and DB.
Actually that’s the main reason we’ve chosen to use Docker to deploy our own infrastructure manager - Heili :) But there are few other products that might have similar functionality but they are not as advanced, neither solid as Docker.

Let’s start

Let`s leave theory here, if you want to know more check Docker site or Google.
Let’s start and build containers! As I already said Docker can run on a lot of different OS’s and it doesn’t really matter what OS you run once you installed it, that’s why I decided not to explain how to install it and just send you to official guide for installation:


Now, once you have Docker installed let’s make a first container, but first we need to have OS. Docker has ready images of common OS and we will use Ubuntu for this tutorial

~ $ docker pull ubuntu:14.04
14.04: Pulling from ubuntu
428b411c28f0: Pull complete
435050075b3f: Pull complete 
9fd3c8c9af32: Pull complete 
6d4946999d4f: Pull complete 
ubuntu:14.04: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Digest: sha256:59662c823007a7a6fbc411910b472cf4ed862e8f74603267ddfe20d5af4f9d79
Status: Downloaded newer image for ubuntu:14.04
Let’s see what this command does:
  • pull - Pull image from repository(by default Docker Hub)
  • ubuntu - Image name
  • :14.04 - Image tag, in this case it’s OS version
After sometime (depends on your internet connection speed) you will have Ubuntu 14.04 image ready for use. And now you can start the container :)

~ $ docker run -t -i ubuntu:14.04
root@eb029641a22c:/#
  • run - Run a command in new container
  • -t - Allocate pseudo terminal, so we will see the output
  • -i - Interactive, you will be able to use STDIN
  • ubuntu:14.04 - Image name
That’s all, you have running container with Ubuntu! Of course that’s not the way we use container, because we don’t want them to be another kind of VM, we want the container to run command/application, that’s why we have run command in Docker. So how do we do it?
The command docker run also expect to receive a command to run once the container is running:

~ $ docker run -t -i ubuntu:14.04 echo “Hello World”
Hello World
~ $
Cool! our container printed “Hello World” and we are back to our original OS, But why?
Docker container is designed to run one command and once this command is done the container is closed. So how can we run something and keep container running? Just execute command that will never exit, until something happens, in our case simple loop (press Ctrl+C to stop it):

~ $ docker run -t -i ubuntu:14.04  sh -c "while :; do echo \"Hello World\"; sleep 10; done"
Hello World
Hello World
^C~ $
So we have simple bash loop that will run until the world collapse but we don’t want to see the output all the time, because if we do, why do we need to use container? This mode is really helpful for debug for image(more explanation in the next posts) and our production container will be demonized:

~ $ docker run -d ubuntu:14.04  sh -c "while :; do echo \"Hello World\"; sleep 10; done"
dde9009d78b7b6f58a57eb7737bea01913552164c2f4125bb1e9fe6e8336b0db
~ $
Voila! Something happened and we have now long strange string, but where is the container? Is it running? Docker has command that allow us to see all containers, let’s check if we can see something with it’s help:

~ $ docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
46671d396781        ubuntu:14.04        "\"sh -c 'while :; d   5 minutes ago      Exited (130) 5 minutes ago                       mad_thompson        
4460b0b7a5f7        ubuntu:14.04        "echo “Hello World     8 minutes ago      Exited (0) 8 minutes ago                        gloomy_pasteur      
50292108ff3b        ubuntu:14.04        "/bin/bash"            12 minutes ago      Exited (0) 12 minutes ago                        silly_lumiere       
dde9009d78b7        ubuntu:14.04        "\"sh -c 'while :; d   3 minutes ago       Up 3 minutes                            cranky_turing
Let’s start with the command:
  • ps - List containers
  • -a - Show all containers, without this flag we will see only running containers
And what we have at the output?
  • Container ID - Each container receive unique ID once it started, for easy management. The string we received earlier is the full ID and here we can see the shorter ID
  • Image - Name of the image that has been used by this container
  • Command - Command that was executed on this container
  • Created - How long it’s here?
  • Status - What is the current status and how long is this status
  • Ports - What ports are open(explanation in the next post)
  • Names - Each container can have a unique name, so we will not have to use the ID all the time. If name is not provided, Docker will generate random name for us(this is the 1st Docker issue submitted on Git)
We have here “mad” Ken Thompson(Unix and C inventor), “gloomy” Louis Pasteur(vaccination discoverer), “silly” Lumiere brothers(first film makers) and “cranky” Alan Truing(founder father of computer since, also spotted as Sherlock) - sounds like the Avengers(probably you have another team members). You can keep using this cool name generator or next time when you run the container give --name flag with desired unique name.
We see that our latest container is up and running but we also can see 3 container that are exited. This are the container that  we used on previous steps, you can see it by the command. This containers are exist, so this mean that this names are reserved, so if you use random names you’re alright, but if you have your own unique name, you can use it again. To be able to use it again we need to remove the stopped containers:

~ $ docker rm mad_thompson gloomy_pasteur silly_lumiere
or

~ $ docker rm $(docker ps -q -f status=exited)
46671d396781
4460b0b7a5f7
50292108ff3b
~ $ docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
dde9009d78b7        ubuntu:14.04        "\"sh -c 'while :; d   5 minutes ago      Up 5 minutes                           cranky_turing
Turing is alone now, it’s it should print “Hello World” in loop for 5 minutes, every 10 seconds = 30 messages. Let’s check the output of the container (make sure to change to your container name):

~ $ docker logs cranky_turing
Hello World
Hello World
Hello World
Hello World
...
Hello World
~ $
As you can see our container is running as expected. To stop just execute:

~ $ docker stop cranky_turing
And delete the container

~ $ docker rm cranky_turing


This is the basics of using the Docker containers, you can play more with it while the next post is written.

The next part will explain more advanced containers usage, containers network and how to build small web application on Docker container!

Provided by:Forthscale systems, cloud experts

No comments:

solving error: Your current user or role does not have access to Kubernetes objects on this EKS cluster.

Trying to access EKS cluster with kubectl you might get an error similar to: Your current user or role does not have access to Kubernetes ob...