Tag Archives: Docker

Getting Started with Docker

Docker logo horizontal spacing

Docker Allows you to run tiny containers inside your linux machine . These containers can be compared to Virtual Machines, but there are some differences that makes them easy and portable. In a user perspective, they are damn fast to start (and kill) compared to VMs and also very portable. I am trying to describe how to install and run containers with Docker on a ubuntu 14 ( trusty) machine.

Installation

Installation is fairly easy with the follwing command. It will ask your sudo password when needed.

wget -qO- https://get.docker.com/ | sh

It adds a apt repo and does the installation of the latest docker version on your machine. Verify the installation by running ‘docker -v’ command. It will show you the version of docker installed.

Pull and Run

Now, we need to pull a basic container and run it to see the magic.

$docker pull ubuntu:14.04
14.04: Pulling from ubuntu
e9e06b06e14c: Extracting [=======================> ] 62.95 MB/65.77 MB
a82efea989f9: Download complete 
37bea4ee0c81: Download complete 
......
$docker run -ti ubuntu:14.04 /bin/bash
root@c419ad172b0f:/#

What has happened just now is that you have downloaded the latest docker ubuntu 14.04 image from the docker registry and ran it. The ‘root@c419ad172b0f#’ prompt you see is the container running the bash shell. You can now work on it as you would in any other normal ubuntu machine, when you exit the shell, it would stop the container as well.  Enjoy !

Maintenance

Everytime you exit a container, it does not actually delete it. It is left in the filesystem so that you can restart it if needed. Let’s take a look at how we can see the images we pulled, and see the containers we started and also how to work with them.

$docker images
REPOSITORY                                           TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu                                               14.04               07f8e8c5e660        2 weeks ago         188.3 MB

As you can see, it lists all images you have downloaded from internet. To remove a image , run ‘docker rmi ubuntu:14.04’

$docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c419ad172b0f ubuntu:14.04 "/bin/bash" 24 minutes ago Exited (0) 2 minutes ago hopeful_thing

This is the container that just got stopped in the previous step. Notice the CONTAINER_ID c419ad172b0f is the same as the hostname of the container we had when we ran the first container. Each time you start a container , it creates a unique ID . You can start a stopped container and attach shell to it using the commands below.

$docker start c419ad172b0f
c419ad172b0f
$docker attach c419ad172b0f
root@c419ad172b0f:/# echo Hello
Hello
root@c419ad172b0f:/# exit
exit
$

Now let’s take a look at how to remove a container.

$docker rm c419ad172b0f

Customized Images

You can also create customized images for your use. For example, I want to have a image that includes ‘screen’ package on top of the basic ubuntu image.  It is easy to prepare a ‘Dockerfile’ and let docker create (build) a new image for you. Create a file named ‘Dockerfile’ inside a empty folder. Content is given below.

FROM ubuntu:14.04
MAINTAINER ksraju007@gmail.com

ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update ; apt-get -y install screen ;
CMD "/bin/bash"

Now build the image. Notice that we are giving it a new name called u14screen using the -t option.

docker build -t u14screen --rm .
Sending build context to Docker daemon 2.048 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu:14.04 
 ---> 07f8e8c5e660
Step 1 : MAINTAINER ksraju007@gmail.com
 ---> Using cache
 ---> 52fe481915f0
Step 2 : ENV DEBIAN_FRONTEND noninteractive
 ---> Running in 7d802e182f91
 ---> ffd0cd910a16
Removing intermediate container 7d802e182f91
Step 3 : RUN apt-get update ; apt-get -y install screen ;
 ---> Running in 6574afc3a441
....
Processing triggers for ureadahead (0.100.0-16) ...
 ---> 76433af5559b
Removing intermediate container 6574afc3a441
Step 4 : CMD "/bin/bash"
 ---> Running in 8933a2b86e12
 ---> ce468a62539a
Removing intermediate container 8933a2b86e12
Successfully built ce468a62539a
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
u14screen latest ce468a62539a About a minute ago 210.9 MB

Hurray ! You have just built your own docker image. Try running it.

$docker run -ti u14screen
root@95d2c547e113:/# screen -v
Screen version 4.01.00devel (GNU) 2-May-06
root@95d2c547e113:/# exit
exit
$

Since we have mentioned the CMD parameter in Dockerfile to start /bin/bash automatically, you do not need to specify that anymore during ‘docker run’ for your image. For more information about advanced docker options and Dockerfile parameters, see https://docs.docker.com/

The best thing about having Dockerfile is that now you can send the Dockerfile to your friends and they can build exactly the same image on their computer too. This makes it extremely easy to replicate work environments across people, not to mention that you are not copying lots of data too.. it is a simple text file.

Enjoy your docker experiments !  🙂