Getting started with Docker

Docker is an open source software for running Linux appplications in an isolated environment called  container. A container provides an  operating system level virtualization. In simple words to say it is a lightweight virtual machine. In general a running Linux OS uses a single namespace which contains process space, network space etc which we call host. On top the host with the help of container facilities provided Linux kernel such as cgroup etc, you can create virtual namespace. Each new virtual namespace contains its own namespace,root user and network space with its own routing. Equally it does not have its own kernel instead it uses the host kernel, it cannot boot a different type of OS other than Linux based distro, and it does not need an init process.

docker

 

 

 

 

 

 

 

 

 

 

 

 

 

Installing Docker:

Docker is growing software and it is supported in Linux from kernel versino 3.10. you can verify this like this.

$ uname -r

If your kernel is older than 3.10 then it is better to either upgrade your OS
or install a new kernel.

The docker website provide a list of installation method, based on the distro you have choose the right method.

If you have sucessfully installed docker, you can verify it using the following command.

# docker version

The above command should be run as root user.
Docker Images:
Once we have the docker running well in our machine, the next stop is to download a image and play around it. A docker image is bundle of files in disk. The bundle can be full blown distro or just a single command. It is a passive content in disk.

The below command will list the images downloaded by your docker.  When you use the docker for the first time it will be empty.

# docker images
REPOSITORY TAG IMAGE ID  CREATED  SIZE

The docker images are defined in a Dockerfile. The docker command uses the file to create the images. In general, You can create one for yoursel for you can use the prepared images uploaded to Docker Hub. The Docker hub is a central repository where you can maintain the version control of your images,. It is easy to get started with such images.

dockerhub

 

 

 

 

 

 

 

 

 

 

 

 

 

 

You can search for images prepared and put on public by other people by using the following command

# docker search hello-world
 
# docker pull hello-world 
# docker run hello-world
# docker run hello-world

There can be a situation where you may need a docker image copied to a machine which has no internet connection, you can save the image and load it later like shown below

 
# docker save -o hello-world.docker hello-world 
 
# docker load <hello-world.docker

Lets run an Ubuntu image. Here we are not pulling the image instead docker takes care of it.

The command line options t and i are explained below
-t              : Allocate a pseudo-tty
-i              : Keep STDIN open even if not attached

 
# docker run -it ubuntu

Now you will get a ubuntu running a bash shell with root privilage, you can install whatever
you want to,change any settings whatsoever, once you exit the shell the changes goes to dust.
Because the changes are not written to a persistant memory.

Image vs Container:
From an object oriented approach, Images are classes and containers are objects.
Image are just plain immutable files. Containers are instance which are running or exited
from the memory.

 
# docker images
 
# docker ps -a

Image ID and Container ID:
Every image and container has an unique ID. You can use the ID to address the unique image
or container.

 
# docker images -q
 
# docker ps -aq

You cannot remove an image from the disk, if a container is depending on it,
Even if the container has already run and exited.
So you need top remove the container inorder to remove the corrensponding image.

 #docker rmi c54a2cc56cbb 

Before that you need to remove all the containers corresponding to this image

 
docker rm da66a8c26ccc

Before that you need to stop container if running. Below are some usefule script
to make docker easier.

 
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
 
docker rmi $(docker images -q)

There are many things that you can do in docker which I have not covered. This reference will provide a complete overview.

Building using Dockerfile:

 
FROM docker/whalesay:latest
RUN apt-get -y update && apt-get install -y fortunes
CMD /usr/games/fortune -a | cowsay
docker build   -t "cowsay:latest"   .  
docker run -it "cowsay:latest"

For more information about dockerfile refer to https://docs.docker.com/engine/reference/builder/

Leave a Reply

Your email address will not be published. Required fields are marked *