Sub topic 9
Choosing public or private visibility
This chapter assumes you can already push an image with docker push, and that you have a Docker Hub account and a working docker login. It covers how visibility controls who can pull your image, how to choose between the two settings, and what visibility does not protect.
What visibility controls
Every Docker Hub repository is either public or private. Visibility applies to the whole repository, so every tag in it shares the same setting. You can't make one tag public and another private.
| Question | Public repository | Private repository |
|---|---|---|
| Who can pull? | Anyone, with or without an account | Only users you grant access to, after logging in |
| Who can push? | Only you and users you grant write access | Only you and users you grant write access |
| Appears in Docker Hub search? | Yes | No |
| Pull without `docker login`? | Yes | No |
| Typical use | Open-source tools, base images, tutorials | Internal services, unreleased products, customer-specific builds |
Visibility never changes who can push. Pushing always requires authentication and write permission, whatever the repository's visibility.
Visibility also never changes the image name. A private repository is still named YOUR_USERNAME/myapp:1.0. Only the permission check on the registry side differs.
Plan limits
Docker Hub's free plan allows unlimited public repositories but only a small number of private ones (at the time of writing, one). Paid plans raise that limit. Plan details change, so check Docker's pricing page before you design around a specific number.
This limit often decides the question for individual developers. If you have several private images and one private slot, you either upgrade, move some images to another registry, or accept that some will be public.
Setting visibility
You can set visibility in three places.
- At creation. In the Docker Hub web interface, choose Create repository, enter a name, and select Public or Private.
- By pushing to a repository that doesn't exist yet. Docker Hub creates the repository for you. Its visibility comes from your account's default repository privacy setting, which you can find in your account settings. Check that setting before your first push, because a surprise public repository is the more costly mistake.
- After creation. Open the repository, go to Settings, and change the visibility. The change applies at once to every tag.
Creating the repository in the web interface before the first push removes the guesswork, since you pick the visibility yourself.
Pulling from a private repository
An anonymous pull from a private repository fails. Try it by logging out and pulling:
docker logout
docker pull YOUR_USERNAME/private-app:1.0The daemon reports:
Error response from daemon: pull access denied for YOUR_USERNAME/private-app, repository does not exist or may require 'docker login': denied: requested access to the resource is deniedThe message says "does not exist" even when the repository exists. Docker Hub gives the same answer for a missing repository and a private one, so outsiders can't use the error to discover which private repositories exist. When you see this error, check for a typo first. Then check whether you are logged in as a user with access.
Log in and the same command succeeds:
docker login -u YOUR_USERNAME
docker pull YOUR_USERNAME/private-app:1.0Being logged in is not enough by itself. The account must also have permission on that repository. A valid login to the wrong account produces the same "denied" error.
Giving other people access
For a repository under a personal namespace, you add collaborators by username in the repository's settings. This works for small groups, but it doesn't scale because you manage each person on each repository.
For teams, create a Docker Hub organization and put repositories in the organization's namespace, such as acme/payments-api. Organizations let you create teams and assign each team a permission level on each repository.
| Permission | Allows |
|---|---|
| Read-only | Pull images |
| Read & Write | Pull and push images |
| Admin | Pull, push, and change repository settings, including visibility |
Give each person or team the lowest level that lets them do their job. Most developers only need to pull, so read-only is the usual default.
Machines need credentials too
A private image has to be pullable by the servers that run it, not only by people. Don't put a personal password on a server. Create an access token with read-only scope for that purpose. Use a separate token for each environment, so you can revoke one without breaking the others.
On Kubernetes, store the token as an image pull secret:
kubectl create secret docker-registry regcred \
--docker-server=https://index.docker.io/v1/ \
--docker-username=YOUR_USERNAME \
--docker-password="$DOCKERHUB_READ_TOKEN"Then reference the secret in the pod specification:
apiVersion: v1
kind: Pod
metadata:
name: private-app
spec:
imagePullSecrets:
- name: regcred
containers:
- name: app
image: YOUR_USERNAME/private-app:1.0If a pod is stuck in ImagePullBackOff and the image name is correct, a missing or wrong pull secret is the most likely cause.
Rate limits and public images
Docker Hub limits how many image pulls it allows in a time window. The limits are lower for anonymous users than for logged-in ones. A public image used by a busy CI system or a large cluster can hit those limits, and the pulls then fail with a "toomanyrequests" error.
This affects how you choose visibility. A public image is easy for others to use, but anonymous pulls from shared networks count against a shared allowance. If your own automation pulls your public images, have it log in. Logging in costs nothing and raises the allowance.
What visibility does not protect
Private repositories restrict who can pull. They do not make an image safe to fill with secrets.
- Layers hold everything you put in them. If a Dockerfile copies a
.envfile or runsRUN echo $API_KEY > /keyand a later step deletes the file, the secret still exists in an earlier layer. Anyone who can pull the image can extract it. This is true for public and private images alike. docker historyshows build commands. Command lines, including arguments such as--build-arg, may expose values you meant to hide.- Making a public repository private doesn't recall copies. Anyone who already pulled the image keeps it, and mirrors or caches may keep it as well. If a public image leaked a credential, revoke the credential. Changing the visibility does not fix the leak.
- Visibility is not licensing. A public image can be pulled by anyone. If it bundles software with restrictive licenses, publishing it may breach those terms.
Pass secrets at run time, or use BuildKit secret mounts at build time, which keep the secret out of the final layers.
Choosing between public and private
Ask these questions in order.
- Does the image contain code, data, or configuration you don't want others to read? If so, use private.
- Is the image meant for anyone to use, such as an open-source project or a teaching example? If so, use public.
- Would a mistake in the image contents (an included secret, a license problem) cause serious harm? If so, start private. You can make an image public later after review. Going the other way doesn't undo the exposure.
- Do you have enough private repository slots on your plan? If not, decide whether to upgrade or host that image on another registry.
When unsure, choose private. Making a repository public later takes one click, while a leak can't be undone.
Checking visibility before you share
Before you tell anyone to pull an image, test it the way they will. Log out and pull:
docker logout
docker pull YOUR_USERNAME/myapp:1.0- If the pull succeeds, the repository is public, and anyone can do the same.
- If it fails with "pull access denied", the repository is private or the name is wrong.
This test confirms the visibility you actually have, whatever you meant to set. Run it again after any change to repository settings.
Common mistakes
- Pushing a private image to a repository you thought was private. An auto-created repository follows your account default. Check the repository page after the first push.
- Using a personal password in CI or on a server. Use a scoped access token. You can revoke a token without changing your login.
- Assuming a private repository hides build secrets. It restricts pulls only. Anyone with pull access can read the layers.
- Sharing a read/write token where read-only would do. A leaked write token lets someone replace your image with a malicious one.
Exercise
You log out of Docker and run docker pull YOUR_USERNAME/tools:2.1. The command fails with "pull access denied ... repository does not exist or may require 'docker login'". You know the repository exists. Name two possible causes and one command that helps tell them apart.
Show answer
Either the repository is private and you are not logged in, or the name or tag is mistyped. Run docker login -u YOUR_USERNAME and repeat the pull. If it now succeeds, the repository was private. If it still fails, check the spelling of the namespace, repository, and tag, and check that the logged-in account has permission on the repository. Docker Hub returns the same error for a missing repository and a private one, so the message alone can't tell them apart.
Exercise
A teammate says: "Our image leaked an API key through a Dockerfile COPY, so let's switch the repository from public to private and we're safe." What is wrong with this plan, and what should the team do instead?
Show answer
Switching to private doesn't recall copies that were already pulled, and the key remains readable in the image layers for anyone who still has access. The team should revoke and replace the API key first. They should then rebuild the image without the secret, using run-time environment variables or BuildKit secret mounts, and push the new image. Making the repository private is an optional extra step, not a fix.