Wie man ein Docker-Image herunterlädt

DockerDockerBeginner
Jetzt üben

💡 Dieser Artikel wurde von AI-Assistenten übersetzt. Um die englische Version anzuzeigen, können Sie hier klicken

Introduction

Docker has revolutionized the way we develop, deploy, and manage applications. At the heart of this ecosystem are Docker images, which serve as the building blocks for creating and running containerized applications. In this tutorial, we will guide you through the process of pulling Docker images from a registry, a crucial step in leveraging the power of Docker for your projects.

By the end of this lab, you will be able to search for Docker images, pull them from Docker Hub, and understand how to use them in your containerized applications.

Understanding Docker Images

Before we start pulling Docker images, it is important to understand what Docker images are and how they work.

What is a Docker Image?

A Docker image is a lightweight, standalone package that contains everything needed to run an application:

  • Application code
  • Runtime environment
  • Libraries and dependencies
  • Environment variables
  • Configuration files

Images are the templates used to create Docker containers, which are the running instances of these images.

Docker Image Repositories

Docker images are stored in repositories within registries. The most popular public registry is Docker Hub, which hosts thousands of ready-to-use images. Some key points about Docker Hub:

  • It contains official images maintained by software vendors
  • It hosts community-contributed images created by users
  • It allows organizations to have private repositories for proprietary images

Docker Image Naming

Docker images follow a specific naming convention:

[registry/]username/repository:tag

Where:

  • registry is the hostname of the registry (optional, defaults to Docker Hub)
  • username is the Docker Hub username or organization name
  • repository is the image name
  • tag is the image version (optional, defaults to "latest")

For example: ubuntu:22.04 or nginx:latest

Checking Your Docker Installation

Before proceeding, let's make sure Docker is properly installed on your system. Open a terminal and run:

docker --version

You should see output similar to:

Docker version 20.10.21, build baeda1f

This confirms that Docker is installed and ready to use.

In the next step, we will search for and pull our first Docker image.

Searching for and Pulling Your First Docker Image

Now that we understand what Docker images are, let's learn how to find and pull them from Docker Hub.

Searching for Docker Images

Before pulling an image, you might want to search for available options. Use the docker search command to find images on Docker Hub:

docker search ubuntu

This will show you a list of Ubuntu-related images available on Docker Hub. You should see output similar to:

NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu                           Ubuntu is a Debian-based Linux operating s...   14938     [OK]
ubuntu-upstart                   Upstart is an event-based replacement for ...   111       [OK]
rastasheep/ubuntu-sshd           Dockerized SSH service, built on top of of...   256                  [OK]
...

The OFFICIAL column marked with [OK] indicates images maintained by Docker or the software vendor.

Pulling a Docker Image

To download a Docker image to your local machine, use the docker pull command followed by the image name:

docker pull ubuntu:22.04

This command pulls the Ubuntu 22.04 image from Docker Hub. You should see download progress similar to:

22.04: Pulling from library/ubuntu
2ab09b027e7f: Pull complete
Digest: sha256:2b7412e6465c3c7fc5bb21d3e6f1917c167358449fecac8176c6e496e5c1f05f
Status: Downloaded newer image for ubuntu:22.04
docker.io/library/ubuntu:22.04

The output shows that Docker is downloading individual layers of the image. Each layer represents a set of file system changes.

Pulling Images Without a Tag

If you don't specify a tag, Docker will pull the image tagged as latest:

docker pull nginx

This will pull the latest version of the Nginx web server image:

Using default tag: latest
latest: Pulling from library/nginx
a603fa5e3b41: Pull complete
(...more layers...)
Digest: sha256:f9c305f882a7062db720e582ce619686cbe29742eea6e1db6dcf84b200eec560
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

Pulling from Other Registries

By default, docker pull downloads images from Docker Hub. To pull from other registries, include the registry hostname:

docker pull mcr.microsoft.com/dotnet/sdk:6.0

This command pulls the .NET SDK 6.0 image from Microsoft Container Registry.

Now that you have pulled some Docker images, let's move on to the next step to learn how to manage them.

Managing Docker Images

After pulling Docker images, you need to know how to list, inspect, and manage them effectively.

Listing Docker Images

To see all the Docker images you have downloaded to your local machine, use the docker images command (or its alias docker image ls):

docker images

You should see output similar to:

REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
ubuntu        22.04     58db3edaf2be   3 weeks ago    77.8MB
nginx         latest    605c77e624dd   4 weeks ago    142MB

This output shows:

  • REPOSITORY: The name of the image
  • TAG: The version of the image
  • IMAGE ID: A unique identifier for the image
  • CREATED: When the image was created
  • SIZE: The size of the image on disk

Inspecting Docker Images

To get detailed information about a specific image, use the docker inspect command:

docker inspect ubuntu:22.04

This command displays a JSON array containing all the details about the image, including:

  • Layer information
  • Environment variables
  • Architecture
  • Operating system
  • Configuration

The output is quite long and detailed. Here's a snippet of what you might see:

[
    {
        "Id": "sha256:58db3edaf2beXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        "RepoTags": [
            "ubuntu:22.04"
        ],
        "RepoDigests": [
            "ubuntu@sha256:2b7412e6465c3fc7fc5bb21d3e6f1917c167358449fecac8176c6e496e5c1f05f"
        ],
        ...
    }
]

Image History

To see the layer history of an image (how it was built), use the docker history command:

docker history ubuntu:22.04

The output shows each layer that makes up the image:

IMAGE          CREATED       CREATED BY                                      SIZE
58db3edaf2be   3 weeks ago   /bin/sh -c #(nop)  CMD ["bash"]                0B
<missing>      3 weeks ago   /bin/sh -c #(nop) ADD file:15efc167a417...     77.8MB

Removing Docker Images

When you no longer need an image, you can remove it to free up disk space using the docker rmi command:

docker rmi nginx

If successful, Docker shows the removed image IDs:

Untagged: nginx:latest
Untagged: nginx@sha256:f9c305f882a7062db720e582ce619686cbe29742eea6e1db6dcf84b200eec560
Deleted: sha256:605c77e624ddb75e6110f997c58876bba43f0blindividualayeridshereXX
(...more layers deleted...)

If the image is being used by a container, you'll need to remove the container first or use the -f (force) option:

docker rmi -f nginx

Now you know how to manage your Docker images. Let's proceed to the final step where we'll actually use an image to run a container.

Using Docker Images

Pulling Docker images is only the first step. Now, let's learn how to use these images to run containers.

Running a Container from an Image

To create and start a container from an image, use the docker run command:

docker run ubuntu:22.04 echo "Hello from Ubuntu container"

This command:

  1. Creates a new container from the ubuntu:22.04 image
  2. Runs the echo "Hello from Ubuntu container" command inside the container
  3. Displays the output:
Hello from Ubuntu container

After executing the command, the container stops because it has completed its task.

Running an Interactive Container

To start an interactive shell inside a container, use the -i (interactive) and -t (terminal) options:

docker run -it ubuntu:22.04 bash

This gives you a bash shell inside the Ubuntu container where you can run commands:

root@a1b2c3d4e5f6:/## ls
bin   dev  home  lib32  libx32  mnt  proc  run   srv  tmp  var
boot  etc  lib   lib64  media   opt  root  sbin  sys  usr

To exit the container, type exit or press Ctrl+D:

root@a1b2c3d4e5f6:/## exit
exit

Running a Container in Detached Mode

To run a container in the background (detached mode), use the -d option:

docker run -d --name nginx-server -p 8080:80 nginx

This command:

  1. Creates a container named nginx-server from the nginx image
  2. Runs it in detached mode (-d)
  3. Maps port 8080 on your host to port 80 in the container
  4. Returns a container ID:
e1d0ac1dcb21a93d9d878dcf40c054eb9f3c2b1bf5ecce7c29b6fa8ce6b219c1

Accessing the Running Container

Now you can access the Nginx web server at http://localhost:8080 in your browser, or use curl to verify it's working:

curl localhost:8080

This should display the Nginx welcome page HTML:

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...
</html>

Listing Running Containers

To see all running containers, use:

docker ps

This shows information about your running containers:

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                  NAMES
e1d0ac1dcb21   nginx     "/docker-entrypoint.…"   30 seconds ago   Up 29 seconds   0.0.0.0:8080->80/tcp   nginx-server

Stopping and Removing Containers

To stop a running container:

docker stop nginx-server

To remove a stopped container:

docker rm nginx-server

You've now successfully used Docker images to run containers. This is the fundamental workflow for using Docker in your development and deployment processes.

Summary

Congratulations on completing this Docker image lab. You have learned the essential skills for working with Docker images:

  1. Understanding what Docker images are and how they are organized in registries
  2. Searching for and pulling Docker images from Docker Hub
  3. Managing your local Docker images including listing, inspecting, and removing them
  4. Using Docker images to run containers in different modes

These skills form the foundation of working with Docker and will enable you to leverage containerization in your development workflow. You can now pull any Docker image from public or private registries and use them to run containerized applications.

Next steps for further learning:

  • Creating your own Docker images with Dockerfiles
  • Working with Docker Compose for multi-container applications
  • Using Docker volumes for persistent storage
  • Setting up container networking

Keep exploring the vast ecosystem of Docker images available on Docker Hub to find tools and applications that can accelerate your development process.