How to use Docker images from Docker hub and run as containers

Introduction

In the previous article we learned how to install Docker on ubuntu 18.04 and used some basic commands to manage docker service.In this session we will learn how to use Docker images from the official Docker hub and run as containers.

If you are not installed Docker on your system check out our previous tutorial http://bistux.com/bl/2020/07/18/how-to-install-and-use-docker-on-ubuntu-16-04-18-04-aws-ec2/

What is DockerHub?

DockerHub is a cloud-based repository run and managed by Docker Inc. It’s an online repository where Docker images can be published and used by users all over the world.

There are both public and private repositories. If you are a company, you can create e a private repository for use within your own organization, whereas public images can be used by anyone. You can also use official Docker images that are published publicly

Download images from Docker Hub

Docker containers are built from Docker images. By default, Docker pulls these images from Docker Hub. So most applications and Linux distributions you’ll need will have images published on Docker Hub.

You can search for images available on Docker Hub as :

docker search <imagename>

For example if you want ubuntu image type:

docker search ubuntu

This command will search for all available images matching the string name from Docker hub and list

In the OFFICIAL column, OK indicates that images are build and supported by the company.To know more about docker official images visit https://docs.docker.com/docker-hub/official_images/ .

After identifying the desired image you would like to use download it in your system locally using docker pull command

If you want to use ubuntu official image run :

docker pull ubuntu 

You will get output as below

You can also search for official docker images on https://hub.docker.com/search?q=&type=image&image_filter=official

As you can see we can select tags for the image.If tag is not specified ,by default image with latest tag will be downloaded.

To see the images that have been downloaded to your computer, type:

docker images

The output should like

Running a Docker Container from Docker image

Now you can run the downloaded ubuntu latest image as container.To learn more about docker containers checkout https://www.docker.com/resources/what-container

we use combination of -i and -t switch to get the interactive shell access into the container.So our command will be

docker run -it ubuntu

After this your command prompt will switch and working inside the container.Output should look like :

docker@docker:~$ docker run -it ubuntu
root@614cb7d3c611:/#

Note the container id in the command prompt. In this example, it is 614cb7d3c611. You’ll need that container ID later to identify the container when you want to remove it.

Now you can run any command inside the container as the original ubuntu machine. For example, let’s update the packages inside the container. You don’t need to prefix any command with sudo, because you’re operating inside the container as the root user:

root@614cb7d3c611:/# apt update

Then Install an application.For example install nginx webserver on ubnutu container

root@614cb7d3c611:/# apt install nginx 

This will install nginx from the official ubuntu repositories.Will check the nginx status after installation:

We can see nginx is running on the container.

Now we can exit from the container by using exit command

root@614cb7d3c611:/# exit
exit

Next let us see how we can manage docker containers

How to Manage Docker Containers

After creating docker containers you will have active (running) and inactive containers in your system.

To view all active containers run :

docker ps

You will see output similar to the following:

CONTAINER ID        IMAGE               COMMAND             CREATED     

In this tutorial we have started ubuntu docker container.Now it is not running but exist in our system

To view all containers (active & inactive ) run :

docker ps -a

We will get output look like

docker@docker:~$ docker ps -a

 CONTAINER ID        IMAGE                            COMMAND                  CREATED             STATUS                         PORTS                            NAMES 614cb7d3c611        ubuntu                           "/bin/bash"              2 hours ago         Exited (0) About an hour ago                                    confident_heisenberg

To start a stopped container run docker start command with container id or container name.Run command as

docker start 614cb7d3c611

The container will start, and you can use docker ps to see its status:

docker@docker:~$ docker ps 
CONTAINER ID        IMAGE                            COMMAND                  CREATED             STATUS                  PORTS                            NAMES
614cb7d3c611        ubuntu                           "/bin/bash"              2 hours ago         Up 9 seconds                                             confident_heisenberg

To stop a running container we use docker stop followed by container ID or container name .Here we use container name assigned by docker ie confident_heisenberg.So it will be as

docker stop confident_heisenberg

To remove a container run docker rm command followed by container id or name

docker rm 614cb7d3c611

Now we can run the same ubuntu image by giving a custom name using –name switch rather than default name assigned by Docker.

docker run --name=myubuntu -it ubuntu 

Now check our container running with the given name

docker ps -a

Here we can our new container running with the given name “myubuntu”

Conclusion

In this tutorial we worked with docker images and containers.Also learned commands to manage docker containers with sample examples.

Leave a Reply