Friday, November 21, 2014

Docker - Part 1


Never heard of docker ?

I found this docker explanation is the quickest to get up-to speed with docker. Also docker official documentation is more complete and up to date.

https://docs.docker.com



So what is docker ?

In short its a better "VM orchestration tool".

But a docker VM is not a VM you used to know. Usually the VM you know is sort of itchy, bloated and makes you vomit. Docker VM (to be more precise, we call it docker container) is like a Pineapple juice in a desert!

Lets cut to the chase.

Docker has two parts. Docker client and Docker server (Docker Host). Docker host can have multiple docker containers. Docker server expose REST Api which the docker client use to manipulate docker containers in the docker server.


Docker Host should run in a Linux box. In MAC, we can use boot2docker VM to run docker host.


Starting up in MAC

boot2docker up   ← How to start docker in MAC.


 asanga@MacBoo-Pro:~$ boot2docker up  
 Waiting for VM and Docker daemon to start...  
 ..........ooo  
 Started.  
 Writing /Users/asanga/.boot2docker/certs/boot2docker-vm/ca.pem  
 Writing /Users/asanga/.boot2docker/certs/boot2docker-vm/cert.pem  
 Writing /Users/asanga/.boot2docker/certs/boot2docker-vm/key.pem  
 To connect the Docker client to the Docker daemon, please set:  
   export DOCKER_TLS_VERIFY=1  
   export DOCKER_HOST=tcp://192.168.59.103:2376  
   export DOCKER_CERT_PATH=/Users/asanga/.boot2docker/certs/boot2docker-vm  


Congratulations. Your docker host is up !! 

But, Now what ? Host is up. But if you want to control the host to do anything useful, you need a remote control to your docker host, which is a docker client.


Remember : docker host is just a server with a REST API. You need a docker client which talks to that API to interact with docker.

Ok lets connect a docker client to the host we just spun up. docker client needs to know about your docker host in-order to connect (Usually in production environments, docker client and docker hosts are in different boxes)

So how does docker client know about docker host ? Your guess is right. Via Environmental Variables.

So here we set those.


export DOCKER_TLS_VERIFY=1  
   export DOCKER_HOST=tcp://192.168.59.103:2376  
   export DOCKER_CERT_PATH=/Users/asanga/.boot2docker/certs/boot2docker-vm 

If you ask me how I knew what those variables and their values, and if I tell you it comes with experience and practise, then you believe me..mmm thats alright ;)

But if you look at the out put of the first command boot2docker up at the end, last 4 lines, Docker actually gives you those values ;)

Extra
boot2docker ip  ← shows docker host ip
boot2docker ssh ← How to connect to docker server in MAC
Or you can use traditional ssh command.  
ssh docker@192.168.59.103
pass: tcuser


See stuff


docker ps ← Show all docker containers
docker images ← Show all docker images
docker logs CONTAINER_NAME  ←Show logs from running container
docker logs 5d3852fa1aef


Start continers


docker run -d IMAGE_NAME command ← Starts container in Deamon mode
docker run -P IMAGE_NAME command ← Starts with all ports exposed to docker host
docker run -d -P --name web nginx    ← Starts nginx deamon with all ports published
docker run -t -i IMAGE_NAME /bin/bash  ← Jump in tp docker container


Port maps are in this format.  HOST:PORT -> CONTAINER:PORT
docker run -p 8080:8080 IMAGE COMMAND


Remove / Stop stuff
docker stop CONTAINER_NAME ← Stops container
docker rmi 195eb90b5349  ← Removes docker image by image id
docker rm 195eb90b5349  ← Removes docker container by container id
docker rm $(docker ps -a -q) ← Remove all docker containers

No comments: