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:

$ docker -v

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:

Docker version 19.03.1, build 74b1e89e8a

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:

$ docker container run alpine echo “Hello World!”

When you run the preceding command the first time, you should see an output in your Terminal window similar to this:

Unable to find image ‘alpine:latest’ locally latest: Pulling from library/alpine 9d48c3bd43c5: Pull complete Digest: sha256:72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb Status: Downloaded newer image for alpine:latest Hello World!

Now that was easy! Let’s try to run the very same command again:

$ docker container run alpine echo “Hello World”

The second, third, or nth time you run the preceding command, you should see only this output in your Terminal:

Hello World!

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:

Docker container run command
Docker container run command

Leave a Reply

Your email address will not be published. Required fields are marked *