אחסון קובצי אימג' של קונטיינרים של Docker ב-Artifact Registry

בעזרת Artifact Registry תוכלו לנהל חבילות פרטיות וקובצי אימג' לקונטיינרים של Docker, במקום מרכזי אחד.

במדריך למתחילים הזה נסביר איך:

  • יצירת מאגר Docker פרטי ב-Artifact Registry
  • מגדירים אימות
  • העלאת תמונה למאגר
  • שליפת התמונה מהמאגר

לחצו על תראו לי איך כדי לקרוא הסבר מפורט על המשימה ישירות במסוף Google Cloud :

תראו לי איך


לפני שמתחילים

  1. נכנסים לחשבון Google Cloud . אם אתם משתמשים חדשים ב- Google Cloud, צרו חשבון כדי שתוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Artifact Registry API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. Make sure that you have the following role or roles on the project: Artifact Registry Administrator

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. Click Grant access.
    4. In the New principals field, enter your user identifier. This is typically the email address for a Google Account.

    5. Click Select a role, then search for the role.
    6. To grant additional roles, click Add another role and add each additional role.
    7. Click Save.
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  7. Verify that billing is enabled for your Google Cloud project.

  8. Enable the Artifact Registry API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  9. Make sure that you have the following role or roles on the project: Artifact Registry Administrator

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. Click Grant access.
    4. In the New principals field, enter your user identifier. This is typically the email address for a Google Account.

    5. Click Select a role, then search for the role.
    6. To grant additional roles, click Add another role and add each additional role.
    7. Click Save.

בחירת מעטפת

כדי להשלים את המדריך למתחילים הזה, משתמשים ב-Cloud Shell או במעטפת המקומית.

Cloud Shell
Cloud Shell היא סביבת מעטפת לניהול משאבים שמתארחים ב- Google Cloud. הוא מגיע עם Docker ו-Google Cloud CLI, ממשק שורת הפקודה הראשי של Google Cloud.
מעטפת מקומית
אם אתם מעדיפים להשתמש במעטפת המקומית, עליכם להתקין את Docker ואת ה-CLI של gcloud בסביבה שלכם.

הפעלת Cloud Shell

כדי להפעיל את Cloud Shell:

  1. עוברים אל Google Cloud המסוף.

    Google Cloud console

  2. לוחצים על הלחצן Activate Cloud Shell (הפעלת Cloud Shell):  .

בחלק התחתון של המסוף ייפתח סשן של Cloud Shell בתוך מסגרת. משתמשים בשורת הפקודות הזו כדי להריץ פקודות gcloud.

הגדרת מעטפת מקומית

כדי להתקין את ה-CLI של gcloud ואת Docker, מבצעים את השלבים הבאים:

  1. מתקינים את ה-CLI של gcloud. כדי לעדכן התקנה קיימת, מריצים את הפקודה gcloud components update.

  2. אם Docker עדיין לא מותקן, מתקינים אותו.

  3. כדי לבצע פעולות במאגרי מידע, ל-Docker נדרשת גישה עם הרשאות. ב-Linux או ב-Windows, מוסיפים את המשתמש שמשמש להפעלת פקודות Docker לקבוצת האבטחה של Docker. אין צורך לבצע את השלב הזה ב-macOS כי Docker Desktop פועל במכונה וירטואלית כמשתמש Root.

    Linux

    קבוצת האבטחה של Docker נקראת docker. כדי להוסיף את שם המשתמש, מריצים את הפקודה הבאה:

    sudo usermod -a -G docker ${USER}
    

    Windows

    קבוצת האבטחה של Docker נקראת docker-users. כדי להוסיף משתמש משורת הפקודה של האדמין, מריצים את הפקודה הבאה:

    net localgroup docker-users DOMAIN\USERNAME /add
    

    כאשר:

    • DOMAIN הוא הדומיין של Windows.
    • USERNAME הוא שם המשתמש שלכם.
  4. כדי שהשינויים במינוי לקבוצה ייכנסו לתוקף, צריך לצאת מהחשבון ולהיכנס אליו שוב. אם אתם משתמשים במכונה וירטואלית, יכול להיות שתצטרכו להפעיל אותה מחדש כדי שהשינויים בחברות ייכנסו לתוקף.

  5. כדי לוודא ש-Docker פועל, מריצים את פקודת Docker הבאה, שמחזירה את השעה והתאריך הנוכחיים:

    docker run --rm busybox date
    

    הדגל --rm מוחק את מופע הקונטיינר ביציאה.

יצירת מאגר Docker

יוצרים מאגר Docker כדי לאחסן את תמונת הדוגמה למדריך הזה למתחילים.

המסוף

  1. פותחים את הדף Repositories במסוף Google Cloud .

    פתיחת הדף Repositories

  2. לוחצים על הוספה יצירת מאגר.

  3. מציינים את quickstart-docker-repo כשם המאגר.

  4. בוחרים באפשרות Docker כפורמט ובאפשרות Standard כמצב.

  5. בקטע Location Type, בוחרים באפשרות Region ואז בוחרים את המיקום us-west1.

  6. לוחצים על יצירה.

המאגר נוסף לרשימת המאגרים.

gcloud

  1. מריצים את הפקודה הבאה כדי ליצור מאגר Docker חדש בשם quickstart-docker-repo במיקום us-west1 עם התיאור 'docker repository'.

    gcloud artifacts repositories create quickstart-docker-repo --repository-format=docker \
        --location=us-west1 --description="Docker repository" \
        --project=PROJECT
    

    כאשר PROJECT הוא מזהה הפרויקט. Google Cloud

  2. מריצים את הפקודה הבאה כדי לוודא שהמאגר נוצר.

    gcloud artifacts repositories list \
        --project=PROJECT
    

כדי לקבל מידע נוסף על פקודות Artifact Registry, מריצים את הפקודה gcloud artifacts.

הגדרת אימות

כדי לדחוף או למשוך תמונות, צריך להגדיר את Docker כך שישתמש ב-Google Cloud CLI כדי לאמת בקשות ל-Artifact Registry.

כדי להגדיר אימות למאגרי Docker באזור us-west1, מריצים את הפקודה הבאה:

gcloud auth configure-docker us-west1-docker.pkg.dev

הפקודה מעדכנת את ההגדרה של Docker. מעכשיו אפשר להתחבר ל-Artifact Registry בפרויקט Google Cloud כדי לשלוח תמונות ולמשוך אותן.

מידע על שיטות אימות אחרות זמין במאמר בנושא שיטות אימות.

איך מקבלים תמונה לדחיפה

במדריך למתחילים הזה, תדחפו תמונה לדוגמה בשם hello-app.

מריצים את הפקודה הבאה כדי למשוך את גרסה 1.0 של התמונה.

   docker pull us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0

נתיבי תמונות ב-Artifact Registry כוללים כמה חלקים. בדוגמה הזו של תמונה:

  • us-docker.pkg.dev הוא שם המארח של קובצי אימג' של קונטיינרים שמאוחסנים במאגרי Docker ב-Artifact Registry, שכולל את מיקום המאגר (us).
  • google-samples הוא מזהה הפרויקט.
  • containers הוא מזהה המאגר.
  • /gke/hello-app הוא הנתיב לתמונה במאגר containers.

הוספת התמונה למאגר

לפני שמעבירים בדחיפה את קובץ האימג' של Docker ל-Artifact Registry, צריך לתייג אותו עם שם המאגר.

תיוג התמונה עם שם מאגר

תיוג קובץ אימג' של Docker עם שם מאגר מגדיר את הפקודה docker push כך שתדחוף את התמונה למיקום ספציפי. במדריך למתחילים הזה, מיקום המארח הוא us-west1-docker.pkg.dev.

מריצים את הפקודה הבאה כדי לתייג את התמונה כ-quickstart-image:tag1:

docker tag us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0 \
us-west1-docker.pkg.dev/PROJECT/quickstart-docker-repo/quickstart-image:tag1

כאשר:

  • us-west1 הוא המיקום של המאגר.
  • us-west1-docker.pkg.dev הוא שם המארח של מאגר Docker שיצרתם.
  • PROJECT הוא מזהה הפרויקט ב- Google Cloud . אם מזהה הפרויקט מכיל נקודתיים (:), כדאי לעיין במאמר בנושא פרויקטים בהיקף הדומיין.
  • quickstart-docker-repo הוא המזהה של המאגר שיצרתם.
  • quickstart-image הוא שם התמונה שרוצים להשתמש בה במאגר. שם התמונה יכול להיות שונה משם התמונה המקומית. במדריך למתחילים הזה, התמונה תאוחסן ישירות מתחת למזהה המאגר quickstart-docker-repo.
  • tag1 הוא תג שמוסיפים לקובץ האימג' של Docker. אם לא ציינתם תג, Docker יחיל את תג ברירת המחדל latest.

עכשיו אפשר להעלות את התמונה למאגר שיצרתם.

העברת האימג' בדחיפה ל-Artifact Registry

אחרי שמגדירים אימות ומתייגים את התמונה המקומית, אפשר להעלות את התמונה למאגר שיצרתם.

כדי לדחוף את קובץ האימג' של Docker, מריצים את הפקודה הבאה:

docker push us-west1-docker.pkg.dev/PROJECT/quickstart-docker-repo/quickstart-image:tag1
מחליפים את PROJECT ב Google Cloud מזהה הפרויקט. אם מזהה הפרויקט מכיל נקודתיים (:), כדאי לעיין במאמר בנושא פרויקטים בהיקף הדומיין. ‫

שליפת התמונה מ-Artifact Registry

כדי למשוך את התמונה מ-Artifact Registry למחשב המקומי, מריצים את הפקודה הבאה:

docker pull us-west1-docker.pkg.dev/PROJECT/quickstart-docker-repo/quickstart-image:tag1
מחליפים את PROJECT ב Google Cloud מזהה הפרויקט. אם מזהה הפרויקט מכיל נקודתיים (:), כדאי לעיין במאמר בנושא פרויקטים בהיקף הדומיין. ‫
latest: Pulling from [PROJECT-ID]/quickstart-image:tag1
Digest: sha256:70c42...
Status: Image is up to date for us-west1-docker.pkg.dev/PROJECT/quickstart-docker-repo/quickstart-image:tag1

הסרת המשאבים

כדי לא לצבור חיובים לחשבון Google Cloud על המשאבים שבהם השתמשתם בדף הזה, פועלים לפי השלבים הבאים:

לפני שמסירים את המאגר, צריך לוודא שכל התמונות שרוצים לשמור זמינות במיקום אחר.

כדי למחוק את המאגר:

המסוף

  1. פותחים את הדף Repositories במסוף Google Cloud .

    פתיחת הדף Repositories

  2. ברשימת המאגרים, בוחרים במאגר quickstart-docker-repo.

  3. לוחצים על Delete.

gcloud

כדי למחוק את מאגר quickstart-docker-repo, מריצים את הפקודה הבאה:

gcloud artifacts repositories delete quickstart-docker-repo --location=us-west1

המאמרים הבאים