DEV Community

DevCorner2
DevCorner2

Posted on

πŸš€ How to Build, Tag, and Push Docker Images to a Private Repository

In modern software delivery, containerization has become the de-facto standard for packaging and deploying applications. While Docker Hub is widely used, many organizations rely on private registries (Harbor, Nexus, Artifactory, AWS ECR, Azure ACR, Google GCR, etc.) to securely host and distribute container images.

This blog walks you step-by-step through the process of creating a project locally, building a Docker image, and pushing it directly to a private Docker repository.


πŸ› οΈ Prerequisites

Before you begin, ensure you have:

  • Docker installed on your local machine.
  • Access to a private registry (credentials + URL).
  • A project to containerize (we’ll use a Java Spring Boot example here).

πŸ“Œ Step 1: Create Your Project

Let’s assume you have a Spring Boot application. Build it locally:

mvn clean package -DskipTests
Enter fullscreen mode Exit fullscreen mode

This will generate an executable JAR inside the target/ folder.


πŸ“Œ Step 2: Write a Dockerfile

Your Dockerfile defines how the container image will be built.

Example:

FROM eclipse-temurin:17-jdk-alpine
WORKDIR /app
COPY target/myapp.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
Enter fullscreen mode Exit fullscreen mode

Here’s what’s happening:

  • Base image: Uses a lightweight JDK 17 image.
  • Workdir: Sets /app as the working directory.
  • Copy: Copies your JAR into the container.
  • Entrypoint: Defines the startup command.

πŸ“Œ Step 3: Build the Docker Image

Run the following in the project root directory:

docker build -t myapp:1.0 .
Enter fullscreen mode Exit fullscreen mode

This builds an image named myapp with the tag 1.0.


πŸ“Œ Step 4: Tag the Image for Your Private Registry

You need to reference your private registry when tagging the image.

For example, if your registry is hosted at myregistry.example.com:5000:

docker tag myapp:1.0 myregistry.example.com:5000/myteam/myapp:1.0
Enter fullscreen mode Exit fullscreen mode

Now the image is ready to be pushed.


πŸ“Œ Step 5: Authenticate with the Registry

Login using Docker CLI:

docker login myregistry.example.com:5000
Enter fullscreen mode Exit fullscreen mode

You will be prompted for a username and password (or token).


πŸ“Œ Step 6: Push the Image

Push the tagged image to the registry:

docker push myregistry.example.com:5000/myteam/myapp:1.0
Enter fullscreen mode Exit fullscreen mode

Once uploaded, your team can pull and use this image anywhere.


πŸ“Œ Step 7: Verify the Push

Confirm the image is available in the registry:

docker pull myregistry.example.com:5000/myteam/myapp:1.0
Enter fullscreen mode Exit fullscreen mode

If successful, your private registry is now serving your custom image πŸŽ‰.


⚑ Pro Tips

  • Versioning: Always use semantic version tags (1.0.0, 1.1.0) for production images. Avoid relying only on latest.
  • Automation: Integrate this workflow into CI/CD pipelines (GitHub Actions, GitLab CI, Jenkins).
  • Security: Configure imagePullSecrets in Kubernetes to authenticate private registries.
  • Efficiency: Use multi-stage Docker builds to reduce image size.

βœ… Conclusion

You’ve successfully:

  1. Built a project locally.
  2. Containerized it with a Dockerfile.
  3. Tagged and pushed the image into a private repository.

This workflow forms the backbone of modern DevOps practices, enabling teams to ship applications faster and more securely.

Top comments (0)