What is a Docker image

A Docker image is a file, comprised of multiple layers, used to execute code in a Docker container. An image is essentially built from the instructions for a complete and executable version of an application, which relies on the host OS kernel. When the Docker user runs an image, it becomes one or multiple instances of that container.

A Docker image is made up of multiple layers. A user composes each Docker image to include system libraries, tools, and other files and dependencies for the executable code. Image developers can reuse static image layers for different projects. Reuse saves time, because a user does not have to create everything in an image.

Most Docker images start with a base image, although a user can build one entirely from scratch, if desired. 

How to pull an image

Docker users store images in private or public repositories, and from there can deploy containers, test images and share them. Docker offers Docker Hub, which is a cloud-based registry service that includes private and public image repositories. It also has Docker Trusted Registry, which adds image management and access control features.

Official images have been produced by Docker, while community images are images created by Docker users.

Take a tour : https://hub.docker.com

To pull an image from Docker Hub, just look for the image you are interested in.

In our example, a CentOS image which can be found here : https://hub.docker.com/_/centos

As you can see on the top of the screen you can easily pull this image by running “docker pull centos” :

You have know downloaded your first image ! You can display it by running “docker images”

How to run a container using an image

Now that we have an image, we can create a container by running our image. To perform this action, just run :

PS C:\Users> docker run centos
PS C:\Users> 

Nothing happened ? Well, not exactly. You just booted up a container from an image, ran an empty command and then exited.

We can run a container and execute a command before exiting :

PS C:\Users> docker run centos echo "Hello World !"
Hello World !
PS C:\Users>

We can display containers that have been run through the following command :

PS C:\Users> docker ps -a

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES

52c234a14dcf        centos              "echo 'Hello World !'"   16 minutes ago      Exited (0) 16 minutes ago                          competent_goldberg

33608b96b57c        centos              "echo 'Hello World !'"   16 minutes ago      Exited (0) 16 minutes ago                          jovial_sanderson
6987492a00b9        centos              "sh"                     About an hour ago   Exited (0) 16 minutes ago                          nifty_almeida

To see running containers, use the command “docker ps

How to run a docker container interactively and let it started

If you want to be able to run more that only one command with your container, run the following command :

PS C:\Users\lionel> docker run -it centos sh

sh-4.2# whoami
root

sh-4.2# pwd
/
sh-4.2#

With “-it” option, you are running your container interactively and it will let it started.

To conclude – More useful commands 🙂

  • “docker history” shows the history of an image, including changes made to it and its layers.
  • “docker update” allows a user to update the configuration of containers.
  • “docker tag” creates a tag, such as target_image, which enables users to group and organize container images.
  • “docker search” looks in Docker Hub, an image repository, for whatever the user needs.
  • “docker save” allows a user to save images to an archive.
  • “docker rmi” removes one or multiple images.

Sources : https://www.techtarget.com

Leave a Comment

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