Docker イメージの検索方法

DockerDockerBeginner
今すぐ練習

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

Introduction

Docker has become an essential tool for modern software development, allowing developers to package and deploy applications in a consistent and reliable manner. Docker images serve as the building blocks for containerized applications, providing pre-configured environments that can be easily shared and deployed.

In this hands-on lab, you will learn how to search for Docker images using the Docker command-line interface, pull these images to your local machine, and run containers based on those images. By the end of this tutorial, you will have the skills to find and use Docker images that meet your specific project requirements.

Checking Docker Installation

Before we start searching for Docker images, let us first verify that Docker is properly installed and running on your system.

Verifying Docker Installation

Open a terminal window and run the following command to check if Docker is installed:

docker --version

You should see output similar to this:

Docker version 20.10.21, build baeda1f

This confirms that Docker is installed on your system. The version number might be different depending on your installation.

Checking Docker Service Status

Let us also check if the Docker service is running properly by executing:

sudo systemctl status docker

You should see output indicating that the Docker service is active and running:

● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since...

If for any reason Docker is not running, you can start it with:

sudo systemctl start docker

Understanding Docker Images

A Docker image is a read-only template containing a set of instructions for creating a container. You can think of an image as a snapshot or blueprint of an application along with its environment, dependencies, and configuration.

Docker images are:

  • Layered: Built using a series of layers that can be shared between images
  • Read-only: Cannot be modified once created
  • Named and tagged: Identified by a name and optional tag (e.g., ubuntu:22.04)

In the next step, we will learn how to search for these images.

Searching for Docker Images

Docker Hub is the official public registry for Docker images. It contains thousands of images, including official images maintained by software vendors and community images created by Docker users.

To search for Docker images, use the docker search command followed by a search term. Let us search for Ubuntu images:

docker search ubuntu

You will see output similar to this:

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

The output shows:

  • NAME: The image name
  • DESCRIPTION: A brief description of the image
  • STARS: Number of users who have starred the image (popularity indicator)
  • OFFICIAL: Tag indicating an official image maintained by the software vendor
  • AUTOMATED: Tag indicating an image built by an automated process

You can filter the search results using the --filter option. For example, to show only official images:

docker search --filter=is-official=true ubuntu

To find images with at least 100 stars (popular images):

docker search --filter=stars=100 ubuntu

You can also combine multiple filters:

docker search --filter=is-official=true --filter=stars=100 nginx

This command searches for official Nginx images with at least 100 stars.

Finding Specific Versions

The docker search command does not show image tags (versions), but you can find them by:

  1. Visiting Docker Hub website (hub.docker.com)
  2. Using the docker image inspect command (after pulling the image)

For example, to see all available Ubuntu versions, visit:
https://hub.docker.com/_/ubuntu?tab=tags

Choosing the Right Image

When selecting a Docker image, consider the following:

  1. Official images: Prefer official images as they are maintained by the software vendor
  2. Popular images: Higher star count indicates more users and potentially better maintenance
  3. Recent updates: Check when the image was last updated
  4. Documentation: Look for images with good documentation
  5. Size: Consider the image size, as smaller images download faster

Downloading Docker Images

Once you've found an image you want to use, you can download it to your local machine with the docker pull command.

Pulling an Image

Let us pull the official Ubuntu image:

docker pull ubuntu

You will see output similar to:

Using default tag: latest
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

By default, Docker pulls the latest tag. To pull a specific version, add a tag:

docker pull ubuntu:20.04

You will see output showing the download progress for that specific version.

Listing Downloaded Images

To see all the images you have downloaded, use:

docker images

Or the newer command format:

docker image ls

Both commands produce the same output:

REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
ubuntu       latest    1318b700e415   4 weeks ago   72.8MB
ubuntu       20.04     1318b700e415   4 weeks ago   72.8MB

Understanding Image Tags

Docker images are identified by tags. The tag format is typically:

repository:tag

Where:

  • repository: Usually in the format username/image-name or just image-name for official images
  • tag: Specifies the version (e.g., latest, 20.04, 3.9-alpine)

If you omit the tag, Docker assumes you want the latest tag.

Getting Image Details

To view detailed information about an image, use:

docker image inspect ubuntu:latest

This command displays a JSON object with all image metadata, including layers, configuration, and environment variables.

To see the history of image layers:

docker history ubuntu:latest

This shows how the image was built, layer by layer.

Running Containers from Images

Now that you have downloaded some Docker images, let us learn how to create and run containers based on these images.

Running a Basic Container

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

docker run ubuntu echo "Hello from Docker"

This command:

  1. Creates a new container based on the Ubuntu image
  2. Runs the command echo "Hello from Docker" inside the container
  3. Exits after the command completes

You should see the output:

Hello from Docker

Running an Interactive Container

To interact with a container, use the -it flags (interactive terminal):

docker run -it ubuntu bash

This starts a bash shell inside the container. You are now effectively "inside" the container and can run commands.

Try a few commands:

ls
cat /etc/os-release

To exit the container, type:

exit

Listing Running Containers

To see all running containers:

docker ps

Since our containers exited immediately after completion, you might not see any output. To see all containers, including stopped ones:

docker ps -a

This shows all containers, their status, and when they were created/exited.

Container Lifecycle

Containers have a lifecycle:

  1. Created: Container is created but not started
  2. Running: Container is currently executing
  3. Paused: Container execution is paused
  4. Stopped: Container has exited but still exists
  5. Removed: Container is deleted

You can remove a stopped container with:

docker rm <container_id>

Replace <container_id> with the ID shown in the docker ps -a output.

To automatically remove a container after it exits, use the --rm flag:

docker run --rm ubuntu echo "This container will be removed after execution"

Running a Web Server Container

Let us try something more practical by running an Nginx web server:

docker pull nginx:alpine

This pulls a lightweight Nginx image based on Alpine Linux.

Now, run a container that maps port 8080 on your host to port 80 in the container:

docker run -d -p 8080:80 --name my-nginx nginx:alpine

This command:

  • -d: Runs the container in detached mode (background)
  • -p 8080:80: Maps port 8080 on your host to port 80 in the container
  • --name my-nginx: Names the container "my-nginx"

Now you can access the Nginx welcome page by navigating to http://localhost:8080 in a web browser, or using curl:

curl http://localhost:8080

You should see the HTML content of the Nginx welcome page.

To stop and remove this container:

docker stop my-nginx
docker rm my-nginx

Summary

Congratulations on completing this Docker image search and usage lab. Throughout this tutorial, you have:

  1. Verified your Docker installation and understood the concept of Docker images
  2. Learned how to search for Docker images using the docker search command
  3. Downloaded Docker images to your local machine using docker pull
  4. Listed and inspected your local Docker images
  5. Created and run containers from these images using various docker run options
  6. Understood the container lifecycle and how to manage containers

These skills form the foundation for working with Docker in your development projects. You can now search for, download, and use Docker images to create containerized environments for your applications.

Next steps in your Docker journey could include:

  • Learning how to create your own Docker images with Dockerfiles
  • Understanding Docker networking and data persistence
  • Exploring Docker Compose for multi-container applications
  • Diving into container orchestration with Kubernetes

Keep experimenting with different Docker images to discover the vast ecosystem of pre-built containers available to accelerate your development workflow.