Sub topic 1
What Docker Hub Is and How Repositories Work
Prerequisites
This chapter assumes you have the following:
- Docker installed and running on your machine.
- Basic command-line skills.
- Familiarity with building an image from a Dockerfile.
- A free Docker Hub account.
You do not need to push anything yet. This chapter builds the mental model that the later commands rely on.
What a registry is
A container image is a set of files plus metadata. To share one, you store it in a registry. A registry is a server that stores images and serves them over HTTP to Docker clients.
Docker Hub is the default public registry. When you run docker pull alpine without naming a server, Docker contacts Docker Hub. Other registries exist, such as GitHub Container Registry and Amazon ECR. They work the same way, but you must put their hostname in the image name.
Repositories and namespaces
A registry holds many repositories. A repository is a named collection of related images, usually different versions of one application. Each version in a repository is identified by a tag.
Every repository on Docker Hub lives inside a namespace. For a personal account, the namespace is your username. For an organization, it is the organization name. The namespace keeps your myapp separate from anyone else's myapp.
The full path of a repository is namespace/repository. For example, YOUR_USERNAME/myapp is the repository myapp in your personal namespace.
Official images such as alpine and nginx are an exception. They live in a special namespace called library, so alpine is shorthand for docker.io/library/alpine.
How the pieces form an image name
An image reference combines these parts:
| Part | Example | Default if omitted |
|---|---|---|
| Registry host | `docker.io` | `docker.io` (Docker Hub) |
| Namespace | `YOUR_USERNAME` | `library` for official images |
| Repository | `myapp` | none, required |
| Tag | `1.0` | `latest` |
So YOUR_USERNAME/myapp:1.0 means: on Docker Hub, in the namespace YOUR_USERNAME, the repository myapp, the version tagged 1.0. The next chapter covers reading and writing these names in detail.
A repository does not need to exist before your first push. Docker Hub creates it when you push to a path inside a namespace you own. You can also create it ahead of time in the web interface, which lets you set a description and visibility first.
Local images versus registry images
An image on your machine and an image on Docker Hub are separate copies. Building locally does not upload anything. Pushing uploads a copy, and pulling downloads one. The name on your machine is only a label, and it decides where a push goes. An image named myapp:1.0 has no namespace, so Docker cannot push it to your repository until you give it a name that includes your namespace.
Each repository also has a visibility setting, public or private. Public repositories can be pulled by anyone without logging in. Private repositories require authentication. A free account has a limited number of private repositories, and the limit can change, so check the current plan details on Docker Hub.
Common misunderstandings
- A repository is not an image. It is a collection of image versions, identified by tags.
- A tag is not a copy of the data. It is a movable label that points to an image, so a tag like
latestcan point to a different image tomorrow. - Your username is not just a login name. It is also the namespace you push into, so it must match exactly in the image name.
Exercise
You run docker push myapp:1.0 on a machine logged in to Docker Hub as alice. What is wrong with the image name, and what should the repository path look like?
Show answer
The name has no namespace, so Docker treats it as docker.io/library/myapp, a namespace you cannot write to. Tag the image as alice/myapp:1.0 first, so the repository path is alice/myapp.