Docker Registry:
Think of the Docker registry as the
entire online library where all the books (Docker images) are stored. A
registry is a centralized place that hosts multiple repositories of Docker
images. It allows users to upload (push) or download (pull) Docker images.
- Example: DockerHub, Amazon ECR, or a private
Docker registry you set up are examples of Docker registries. They are
like the library building that holds many collections of books.
Docker Repository:
A Docker repository is like a specific
shelf or section in that library, where all the different versions of a
particular book are stored. A repository holds a set of related Docker images,
usually different versions of the same application.
- Example: If you have an application called "my-app",
the repository would be called my-app. Inside that repository, you might have several versions (images),
such as my-app:1.0, my-app:2.0, etc. These different versions are like
different editions of the same book on that shelf.
In simple terms:
- Docker Registry = Big
library holding many collections (repositories) of images.
- Docker Repository = A
shelf inside the library, containing different versions of a specific
application (images).
Scenario:
Consider the scenario, where
we have a Docker registry set up at myregistry.example.com using a
Docker registry container, and we want to store 25 applications (myapp1 to myapp25).
Let’s see how the concept of
registry, repository, and versions will work in this scenario:
1. Registry:
The registry is myregistry.example.com.
It will act as the central server to host all our Docker images (for all 25
applications). This is where we’ll push (upload) and pull (download) images.
2. Repositories for Each Application:
Each application (from myapp1 to myapp25) will have
its own repository inside the registry. Think of each repository as a
separate directory or space within the registry dedicated to one application.
- Example:
- myapp1 will have its own
repository at myregistry.example.com/myapp1.
- myapp2 will have its own
repository at myregistry.example.com/myapp2.
- Similarly, all other applications up to myapp25 will have their own
repositories.
3. Versioning:
We can maintain different versions
(tags) of each application by tagging the Docker images. Docker uses tags
to differentiate between versions of the same image in a repository.
For each application repository, we’ll push
different versions like this:
- For myapp1, you
might have versions 1.0, 2.0, 3.0, etc.
- Image names: myregistry.example.com/myapp1:1.0, myregistry.example.com/myapp1:2.0, etc.
- For myapp2,
you’ll have similar versioning:
- Image names: myregistry.example.com/myapp2:1.0, myregistry.example.com/myapp2:2.0, etc.
4. Push and Pull Workflow:
Here’s how push and pull will work for
each application:
Pushing an Image:
To push an image to your registry, you first need to tag it with the correct
repository and version (tag) name, then push it to the registry.
For example:
- You
build a Docker image for
myapp1
locally:
docker build -t myapp1 .
- You tag
the image with the registry URL, repository, and version:
docker tag myapp1 myregistry.example.com/myapp1:1.0
- You push
the image to the registry:
docker push myregistry.example.com/myapp1:1.0
We can repeat this process for different versions of myapp1
or for other applications (myapp2
, myapp3
, etc.).
Pulling an Image:
To pull an image from the registry, we’ll specify the registry URL,
repository, and the version we want.
For example:
1. To
pull version 1.0
of myapp1
from the registry:
docker pull myregistry.example.com/myapp1:1.0
2. To
pull version 2.0
of myapp2
:
docker pull myregistry.example.com/myapp2:2.0
Example Push/Pull Flow for Multiple Applications:
1. For
myapp1
, version 1.0
:
docker build -t myapp1 .
docker tag myapp1 myregistry.example.com/myapp1:1.0
docker push myregistry.example.com/myapp1:1.0
2. For
myapp2
, version 2.0
:
docker build -t myapp2 .
docker tag myapp2 myregistry.example.com/myapp2:2.0
docker push myregistry.example.com/myapp2:2.0
3. To
pull a specific version of an app:
docker pull myregistry.example.com/myapp1:1.0
docker pull myregistry.example.com/myapp2:2.0
Maintaining latest version:
In Docker, the latest version of an image is usually
represented by the latest
tag. This is not automatically assigned—we have to manually tag and
push the image with latest
if
we want it to be the default version pulled when no specific version is
provided.
How to Maintain the Latest Version:
1. Tagging
the Latest Version: Each time we release a new version of an
application that we want to be the default (or latest), we will need to tag it
with latest
in addition to its
specific version tag.
2. Pulling
the Latest Version: If we don't specify a version when pulling, Docker
will automatically pull the image tagged as latest
.
Example of Maintaining the Latest Version:
Let's take the example of myapp1
.
Step 1: Build Your Application Image
We have a new version of myapp1
(version 3.0
) and want this to be
the latest.
docker build -t myapp1 .
Step 2: Tag with the Version and latest
Now, tag the image with both the specific version (3.0
) and latest
:
# Tag with version 3.0
docker tag myapp1 myregistry.example.com/myapp1:3.0
# Tag the same image as 'latest'
docker tag myapp1 myregistry.example.com/myapp1:latest
Step 3: Push Both Tags
Push both the versioned and the latest
tags to our registry:
# Push version 3.0
docker push myregistry.example.com/myapp1:3.0
# Push the latest tag
docker push myregistry.example.com/myapp1:latest
Step 4: Pulling the Latest Version
When someone pulls myapp1
without specifying a version, Docker will pull the image tagged as latest
:
# This will pull 'myapp1:latest'
docker pull myregistry.example.com/myapp1
If someone wants to pull a specific version, they can still do so by
specifying the version tag:
# This will pull 'myapp1:3.0'
docker pull myregistry.example.com/myapp1:3.0
Keeping the latest
Tag Up-to-Date:
- Whenever
we push a new version that you want to be the default, tag it as
latest
along with the specific version number. - The
latest
tag will always point to the most recent image that you tagged aslatest
.
Example Push Flow for Keeping latest
Up-to-Date:
Let’s say we’ve already pushed versions 1.0
and 2.0
, and now we’re pushing
version 3.0
:
1. Build
the new version:
docker build -t myapp1 .
2. Tag
it as both version 3.0
and latest
:
docker tag myapp1 myregistry.example.com/myapp1:3.0
docker tag myapp1 myregistry.example.com/myapp1:latest
3. Push
both tags to the registry:
docker push myregistry.example.com/myapp1:3.0
docker push myregistry.example.com/myapp1:latest
No comments:
Post a Comment