Hands-On Docker for Microservices with Python
上QQ阅读APP看书,第一时间看更新

Pushing into a registry

Once we have our image tagged, we can push it to a shared registry so that it's available for other services to use.

It is possible to deploy your own Docker registry, but, unless strictly necessary, it's better to avoid it. There are cloud providers that allow you to create your own registry, either public or private, and even in your own private cloud network. If you want to make your image available, the best alternative is Docker Hub, as it's the standard and it will be most accessible. In this chapter, we will create one here, but we'll explore other options later in the book.

It's worth saying it again: maintaining your own Docker registry is much more expensive than using a provider one. Commercial prices for registries, unless you require a lot of repos will be in the range of tens of dollars per month, and there are options from well-known cloud providers such as AWS, Azure, and Google Cloud.

Unless you really need to, avoid using your own registry.

We will create a new repo in the Docker Hub registry. You can create a private repo for free, and as many public ones as you want. You need to create a new user, which was probably the case when downloading Docker.

A repo, in Docker terms, is a set of images with different tags; for example, all the tags of thoughts-backend. This is different from the registry, which is a server that contains several repos.

In more informal terms, it's common to refer to registries as repos and to repos as images, though, speaking purely, an image is unique and may be a tag (or not).

Then, you can create a new repo as follows:

Once the repo is created, we need to tag our image accordingly. This means that it should include the username in Docker Hub to identify the repo. An alternative is to name the image directly with the username included:

$ docker tag thoughts-backend:latest jaimebuelta/thoughts-backend:latest

To be able to access the repo, we need to log into Docker with our username and password in Docker Hub:

$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: jaimebuelta
Password:
Login Succeeded

Once logged in, you can push your image:

$ docker push jaimebuelta/thoughts-backend:latest
The push refers to repository [docker.io/jaimebuelta/thoughts-backend]
1ebb4000a299: Pushed
669047e32cec: Pushed
6f7246363f55: Pushed
ac1d27280799: Pushed
c43bb774a4bb: Pushed
992e49acee35: Pushed
11c1b6dd59b3: Pushed
7113f6aae2a4: Pushed
5275897866cf: Pushed
bcf2f368fe23: Mounted from library/alpine
latest: digest: sha256:f1463646b5a8dec3531842354d643f3d5d62a15cc658ac4a2bdbc2ecaf6bb145 size: 2404

You can now share the image and pull it from anywhere, given that the local Docker is properly logged. When we deploy a production cluster, we need to be sure that the Docker server executing it is capable of accessing the registry and that it's properly logged.