Docker

Docker run image

Docker images are nothing but a set of read-only files when I say a set of read-only files it means once a docker images build it cannot be modified, but you can always create a new image with the help of existing docker image. Docker images are used to build containers. So, when we run the docker image with the help of docker run command, it produces output as a docker container. You can also say containers are nothing but an instance of a docker image. You can also create any number of containers from the same docker image. In simple terms, the docker image is a blueprint of docker container or definition of a docker container. Docker container is an actual place where the live application or the database or any other software application runs.

Every docker image contains some necessary sets of files. These files are nothing but a small part of an operating system that is required to run docker container as an isolated unit of any machine. So, you can say this part is a minimal part of an operating system or operating system userspace minus operating system kernel.

In this tutorial, we will show you how to use docker run image command in Linux.

Requirements

  • A Linux system with Docker installed.
  • A root password is configured in your system.

Basic Syntax

Docker image is a read-only template, composed of a layered file system, needed to build a running docker container.

The basic syntax of docker images command is shown below:

docker image [OPTION]

A brief explanation of each option is shown below:

build: This option is used to build an image from the docker file.
pull: This option is used to download an image from the docker registry.
push: This option is used to upload or push an image to the docker registry.
save: This option is used to save an image to the tar archive.
prune: This option is used to remove all unused images.
rm: This option is used to remove one or more images.
history: This option is used to display the history of an image.
load: This option is used to load an image from the tar archive.

Download Docker Image

You can download and run any image from the Docker Hub using the following syntax:

docker run image

For example, to download an Nginx image from the Docker Hub run the following command:

docker pull nginx

This will download the Nginx image, as shown below:

Using default tag: latest
latest: Pulling from library/nginx
8559a31e96f4: Pull complete
8d69e59170f7: Pull complete
3f9f1ec1d262: Pull complete
d1f5ff4f210d: Pull complete
1e22bfa8652e: Pull complete
Digest: sha256:21f32f6c08406306d822a0e6e8b7dc81f53f336570e852e25fbe1e3e3d0d0133
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

Now, you can run the downloaded image in the container with the following command:

docker container run -dt -p 8080:80 nginx

This will start the Nginx container and map TCP port 80 in the container to port 8080 on the Docker host:

ba43241e3ce3951d8599ce87450c64ea944c45e484922dbccbb22231a3ab244a

You can see your running container with the following command:

docker ps

You should see the following output:

CONTAINER ID  IMAGE  COMMAND                 CREATED        STATUS         PORTS                  NAMES

ba43241e3ce3  nginx  "/docker-entrypoint.…" 2 minutes ago,  Up 2 minutes  0.0.0.0:8080->80/tcp  frosty_bassi

List Docker Image

You can list all the images available in your system with the following command:

docker images

Or

docker image ls

You should see the following output:

REPOSITORY       TAG             IMAGE ID         CREATED           SIZE
nginx           latest        2622e6cca7eb     4 weeks ago         132MB

You can also use the option -q to display only image ID of the images:

docker images -q

You should see the following output:

2622e6cca7eb

Conclusion

In the above guide, you learned what docker image is and how to download and run images from the Docker Hub. You can use this guide to learn the basics of the Docker image.

About the author

Hitesh Jethva

Hitesh Jethva

I am Hitesh Jethva lives in Ahmedabad, INDIA. I am Linux system administrator and Technical writer.

I felt in love with Linux while i was started to learn Linux. I am a fan of open source technology and have more than 8+ years of experience in Linux and Open Source technologies. My main motto is to make uncomplicated things easier.

I have extensive experience within the following areas: CentOS/RHEL/FreeBSD/Ubuntu/Debian, cPanel/WHM, High Availability Architecture, Proactive security/Web Application Security, Amazon Web Services (AWS), Apache/Nginx, OpenLDAP, DevOps automation: Chef/Docker.

My expertise includes Linux system administration, installation, configuration, tuning, security and troubleshooting. I have large skills in configuring/management/support of Apache, Nginx, MariaDB, MongoDB, PHP-FPM, Nagios, Zabbix, Graylg, Redis, Docker and much more.