· Tutorials  Â· 5 min read

Docker for beginners - Part 2 - Docker Images and Containers

Docker image is a read-only template that contains instructions for creating a Docker container. It is a snapshot of a Docker container. In this tutorial, you will learn about Docker images and containers.

Docker image is a read-only template that contains instructions for creating a Docker container. It is a snapshot of a Docker container. In this tutorial, you will learn about Docker images and containers.

Series Overview

Docker image is a read-only template that contains instructions for creating a Docker container. It is a snapshot of a Docker container. Docker images are stored in a Docker registry. Docker Hub is a public Docker registry that contains thousands of Docker images. You can also create your own Docker registry to store your Docker images, which is very well outside the scope of this tutorial.

You can think Docker Image as a class and Docker Container as an instance of that class, by Object Oriented Programming analogy. You can create multiple Docker containers from a single Docker image. Well, you can create multiple instances of a class, right? Anyways, we explore with an example in the next section.

Docker Hub

For our tutorial, we will browse Docker Hub and pull a Docker image. We will then create a Docker container from the Docker image.

Go to https://hub.docker.com and search for nginx. You will see a list of Docker images that matches your search term. Click on the first result.

Docker Hub

Now comes the important point of Docker

Docker images are used to package up applications and pre-configured server environments. You can use them for anything from running simple scripts, to testing out a web server running on a Linux distribution, to running a full database. The instance of the image you use is called a Docker container.

These containers are isolated from each other and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels. All containers are run by a single operating system kernel and therefore use fewer resources than virtual machines. That’s why Docker became so popular in the DevOps world.

Using a Docker Image

Before we use a Docker Image, we’ll first look at Docker CLI. Docker provides a command-line interface (CLI) tool called docker to interact with Docker daemon. Docker daemon is a background process that manages Docker objects like images, containers, networks, and volumes. Docker CLI uses Docker REST API to interact with the Docker daemon.

We will go in detail about Docker CLI in the next part of this tutorial. For now, let’s use Docker CLI to pull a Docker image from Docker Hub.

Open a terminal and run the following command.

docker pull nginx

The above command will pull the latest version of the nginx Docker image from Docker Hub. You can also specify the version of the Docker image to pull. For example, to pull the nginx Docker image with version 1.19.10, run the following command.

docker pull nginx:1.19.10

You can also pull the nginx Docker image with version 1.19 by running the following command.

docker pull nginx:1.19

This pull command will download the nginx Docker image from Docker Hub and store it in your local machine. You can view the list of Docker images in your local machine by running the following command.

docker images

You will see the following output, for pulling 3 different versions of the nginx Docker image.

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              4bb46517cac3        2 weeks ago         133MB
nginx               1.19.10             4bb46517cac3        2 weeks ago         133MB
nginx               1.19                4bb46517cac3        2 weeks ago         133MB

Using a Docker Container

With these Docker images pulled to your local machine, you can now create Docker containers from them. To create a Docker container from a Docker image, you need to run the docker run command. Let’s create a Docker container from the nginx Docker image.

docker run nginx

The above command will create a Docker container from the nginx Docker image. Now the container runs in the foreground and you can see the logs in your terminal. You can stop the container by pressing Ctrl + C. We can also run the container in the background by passing the -d flag.

docker run -d nginx

The above command will create a Docker container from the nginx Docker image and run it in the background.

To check the status of the Docker container, run the following command.

docker ps

You will see the following output, for running 2 different versions of the nginx Docker image, with one container stopped and another still running.

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
f3b7b0b3b6b1   nginx     "/docker-entrypoint.…"   2 minutes ago    Stopped                  keen_mendeleev
e3b7b0b3b6b1   nginx     "/docker-entrypoint.…"   2 minutes ago    Up 2 minutes    80/tcp    keen_mendeleev

The container we created, is a web server. You can access the web server by visiting http://localhost in your browser. You will see the following output.

Nginx

You can stop a Docker container by running the following command.

docker stop <container-id>

You can also remove a Docker container by running the following command.

docker rm <container-id>

Wrapping Up

In this tutorial, you learned about Docker images and containers. You also learned how to pull a Docker image from Docker Hub and create a Docker container from it. In the next part of this tutorial, you will learn about Docker CLI and Dockerfile.

Next - Docker for beginners - Part 3

Back to Blog

Related Posts

View All Posts »
Docker for beginners - Part 5 - Docker Compose

Docker for beginners - Part 5 - Docker Compose

Docker Compose is a tool for running multi-container applications. It defines the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.

Docker for beginners - Part 3 - Docker CLI

Docker for beginners - Part 3 - Docker CLI

Docker CLI is the command-line interface for Docker. It allows you to interact with Docker daemon using commands. You can use Docker CLI to build, run, and manage Docker containers. In this tutorial, we will learn about some of the most commonly used Docker CLI commands.