The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.

Friday, February 10, 2017

Docker for Beginners

Docker is the hottest celebrity in the tech World. Everyone around you might be jumping over it. And you might wonder what the hype is about?

In simplest form, I would say it is much like a virtulbox or vm but very light weight. Reason? It eliminates an entire OS layer and uses most of the required components from the host OS. I would let you read more firm definition from official docker documentation

I started playing with it very recently and thought to put up some very basic getting started steps. I am using a mac pro with Sierra 10.12.3 as host OS.


  1. Head over to https://docs.docker.com/engine/getstarted/step_one/#/docker-for-mac and just hit "Get Docker for Mac".
  2. Install like any other OSX app and you are done.
  3. As a confirmation you should see a whale icon in your mac toolbar.

Create Your First Image

Before you create your first docker image, lets check what we already have. 


This list out all the docker images you already have. e.g.
Above lists out the repository where image exists (if not pushed then local), a tag which works like a version, the unique image id. Instead of re-inventing the wheel you should first check if someone already created an image you need and published on docker hub. If not exactly what you want but may be a starting point that you can extend further.  To mention docker hub is like a marketplace where you can look for images built by other devs like you. You can simply pull these images if there exists one that does your job. Likewise, once you extend or customize and existing image, then you can push your flavor for other's use. To push or pull you would need to create a docker hub account.

For this post, we are extending a Oracle Enterprise Linux 7 image with some of the handy utilities/scripts. To do that, you need to pull the base image as the first step and then start downloading and installing the required utils/scripts. This sequence of steps need to be defined in a file called Dockerfile. To store your docker file and any additional resources, lets start with a new dir.

Create a text file and name it Dockerfile using your favorite editor. I use sublime. The very first instruction would be to pull the base image (Oracle Enterprise Linux 7 in this case). The file will look like:
Save it and lets build our first image. In this case, we are literally pulling and building an image as is.


The -t option is to give a tag to the image you just built. The first line sends the data to docker demon.  2nd line prints the step from Dockerfile it executed. 3rd line is a layer identifier (every step is executed as a layer and cached for reuse when you rebuild). Final line shows the image built with its image id.

Let's check what images do we have now:

You will notice a new image (2nd last) which we just created. Next step is to run it. And we run an image by initiating a  docker container.  Let's do that


So, the last word bash here along with option i and t makes the container interactive with bash open. Lets try to run some simple commands.
If you notice ls worked just fine as it's a base linux image. However, unzip did not. The reason? Well Oracle Linux 7 docker image that you pulled doesn't include that. Options? You have 2 options. First, you go ahead install in the container. Secondly, you can extend this original image and have instructions to install unzip and build an image.

Though both options will work but 2nd option helps you avoid installing manually every time you have a new container. And this becomes super productive when you need a complete development environment for multiple developers to build and test their work. For example, you can configure with some additional utilities and a node or have a docker container for hosting a db.

Assuming you are convinced with the requirement of having your own image, lets extend our image and rebuild. The modified Dockerfile will look like below:
And on building, and starting a new container and trying the same will look like below:
So, this time you have an enhanced image with unzip installed. Before we go ahead and push it to docker hub lets see how to see all the running containers and stop any if we want.


So, that how we list and stop a container by its id. Lets now push the image to docker hub. For pushing there are 3 steps. 1 tag your image 2 login 3 push

That's it. Your new image is ready for you and World (if you like) to consume. To see how it appears on docker hub here is a screenshot:

Congratulations!!! Hope you find it useful.

No comments: