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](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/Dockerhub.png?resize=1000%2C471&ssl=1)
Just search for BusyBox. Let’s click the first one which is the official BusyBox repository.
![BusyBox docker repository](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/busybox1.png?resize=1000%2C581&ssl=1)
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](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/busybox2.png?resize=1000%2C588&ssl=1)
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](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/busyboxtags.png?resize=1000%2C603&ssl=1)
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
![](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/dockerimages.png?resize=1000%2C395&ssl=1)
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](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/docker-run.png?resize=1000%2C487&ssl=1)
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](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/dockerimages2.png?resize=1000%2C488&ssl=1)
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](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/dockerrun2.png?resize=1000%2C475&ssl=1)
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](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/dirlist.png?resize=1000%2C480&ssl=1)
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](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/DockerInteract.png?resize=852%2C100&ssl=1)
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](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/touch.png?resize=844%2C194&ssl=1)
After that, we do ls again. We can see the file is created.
![container directory](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/rootlist.png?resize=1004%2C173&ssl=1)
Now, we can type exit to exit the container.
![exit into container](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/exit.png?resize=972%2C165&ssl=1)
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](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/shutsdown.png?resize=836%2C469&ssl=1)
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.