What is Docker Image?

Docker Image is an executable package of software that includes everything needed to run an application. This image informs how a container should instantiate, determining which software components will run and how. Docker Container is a virtual environment that bundles application code with all the dependencies required to run the application. The application runs quickly and reliably from one computing environment to another.

Table of Content

  • What is Docker Image?
  • SubCommands of Docker Image
  • Docker Image Prune
  • Docker Image Build
  • Docker Image Tag
  • Uses of Docker Images
  • Difference between Docker Image VS Docker Container
  • Structure Of Docker Image 
  • How To Create A Docker Image And Run It As Container?
  • How to Build Docker Python Images? A Step-By-Step Guide
  • Docker Image commands
  • Docker Images – FAQs

What is Docker Image?

Docker images are built using the Dockerfile which consists of a set of instructions that are required to containerize an application. The docker image includes the following to run a piece of software. A docker image is a platform-independent image that can be built in the Windows environment and it can be pushed to the docker hub and pulled by others with different OS environments like Linux.

  • Application ode.
  • Runtime.
  • Libraries
  • Environmentaltools.

Docker image is very light in weight so can be portable to different platforms very easily.

SubCommands of Docker Image

The following are the some of the sub commands that are used with Docker Image:

Command

Description

docker image build

This command is used for building an image from the Dockerfile

docker image history

It is used for knowing the history of the docker image

docker image inspect

It is used for displaying the detailed information on one or more images

docker image prune

It used for removing unused images that are not associated with any containers

docker image save

This command helps in saving the docker images into a tar archived files

docker image tag

It helps in crating a tag to the target image that refers to the source image.

Docker Image Prune

Docker image prune is a command used in the docker host to remove the images that are not used or Docker image prune command is used to remove the unused docker images.

docker image prune
  • All the unused images are also know as dangling images which re not associated with any containers

Docker Image Build

The Following is the command which is used to build the docker image.

docker build -t your_image_name:tag -f path/to/Dockerfile .

  • Docker build: Initiates the build process.
  • -t your_image_name:tag: Gives the image you’re creating a name and, if desired, a tag.
  • path/to/Dockerfile . : Gives the location of the Dockerfile. Give the right path if it’s not in the current directory. “(.) DOT” represents the current wordir.

Docker Image Tag

Docker tags are labels for container images, used to differentiate versions and variants of an image during development and deployment. Docker tags will help you identify the various versions of docker images and help distinguish between them. Docker image will help us to build continuous deployment very quickly

Uses of Docker Images

The following are the uses of Docker Images:

  1. We can easily and effectively run the containers with the aid of docker images.
  2. All the code, configuration settings, environmental variables, libraries, and runtime are included in a Docker image.
  3. Docker images are platform-independent. 
  4. Layers are the building blocks of an image. 
  5. WithWhen using the build command, the user has the option of completely starting from scratch or using an existing image for the first layer.

Difference between Docker Image VS Docker Container

The following are the difference between Docker Image and Docker Container:

Docker image Docker container 
The Docker image is the Docker container’s source code. The Docker container is the instance of the Docker image.
Dockerfile is a prerequisite to Docker Image. Docker Image is a pre-requisite to Docker Container.
Docker images can be shared between users with the help of the Docker Registry.  Docker containers can’t be shared between the users.
To make changes in the docker image we need to make changes in Dockerfile. We can directly interact with the container and can make the changes required.

Structure Of Docker Image

The layers of software that make up a Docker image make it easier to configure the dependencies needed to execute the container. 

  • Base Image: The basic image will be the starting point for the majority of Dockerfiles, and it can be made from scratch.
  • Parent Image: The parent image is the image that our image is based on. We can refer to the parent image in the Dockerfile using the FROM command, and each declaration after that affects the parent image.
  • Layers: Docker images have numerous layers. To create a sequence of intermediary images, each layer is created on top of the one before it. 
  • Docker Registry: Refer to this page on the Docker Registry for further information.

How To Create A Docker Image And Run It As Container?

Follow the below steps to create a Docker Image and run a Container:

Step 2: Run the following command in the terminal and it will create a docker image of the application and download all the necessary dependencies needed for the application to run successfully.

docker build -t <name>:<tag> 

This will start building the image.

Step 3:  We have successfully created a Dockerfile and a respective Docker image for the same.

Step 4: Run the following command in the terminal and it will create a running container with all the needed dependencies and start the application.

docker run -p 9000:80 <image-name>:<tag> 

The 9000 is the port we want to access our application on. 80 is the port the container is exposing for the host to access.

How to Build Docker Python Images? A Step-By-Step Guide

Step 1: Create A Dockerfile

  • Firstly write a dockerfile for supporting python applications and try to python application running instruction with the CMD options for the default run. The following is the sample dockerfile that we are using here:
# Use the official Python image as a base
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed dependencies specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

Step 2: Create requirements.txt and app.py Files

  • Try on provide the requirements.txt with specifying the names of required software names and app.py file with writing the python application. The following are the sample code of those requirements.txt and app.py.

Filename: requirements.txt

Flask==2.1.0

Filename: app.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello, World!'

if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=80)

Step 3: Build a Docker Image

  • The following command is used for building the docker python image from the Dockerfile:
 docker image build -t mypython-app:v1 .
  • The following shows it practically of building of python based docker image from the Dockerfile:

Step 4: Verify the Docker Image

  • Use the following command to verify the success build of docker application:
docker images
  • The following screenshot shows the success build of python based docker image.

Step 5: Run a Python based Docker Container

  • Run a container to that docker python image with the following command:
docker run -dit-p 80:80  --name  mycontainer1 mypython-app:v1
  • The following command is the successful run of that python based docker container:

Step 6: Access the Docker Container

  • Now, try on access the docker container with public IP of the host system with the exposed port to see the successful run of the containerized python application. The following screenshot illustrates the successful accessing of it.

Docker Image commands

The following are the some of the Docker Image Commands that are widely used:

List Docker Images

docker images

Example:

$ docker ls

REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 0d9c6c5575f5 4 days ago 126MB
ubuntu 18.04 47b199b0cb85 2 weeks ago 64.2MB

Pull an Docker Image From a Registry

docker image pull <image-name>

Example:

$ docker pull alpine:3.11

3.11: Pulling from library/alpine
Digest: sha256:9f11a34ef1c67e073069f13b09fb76dc8f1a16f7067eebafc68a5049bb0a072f
Status: Downloaded newer image for alpine:3.11

Docker Images Prune

  • The Docker Image Prune will remove the unused Docker Images that are not associated with any containers. The Command looks as follows:
docker image prune

Docker Images Filter

  • When we want to filter some specific images we can filter out them using its labels or its meta information tags with the option –filter. The command looks as follows:
docker image ls -f "reference=mypython-app"

 Remove an Image from Docker

  • The following command is used for removing the docker image from the local registry:
docker rmi <id-of-image>

Example:

$ docker rmi <image_id>

Untagged: <image_id>
Deleted: sha256:<image_id>

 Searching for a specific image on Docker Hub

docker search ubuntu

Example:

$ docker search ubuntu

NAME                             DESCRIPTION                                                          STARS                 OFFICIAL                          AUTOMATED

ubuntu                           Ubuntu is a Debian-based Linux operating s…                4458                    [OK]               

ubuntu-upstart              Upstart is an event-based replacement for …                  62                         [OK]               

tutum/ubuntu                 Simple Ubuntu docker images with ssh access             49                          [OK]               

ansible/ubuntu14.04-ansible

Docker Images – FAQs

What is Docker Image VS Docker Container?

  • Docker Image: It is the blue print of Docker container.
  • Docker container: It is an instance of the Image.

Refer to docker image vs docker container.

What is DockerHub?

Docker Hub is a repository service and it is a cloud-based service where people push their Docker Container Images and also pull the Docker Container Images from the Docker Hub anytime or anywhere via the internet. It provides features such as you can push your images as private or public.

How to use Docker Run Command?

Docker run command will run the docker image as an container. docker ps: Docker run will list all the containers which are running in the docker host.

How to list Docker Images?

The command to list all the docker images was mentioned below.

docker images

Where are my docker images stored?

The docker images are stored in the default location based on your operating system such as
/var/lib/docker on Linux distributions, C:\ProgramData\docker on Windows and /Users/<username>/Library/Containers/com.docker.docker/Data on MacOS.

How do I find the Docker Images?

You can find the docker images by listing the docker images using the following docker command:

docker image ls

How to Store Docker Images Locally?

The Docker Images are stored locally by default when you build or pull them. You can save them into tar archive files using the following command:

docker image save

How do I change the Docker Image location?

We can change the default location of the docker images from where it is storing by changing the docker daemon configuration settings through the file /etc/docker/daemon.json by specifying a different location value for the data-root key. Then restart the docker daemon to update the changes to take effect.



Contact Us