9.1 Getting Reacquainted with Docker Hub
Let's get to know Docker Hub a bit better. Just a reminder, it's the main public registry for Docker where users can store, share, and find ready-to-go container images. This platform gives access to millions of containers created by official organizations as well as the community. In this lecture, we'll cover how to effectively search for and download ready-made images from Docker Hub.
Main purposes of Docker Hub
Docker Hub is a cloud service that lets you upload images into a centralized storage and pull them from there. The service acts as a platform where developers can share their images and use solutions created by others. The main features of Docker Hub include:
- Public and private repositories: Allows you to store images either publicly for everyone or restrict access to specific users.
- Automated builds: Integration with version control systems like GitHub to automatically build images every time the code changes.
- Webhooks: Setting up automatic actions that are triggered when images are updated.
9.2 Finding pre-built images
1. Using the Docker Hub web interface
- Go to the Docker Hub website: open Docker Hub in your browser.
- Searching for images: type the name of the image or keywords related to your project in the search bar. For example, to search for an Nginx image, type "nginx" and press Enter.
- Reviewing the results: the search results will display suitable images. Official images are marked as "Official" and are supported by Docker or the respective organizations. Images from other users will have the username or organization mentioned.
- Selecting an image: click on the image you’re interested in to view its description, available tags, usage instructions, and other helpful info.
2. Searching for images via the command line
Docker also lets you search for images directly from the command line using the docker search command.
Command example:
docker search nginx
This command will output a list of images with nginx in their name or description. The output will include the image name, description, star count (rating), official status, and information on automated builds.
Example output:
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 13764 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for... 2135 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM... 819 [OK]
9.3 Pulling Images
After you find the image you need, you can download it to your local machine using the docker pull command.
1. Pulling a specific image
To pull a specific image, use the following syntax:
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Where:
- NAME: the name of the image.
- TAG: (optional) the tag of the image. By default, the
latesttag is used if no other is specified. - @DIGEST: (optional) the SHA256 identifier of the image.
Example of pulling an image:
This example will pull the latest nginx image with the latest tag.
docker pull nginx:latest
2. Pulling an image with a specific tag
If you need a specific image tag, specify it after the colon. This example will pull the Ubuntu image version 20.04.
docker pull ubuntu:20.04
3. Checking downloaded images
After pulling an image, you can check which images are stored on your local machine using the docker images command.
Example command:
The command output will display a list of all downloaded images with details like repository, tag, image ID, creation date, and size.
docker images
9.4 Examples of Using Prebuilt Images
1. Running the Nginx Web Server
After downloading the Nginx image, you can quickly launch a container based on it.
Example:
This example starts an Nginx container in detached mode and maps the container's port 80 to port 8080 on the host machine. Now you can access the Nginx web server by opening your browser and entering http://localhost:8080.
docker run -d -p 8080:80 nginx
2. Running a MySQL Database
Docker Hub provides prebuilt database images like MySQL, making it easy to deploy databases for development and testing.
Example:
This example launches a MySQL container, sets a password for the root user, and maps the container's port 3306 to port 3306 on the host machine.
docker run -d -p 3306:3306 --name my_mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql:latest
3. Using the Redis Image for Caching
Redis is a popular caching system, and Docker Hub provides a prebuilt Redis image that can be quickly deployed.
Example:
This example starts a Redis container and maps the container's port 6379 to port 6379 on the host machine.
docker run -d -p 6379:6379 --name my_redis redis:latest
GO TO FULL VERSION