Post

Docker 3 - How to manage Docker images

Introduction

  • Docker Images Management:
    • Docker images are large files stored on your PC.
    • Tools for Management:
      • Use Docker CLI and Docker Desktop to build, list, remove, and run images.
    • How It Works:
      • Management is done through the Docker client (docker command).
      • The client sends commands to the dockerd daemon, which executes them.

How to Build a Docker Image

  • We use the docker build command to build Docker images.
  • Let’s assume we use the Dockerfile definition from earlier to build an image.
  • Here’s an example that shows the build command:
1
docker build -t temp-ubuntu .
  • Here’s the output the build command generates:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Sending build context to Docker daemon  4.69MB
Step 1/8 : FROM ubuntu:18.04
 ---> a2a15febcdf3
Step 2/8 : RUN apt -y update && apt install -y wget nginx software-properties-common apt-transport-https && wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb && dpkg -i packages-microsoft-prod.deb && add-apt-repository universe && apt -y update && apt install -y dotnet-sdk-3.0
 ---> Using cache
 ---> feb452bac55a
Step 3/8 : CMD service nginx start
 ---> Using cache
 ---> ce3fd40bd13c
Step 4/8 : COPY ./default /etc/nginx/sites-available/default
 ---> 97ff0c042b03
Step 5/8 : WORKDIR /app
 ---> Running in 883f8dc5dcce
Removing intermediate container 883f8dc5dcce
 ---> 6e36758d40b1
Step 6/8 : COPY ./website/. .
 ---> bfe84cc406a4
Step 7/8 : EXPOSE 80:8080
 ---> Running in b611a87425f2
Removing intermediate container b611a87425f2
 ---> 209b54a9567f
Step 8/8 : ENTRYPOINT ["dotnet", "website.dll"]
 ---> Running in ea2efbc6c375
Removing intermediate container ea2efbc6c375
 ---> f982892ea056
Successfully built f982892ea056
Successfully tagged temp-ubuntu:latest

What is an image tag?

  • Image Tag:

    • A text string used to version a Docker image.
    • Example: temp-ubuntu:latest where latest is the tag.
  • Tagging Images:

    • Use the -t flag during image build to name and tag an image.
    • If no tag is specified, the image is automatically labeled with the latest tag.
  • Multiple Tags:

    • An image can have multiple tags.
    • Conventionally, latest is used for the most recent version, along with a tag for the version number.
    • When releasing a new version, you can reassign the latest tag to point to the new image.
  • Windows Base Container Images:

    • Microsoft doesn’t provide a latest tag for Windows base images.
    • You must specify a specific tag, such as ltsc2016, ltsc2019, or ltsc2022 for Windows Server Core images.
  • Example with .NET Core:

    • Microsoft offers multiple .NET Core sample images, each tagged for different platforms like aspnetapp, wcfservice, and dotnetapp.
    • Tags help specify which sample or version of .NET Core the image is related to.

How to List Docker Images

  • Listing Images:

    • Use the docker images command to list all images on your PC.
    • Here’s an example of the output:
1
docker images
  • Output:
1
2
3
4
REPOSITORY          TAG                     IMAGE ID            CREATED                     SIZE
tmp-ubuntu          latest             f89469694960        14 minutes ago         1.69GB
tmp-ubuntu          version-1.0        f89469694960        14 minutes ago         1.69GB
ubuntu              18.04                   a2a15febcdf3        5 weeks ago            64.2MB
  • Notice how the image is listed with its Name, Tag, and an Image ID.
  • Recall that we can apply multiple labels to an image.
  • The preceding output shows an example; even though the image names are different, we can see the IDs are the same.
  • The image ID is a useful way to identify and manage images where the name or tag of an image might be ambiguous.

How to remove an image

  • You can remove an image from the local docker registry with the docker rmi command.
  • This is useful if you need to save space on the container host disk, because container image layers add up to the total space available.
  • Specify the name or ID of the image to remove.
  • This example removes the image for the sample web app using the image name:
1
docker rmi temp-ubuntu:version-1.0
  • You can’t remove an image if a container is still using the image.
  • The docker rmi command returns an error message, which lists the container that relies on the image.
This post is licensed under CC BY 4.0 by the author.