Run Our First Hello World Docker Container

Introduction

In this article, we will learn how to create and run our first container. We are going to create the container from an image. The image we are going to use here is called BusyBox. Please, read our previous article before proceeding to this article where we discussed the Important Concepts of Docker Technology.

Let’s go and check it out this link hub.docker.com. So, here we are at the Dockerhub website.

Dockerhub

Just search for BusyBox. Let’s click the first one which is the official BusyBox repository.

BusyBox docker repository

We quickly scroll down the document. As you see, BusyBox is a tiny image that is only about 1 megabyte. This is the main reason we choose BusyBox.

BusyBox docker repository

Because of its tiny size so it would take little time to download.

Let’s check out the tags tab. BusyBox has various different tags.

BusyBox tags

Here, Let’s open the Docker quickstart terminal.

If you have installed Docker for Mac, Windows, or you use Linux, you can just open up a normal terminal. When we use the image to create a container.

Docker will first look through our local box to find the image if Docker is able to find the image locally. it will use the local image to create the container.

If Docker can’t find the local copy of the image, it will download from the remote registry.

List Docker Images

To find out what image you have in your local box, we can run the docker images command

we don’t have any images in our local box.

Run docker container

Now, let’s start running our container. To run a container, we will use the docker run command, the docker run command will create the container using the image. we specify in the command line and it will spin up the container and run it.

As we learned in the previous article, an image is specified by the repository name and tag. We need to put a colon between the repository name and the tag. e.g Repository name: Tag

So, let’s use the BusyBox image with tag 1.24 After that, we need to specify what command we would like to run in that container and pass the argument for that command.

The command we are going to run echo and let’s put the argument “hello world”. So that Docker should output “hello world” after spinning up the container.

docker run

As you see, Docker goes ahead and downloads the image from the remote repository. That is because we don’t have BusyBox 1.24 on our local box.

After the Docker downloads the image, so, it will create the container from the image and run the container. then, Docker outputs “hello world” which is what we expected.

Now, if we run docker images command again.

docker images

As you see, we have one image which is the one we just downloaded, BusyBox 1.24. And the image has a unique ID, When we run the container again, Docker will use the local copy of the image to create and run the container.

docker run

Let’s see it in action. Notice how faster the execution is. It prints out “hello world” right away. This is because we already the BusyBox 1.24 image in our local box.

So, Docker creates the container from the local image right away without the need to download the image from the remote registry.

Let’s see another example. here, we want to display all the contents in the root directory of the container.

directory list in container

So, the above outputs all the contents under the root directory of the container.

Docker container run in interactive mode

We can also run a container in an interactive mode so that we can go inside the container. So, We need to another two options -i and -t.

A] The -i flag will start an interactive container.

B] The dash t flag will create a pseudo-TTY that attaches standard input and output.

We will keep using BusyBox 1.24 again as the image. Let’s see the below action.

Docker Interactive mode

There we go, it gets us right inside the container. We can run ls command, It gives us all the contents in the root level.

Let’s say we want to create a new file here called a.txt

create file in container

After that, we do ls again. We can see the file is created.

container directory

Now, we can type exit to exit the container.

exit into container

Note that once we exit the container, Docker also shuts down the container.

If we run the same container using the exact same container run command, it will spin up a container. But this time Docker starts a brand new container.

So, If we go and check out the file we created previously by running the ls command, you can see it is not there.

directory list in container

As you see when we run the docker run command, it spins up a new container, the container we created previously with the text file has been shutdown.

Thank you for reading this article, I hope you will understand this article. We’ll get into more details about this when we start playing Docker container command in the next article.

Leave a Reply

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