AWS Logo
Menu
Pull and push docker images from Harbor

Pull and push docker images from Harbor

This tutorial is if you're pulling and pushing back those docker images to Harbor.

Published Feb 10, 2025

Step 1: Back Up All Docker Images from Harbor

We will pull all images from the current Harbor instance before destroying it.

1.1 Authenticate with Harbor API (if needed)

Since you don’t have .netrc, we need to configure it for authentication.
Create the .netrc file:
nano ~/.netrc
Add the following lines (replace with actual values):
Save and exit (Ctrl + X, then Y, then Enter).
Set correct permissions :chmod 600 ~/.netrc
Now, the script can authenticate with Harbor API.
  • .netrc should be created on the machine where you will run the backup and restore scripts.
  • After deploying the new Harbor, make sure the Harbor URL remains the same, or update the image push script.
  • Once you create the .netrc file, it will be automatically used by curl for authentication. You don’t need to "run" it manually.
However, to verify that it is working, you can test it using curl:
curl -n "https://<harbor-url>/api/v2.0/projects"

1.2 Run the Script to Get Image List

Run the script to get all image repositories and tags:
./that_script.sh > docker_images
#if permission errror
chmod +x that_script.sh
This will save all image URLs into docker_images.
GOT THESE ERROR?
  1. Cannot connect to the Docker daemon at unix:///Users/xxx/.docker/run/docker.sock. Is the docker daemon running → Put the docker software on.
  2. Error response from daemon: unauthorized: unauthorized to access repository: xxx/xxx/chinstrap, action: pull → docker login <your-harbor-url>
  3. Check Harbor Permissions in the repo that pull is allowed.

1.3 Pull All Docker Images

Run the following command to pull each image to your local Docker:
for r in $(cat docker_images) ; do docker pull $r ; done
This ensures that all images are stored locally before we proceed.

Step 2: Destroy Old Harbor

You mentioned that Terraform is ready.
Once all images are backed up, you can destroy the old Harbor:
#try remove harbor code and apply change. ONLY IF CONFIDENT.
terraform state list | grep harbor
terraform destroy -target=helm_release.harbor -auto-approve
terraform destroy -target=helm_release.harbor_postgresql_ha -auto-approve
terraform destroy -target=helm_release.harbor_redis -auto-approve
#Then Terraform is managing the namespace, and you can delete it with:
terraform destroy -target=kubernetes_namespace.harbor -auto-approv

Step 3: Push those docker images to new Harbor:

To deploy the Docker images back to the new Harbor, follow these steps:

1️⃣ Verify the New Harbor is Running

Before pushing images, ensure the new Harbor instance is up and running:
#without HHTPS
curl -u <username>:<password> "https://<new-harborurl>/api/v2.0/projects"
If this returns a list of projects, your new Harbor is ready.

2️⃣ Authenticate Docker with the New Harbor

Log in to the new Harbor registry:
docker login <new-harbor-url>
Enter your username and password when prompted.

3: Retag and Images for the New Harbor

Since the Harbor URL has changed, you need to retag each image before pushing:

4:Push Images to the New Harbor

Once tagged, push all images:

5: Verify Images in the New Harbor

Check if images were successfully pushed via the UI or use the API:
  • you can also check in the harbor console.
  • if project error you might needs to create a project in the Harbor console.
curl -u <username>:<password> "https://<new-harborurl>/api/v2.0/projects"

 

Comments