CodeGym /Courses /Docker SELF /Getting to Know Docker Images

Getting to Know Docker Images

Docker SELF
Level 13 , Lesson 0
Available

1.1 Basics of Docker Images

Docker Images are templates for Docker containers. They include everything you need to run applications: code, libraries, dependencies, tools, and configuration files. Understanding what Docker Images are and how they are structured is important for effectively using Docker.

Docker Image is an immutable template used to create containers. It contains all the necessary components to run an application in an isolated environment. When you start a container, Docker creates an instance of the image and launches it.

Key features of Docker Images:

  1. Immutability: Docker Images are immutable. Any changes in the container don't affect the original image. To modify the content of an image, you need to create a new image.
  2. Layered structure: Docker Images consist of multiple layers. Each layer represents a change compared to the previous one. This allows efficient storage by reusing layers.
  3. Identification: Each image has a unique identifier (SHA256 hash) and can have one or more tags to simplify version management of images.

Structure of Docker Images:

Docker Images have a layered structure where each layer is a file system. These layers are stored in the Docker cache and can be reused by other images. The layered structure makes Docker Images lightweight and efficient. For example, the first layer could be the operating system, the second — Python, and the third — your application.

Main components of Docker Images structure:

  1. Base Layer: The initial layer of an image, often used as a foundation for creating other images. For example, it could be a minimal version of an operating system like Ubuntu or Alpine Linux.
  2. Intermediate Layers: These layers are created based on commands executed in the Dockerfile (like installing packages, copying files, etc.). Each layer adds changes to the previous one.
  3. Final Layer: The final layer, created based on the CMD, ENTRYPOINT instructions, or changes inside the container, is called the "container" layer. This layer is temporary and exists only during the container's runtime.

1.2 How Docker Image Layers Work

Each layer of a Docker image is a set of changes compared to the previous layer. When Docker builds an image, it executes each instruction from the Dockerfile and adds a new layer for each change.

Example of Layer Creation:

  1. Base Image: Let's start with a base image, for example, ubuntu:20.04. This will be the first layer.
  2. Package Installation: The command RUN apt-get update && apt-get install -y python3 will create a new layer with Python installed.
  3. Copying Files: The command COPY . /app will add the application files into a new layer.
  4. Setting the Working Directory: The command WORKDIR /app will create a layer that sets the working directory for subsequent commands.

Advantages of Using Layers:

  1. Reusability: Layers can be reused by other images. For example, if two images use the same base layer, Docker will download it only once, saving space and time.
  2. Caching: Docker caches layers, which speeds up the image-building process. If a layer hasn't changed, Docker uses it from the cache instead of rebuilding it.
  3. Modularity: The layered structure allows you to design images modularly. You can update or modify individual layers without affecting the whole image.

Identification and Tags for Docker Images:

Each Docker Image is identified by a unique hash (SHA256) generated based on the image's content and all its layers. For easier management and versioning, Docker Images can also have tags.

Example of Image Identification:

The following command will show a list of all images on your system, including their repository, tags, IDs, and sizes:

Terminal

docker images

1.3 Working with Docker Images

Docker gives you a bunch of commands to work with images. Here are the main ones:

  • Creating an image: docker build
  • Running a container from an image: docker run
  • Listing images: docker images
  • Deleting an image: docker rmi
  • Searching for images on Docker Hub: docker search
  • Pulling an image from Docker Hub: docker pull
  • Pushing an image to Docker Hub: docker push

Example of creating and using a Docker Image:

1. Creating a Dockerfile:

dockerfile

# Use a base Ubuntu image
FROM ubuntu:20.04

# Install Python
RUN apt-get update && apt-get install -y python3

# Copy app files into the container
COPY . /app

# Set the working directory
WORKDIR /app

# Specify the command to run the app
CMD ["python3", "app.py"]

2. Building the image:

The following command will create an image called my_python_app using the Dockerfile from the current directory:

Terminal

docker build -t my_python_app .

3. Running a container from the image:

Use the following command to run a container from the created image my_python_app in the background:

Terminal

docker run -d my_python_app
3
Task
Docker SELF, level 13, lesson 0
Locked
Viewing the list of local Docker Images
Viewing the list of local Docker Images
3
Task
Docker SELF, level 13, lesson 0
Locked
Creating and Running a Docker Image
Creating and Running a Docker Image
3
Task
Docker SELF, level 13, lesson 0
Locked
Searching for images on Docker Hub
Searching for images on Docker Hub
3
Task
Docker SELF, level 13, lesson 0
Locked
Removing a Docker Image
Removing a Docker Image
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION