How to run docker images as a container

In the previous blog, we discussed How to install docker from official docker repo in ubuntu 20.04
In this blog, I will explain to you what is docker container and its working with some basic docker commands. we will be pulling the Nginx docker image from the docker hub to run our first container.

Getting started with basic Docker commands.

To get the list of all docker commands type.

docker

To see the version of the docker installed type.

docker –version

In order to get the information of docker commands type.

docker <command> –help

docker images –help

How to use docker help command

It is impossible to explain the use cases of all docker commands in a single blog. So that i am going to show some of the basic commands used by running a container inside docker.
So that you will be able to know all the details of each docker command using the –help flag.

Getting deeper into docker and it’s working.

I think still you don’t have an exact answer to the questions below

What is a Docker hub?
What is a docker image?
What is a docker container?

I will explain to you in this blog and you will gain a basic understanding of docker images,docker hub,and Docker containers,

1: Docker hub
Docker hub is a hosted repository provided by Docker used to store and share container images. We can create our own repository and store our own private images in Dockerhub (https://hub.docker.com )with our account or else We can also use the global images stored inside docker in order to run applications (ubuntu,apache,MySQL etc).

I have my own docker hub account and this is the dashboard you will be able to see once you login to https://hub.docker.com

2: image in Docker
Now we know that the docker hub is used to share and store global and private images.
So what is exactly an image in docker?
A Docker image is a file used to execute code in a Docker container. Docker images act as a template which contains a set of instructions to build a Docker container. We can pull an image from the docker hub also we can push our local images to the docker hub.

3: Docker container
Now we understood that the Docker hub is used to store and share a set of images and the images act as a template in order to run a container.
So what is exactly a container?
A container is standard software that packages all the code and dependencies in order to run the application in an isolated environment. Images at run-time are called containers. We can create as many containers from one image.

Wrapping up

Let’s say this in a practical case. In our Virtual machine suppose I want to install an Nginx webserver. The only way to do it is to create a Linux based VM either ubuntu, red hat, centos, etc and install the webserver package.
The problem with that is I need to only run a webserver so I don’t want all other files and libraries of the VM let’s say ubuntu. So ubuntu will have some default files and configurations. So if we want to run even a small application we comprise our storage because ubuntu files will consume some of the storage space.

Docker container is developed in order to overcome these kinds of situations. Ie if I want to run only a webserver I can pull the image from the docker hub and then run it as a container. My running container will consist of only the files and dependencies in order to run the webserver there will be no other garbage files.

So docker hub consists of all the images as packages that hold all the code, dependencies and other libraries in order to run that specific application. Whenever we want to use that application we can directly pull it from the docker hub so only the required libraries and files will be used.

Pulling an image from the docker hub

Now we are familiar with the basic docker commands now let’s create the first image using docker. Either you can use the docker login command to enter into your official docker hub account to pull global and local images or else you can pull images directly without login into the docker hub I am using the second method because it will be more convenient to all of us.

docker images

initially, when we run this command we will get no output as there are no images.

Checking an docker images

Now we are going to pull an image from the docker hub. A Docker hub is a repo where Docker images are stored.
Now we are going to pull the image of Nginx using the docker pull command.

docker pull nginx

pulling an image from docker hub

As you can see in the above images we have successfully pulled the Nginx image and after that, it is also showing while checking the images using docker images.
Now the image is pulled now we are going to run a container based on this image for the first than initially type this command.

Running a container using the image

docker ps

Command to check the running containers

This command will list all the running containers it is showing no output because there are no containers running now.

Next, we are going to run a container based on the previous Nginx image pulled from the docker hub for that enter

docker run -d -p 8080:80 nginx

Note that Nginx is a web server and it requires a port to run so I am mapping the TCP port 80 on the container to the port 8080 on the docker host with the -p flag and the -d flag is used to run the container in detached mode.

Let’s run the docker ps command again and check. As in the below image, we can see that our container is running as we didn’t assign a tag docker has named the container as admiring_bartik.

So far we have pulled the Nginx image from the docker hub and made it run as a container in our system. Let’s put a page and check if Nginx is working on not?

Entering into a running container to verify the installation

In order to enter the running container enter this command

docker exec command is used to run a command in a running container the -t flag is used to Allocate a pseudo-TTY and-i flag is used to Keep STDIN open even if not attached.

Either you can enter the container id or the container name in the docker exec command.

docker exec -ti 5145279acf55 /bin/bash

docker exec -ti admiring_bartik /bin/bash

Now you will be inside the container which is running Nginx.

The default location of Nginx will be in /usr/share/nginx/html/ you can an index.html page inside this location.

curl http://localhost will show the default Nginx page and thus we can confirm that Nginx is running fine in this container.

Summary:

In this blog, we have learned how to create an image, how to run a container using the image and how to enter into a running container and execute commands in it.

9 Comments

Leave a Reply to bbc persianCancel Reply

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