Home DockerHow to Build and Push Docker Images to Docker Hub

How to Build and Push Docker Images to Docker Hub

by Knowledge Base Linux
How to Build and Push Docker — How to Build and Push Docker Images to Docker Hub

This article covers How to Build and Push Docker in detail. You built an image that runs perfectly on your machine — now your team, your CI pipeline and your production servers need it too. The standard way to distribute images is a registry, and the public default is Docker Hub. Learning how to push docker image to docker hub correctly — with proper tagging, secure login and sensible versioning — saves you from the classic “works on my laptop” deployment mess. This tutorial covers the full workflow step by step. You should already have an image built locally; if not, our Docker installation and first container guide gets you started.

Prerequisites – How to Build and Push Docker

  • A free Docker Hub account at hub.docker.com
  • Docker Engine installed and running locally
  • An image you want to publish, for example myapp built from your project
Registry harbor illustration for push docker image to Docker Hub workflow

Step 1: Understand Image Names and Tags – How to Build and Push Docker

An image reference has the form namespace/repository:tag. On Docker Hub, the namespace is your username. A local image named just myapp:latest belongs to the implicit library namespace, so Docker will refuse to push it under your account until you tag it with your username:

# Syntax
docker tag SOURCE_IMAGE USERNAME/REPOSITORY:TAG

# Example
docker tag myapp:latest johndoe/myapp:1.0.0

Tags are mutable labels, not versions carved in stone — latest simply means “the default tag”, nothing more. For anything you deploy, always push a specific version tag (such as 1.0.0 or a git commit SHA) in addition to latest, so you can roll back to an exact image later.

Step 2: Log In Securely with an Access Token

Do not use your account password for CLI logins. In Docker Hub, go to Account Settings → Security → New Access Token, create a token, and use it as the password:

docker login -u johndoe
# Paste the access token when prompted for a password

On a headless server, avoid storing credentials unencrypted. Docker saves the login in ~/.docker/config.json — readable by anyone with your account — so on shared machines configure a credential helper, and always docker logout when you finish. Treat the access token like a password: it grants push access to all your repositories.

Step 3: Push the Image

docker push johndoe/myapp:1.0.0
docker push johndoe/myapp:latest

Docker uploads only the layers that are not already present on Docker Hub, which is why pushing an updated image that shares its base layers with the previous one is fast. When the push completes, open https://hub.docker.com/r/johndoe/myapp — your repository is now public by default. Free accounts include one private repository; organizations typically upgrade for more.

Step 4: Pull and Run It Anywhere

On any other machine with Docker installed:

docker pull johndoe/myapp:1.0.0
docker run -d -p 8080:80 --name myapp johndoe/myapp:1.0.0

This push–pull loop is the foundation of container deployment pipelines: CI builds and pushes, servers pull and run. Our tutorial on building a CI/CD pipeline with GitHub Actions automates exactly this flow, including login via repository secrets.

Step 5: Manage Tags and Repositories

# List local images
docker images

# Remove a local tag (image data stays if other tags point to it)
docker rmi johndoe/myapp:latest

# Inspect what will be pushed before pushing
docker image inspect johndoe/myapp:1.0.0

Use semantic versions for releases (1.0, 1.0.1) and meaningful channel tags for environments (stable, beta) if your process needs them. From the Docker Hub web UI you can add a description and README to the repository — a small effort that saves your future self and your teammates a lot of guessing.

Pin Images by Digest for Reproducible Deploys

Tags can be overwritten — pushing a new 1.0.0 changes what that tag delivers, which quietly breaks reproducibility. For deployments where exactness matters, reference images by digest instead. After pushing, Docker prints the digest (sha256:...), and docker pull johndoe/myapp@sha256:<digest> always returns the identical image, even if the tag is later moved. Many teams deploy by tag in development and by digest in production, combining convenience with a guaranteed audit trail of exactly what ran where.

Troubleshooting

denied: requested access to the resource is denied. The image name does not start with your Docker Hub username, or you are not logged in. Re-tag with the correct namespace and run docker login again.

unauthorized: incorrect username or password. You likely typed your account password instead of an access token, or the token was revoked. Create a fresh token and retry.

Push is extremely slow. Your image is probably huge. Slim it down first — smaller images push dramatically faster and pull faster on every server.

Repository not found when pulling. Private repositories require docker login on the pulling machine too, with an account that has access.

Conclusion

The workflow is simple: build, tag with your namespace and a real version, log in with an access token, and push. Everything after that — CI pipelines, staging servers, rollbacks — builds on these same commands. For full details on image references and the push command, see the official docker image push documentation.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.