This article will demonstrate how to run your first container in docker.
Before we start, we want to make sure that Docker is installed correctly on your system and ready to accept your commands. Open a new Terminal window and type in the following command:
If everything works correctly, you should see the version of Docker installed on your machine output in the Terminal. At the time of writing, it looks like this:
If this doesn’t work, then something with your installation is not right. Please make sure that you have followed the right instructions.
So, you’re ready to see some action. Please type the following command into your Terminal window and hit return:
When you run the preceding command the first time, you should see an output in your Terminal window similar to this:
Now that was easy! Let’s try to run the very same command again:
The second, third, or nth time you run the preceding command, you should see only this output in your Terminal:
You would be surprised why the first time you run a command you see different outputs than all the subsequent times. This is because we are using alpine image which was not present initially so docker has downloaded from docker hub and stored in the local cache. From next time onwards it will take it from local cache.
Now let’s dig above command line. First and foremost, we have the word docker. This is the name of the Docker command-line interface (CLI), which we are using to interact with the Docker engine that is responsible to run containers. Next, we have the word container, which indicates the context we are working with. As we want to run a container, our context is the word container. Next is the actual command we want to execute in the given context, which is run.
So far, we have docker container run, which means, Hey Docker, we want to run a container…
Now we also need to tell Docker which container to run. In this case, this is the so-called alpine container. Finally, we need to define what kind of process or task shall be executed inside the container when it is running. In our case, this is the last part of the command, echo “Hello World”.
Maybe the following figure can help you to get a better approach to the whole thing: