Skip to main content

MANAGING DOCKER BASICS

Docker is basically a container engine which uses the Linux Kernel features like namespaces and control groups to create containers on top of an operating system and automates application deployment on the container.
In a normal virtual environment, one or more virtual machines run on top of a physical machine using a hypervisor like Xen, Virtual box etc. On the other hand, Containers run in user space on top of operating systems kernel. It can be considered as OS level virtualization. Each container will have its isolated user space and you can run multiple containers on a host, each having its own user space.


Managing docker images

Before pulling an image we will search for it.
#docker search centos
This will search for several centos images available and we can see it.To pull a docker image use docker pull command
#docker pull centos
This will download latest centos image by default. If we want to download previous version tag it as follows
#docker pull centos:centos6
Now to see images give
#docker images
To find the currently running docker containers
#docker ps
To see procceses that run in background
#docker ps -a






Now let us login to a centos container
#docker run -it centos:centos6 /bin/bash

we can see that we are logged in to a centos container using /bin/bash

During this time duplicate a session and check currently running containers
#docker ps
we can see our centos container running.





How to view ip address of a running container?
First we will pull an image
#docker pull nginx
Then start
#docker run -d nginx
#docker ps
We can see nginx container is running.We can also see a name is assigned to it automatically.To view ip address
#docker inspect inspiring_nightingale | grep IP

  
 Try to acces that ip
#elinks http://172.17.0.2/
 we can see nginx home page

Here inspiring_nightingale is the name that is automatically assigned.We can also run a container in name we want.Suppose we want to run a container in name web
#docker run -d --name=web nginx
#docker ps
We can see a new container is running under name web.Now to see ip address
#docker inspect web | grep IP


Image and container management
To delete an image that is not used as a container.Here httpd is image which is not used by any container
#docker rmi  centos
we can see the image is deleted.Now try to delete centos:centos6 image(we can also forcefully delete it by docker rmi -f centos:centos6)
#docker rmi centos:centos6
We can see that we are unable to delete because a container is running out of that image.So we have to stop delete that container first.
#docker ps -a
we can see centos:centos6 is there.Just try deleting that using its container id
#docker rm ee03bf5cf987
We can see still we are unable to delete.Its because that container is running.So we have to stop it first
#docker ps
#docker stop ee03bf5cf987
Now delete it
#docker rm ee03bf5cf987
Now try deleting the image.We can see that we are able to delete it.
#docker rmi centos:centos6

Note:If we want to stop all stopped containers in a single command.use
#docker rm `docker ps -q -a`


Redirecting ports and volumes
Inorder to make our docker image accessable we want to forward any port of our local sysytem to docker.
#docker run -d --name=web2 -P nginx
Here it will select a random port which points to 80 port of our nginx container.

 Here the traffic that comes to 32768 port of our local server will redirect to our nginx container.
Try to access that port using elinks
#elinks http://localhost:32768
We can see our nginx default page.
To bind a particular port we can use following command
#docker run -d -p 8080:80 --name=web3 nginx
Now access 8080 port of localhost using elinks and we will be able to get nginx home page
#elinks http://localhost:8080


Running docker container(nginx) with a mounted volume
Consider we want to place web contents in a perticular folder and want to mount that as web directory for our nginx container.First create a folder /www
#mkdir /www
#cd /www
create an index.html file  type the web contents
#vi index.html
 test ok
Now we will mount it and run it
#docker run -d -p 8081:80 -v /www:/usr/share/nginx/html nginx


#elinks http://localhost:8081
we can see our test page comming up.