Tagging and pushing images
- Version
- 29
- Review
- Draft
Summary#
docker tag adds another name to a local image. docker push uploads it to the registry named in that reference. A reference without a registry host points to Docker Hub.
Syntax#
docker tag SOURCE[:TAG] REGISTRY_HOST[:PORT]/NAMESPACE/REPOSITORY[:TAG]
docker login REGISTRY_HOST[:PORT]
docker push REGISTRY_HOST[:PORT]/NAMESPACE/REPOSITORY:TAG
docker push --all-tags REGISTRY_HOST[:PORT]/NAMESPACE/REPOSITORY
Example#
Use a disposable local Docker daemon with a local registry:3 image. The temporary registry uses a loopback port derived from the shell process ID. If it is busy, the example stops. Discard the daemon's storage afterward to clear the build cache.
(
set -eu
id="dlr-docker-commands-push-$$"
repo="dlr-docker-commands-push/app"
dir=$(mktemp -d)
port=$((20000 + $$ % 20000))
ref=
cleanup() {
docker rm -f "$id" "$id-app" >/dev/null 2>&1 || true
docker image rm "$id:local" ${ref:+"$ref"} >/dev/null 2>&1 || true
rm -rf -- "$dir"
}
trap cleanup EXIT
docker run --pull never --detach --name "$id" --publish "127.0.0.1:$port:5000" \
--tmpfs /var/lib/registry registry:3 >/dev/null
ref="127.0.0.1:$port/$repo:1.0"
tries=0
until docker exec "$id" wget -qO /dev/null http://127.0.0.1:5000/v2/ 2>/dev/null; do
tries=$((tries + 1))
[ "$tries" -lt 30 ]
sleep 1
done
printf 'FROM alpine:3\nLABEL dlr-docker-commands-push=%s\nCMD ["echo", "pulled back"]\n' \
"$$" > "$dir/Dockerfile"
docker build --quiet --tag "$id:local" "$dir" >/dev/null
docker tag "$id:local" "$ref"
docker push --quiet "$ref" >/dev/null
docker image rm "$id:local" "$ref" >/dev/null
docker pull --quiet "$ref" >/dev/null
docker run --pull never --rm --name "$id-app" "$ref"
docker exec "$id" wget -qO- "http://127.0.0.1:5000/v2/$repo/tags/list"
)
Expected output#
pulled back
{"name":"dlr-docker-commands-push/app","tags":["1.0"]}
Common mistakes#
docker push myapp:1.0 targets Docker Hub's library namespace. Tag the image with your registry host and namespace first. Fix certificate trust for a remote registry before pushing.
Version applicability#
Checked with Docker Engine 29. A Docker-assigned port worked on Linux, while Docker Desktop for macOS needed an explicit port in testing. The Engine currently lets registries on 127.0.0.0/8 use plain HTTP by default.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0