Docker: Execute the first command
When you are coming and reading me this post, I am assuming that you already have installed the docker in your system and ready to execute your first docker command together. In case you haven't, no worries :) you may go through my previous two posts very quickly through the following links:
- Article #1: Docker Overview
- Article #2: Docker Installation
In case you want to ensure that the docker is installed on your computer or not. you may use the following command through the command line.
docker version
If it gives the output as I have given below, you rest assured that the docker is not only installed but indeed running in your system right now.
Ok great! Let's start running our first command and understand every bit of that in more detail. the command you and I going to run together is here:
docker run hello-world
yes, of course. Hello-World. How can we go a bit more without running this command :) now the moment you press enter after this you will see the series of steps executed in your command window. Probably the very first line after might again depress you because it says,
Unable to find image 'hello-world:latest' locally
Well, there is nothing to be sad here because this is what docker comes with after all. It tries to find the image in your local system and it also notifies you that it didn't find any. so, finally, it will go to the public image repository and tries to find an image with the name hello-world. If you have written a correct spelling of hello-world, it will find the image and will start the processing of downloading that from the repository to your local computer. the output of the running process may look similar to mine as below:
Once the full image is downloaded, it will also execute the command and print something.
Hello from Docker!
So far so good. We haven't understood anything about docker but we ran one full command and it executed as well. The important deal of this exercise is it ran and didn't give any warning and error message. Great achievement right?
Ok enough of fun now. Let us understand what is going behind the scene. When we run a command, basically docker client installed in our system is going to ask the docker server (installed in our computer) that please provide the container with hello-world image and docker server is going to find the same in image cache (specific part in our computer maintain by docker). Well, the first time it won't find any such image and hence, it is going to download that from the docker hub. instead of just going to give that to docker client, it will store that image in image cache and docker server will generate a running container from that image (remember the container is nothing but an instance of an image. furthermore, the image here is nothing but docker image) so, the finally the diagram will be look like below.
It is also important to understand What container means or what it includes when we say the container is nothing but the instance of an image.
Let me give you an analogy from our packaging workflow. When we are ready to ship our solution (maybe a web application or a desktop application) we package that and that contains a set of files, command arguments, static content, Dlls, exes and other things right? similarly, docker Image is also having two main things. (1) File system (2) Command to run
So, when Docker creates a container using an image, it gives the set of RAM, Network, CPU, Hardware and Space in File System from our computer to that Image (rather the instance of an image to be more specific) and it executes the startup command set in that image by a provider.
Now, before we wind up this article let us run this command one more time and ensure that our understanding with docker is correct. If the image is already downloaded on our computer then it should not download this image one more time, right?
Do this exercise yourself and indeed it won't see that message saying "Unable to find image 'hello-world: latest' locally" rather it simply prints the final message "Hello from Docker!"
Great! Finally, we have run our first valuable docker command and finished our "Hello World" exercise. I hope you find this learning valuable and I will see you in my next article with something more interesting.