ข้ามไปยังเนื้อหา
AggregaAggregaAggregaAggrega
เลือกภาษา: ไทย
หน้านี้ถูกแปลด้วยเครื่อง อ่านต้นฉบับภาษาอังกฤษ

นำเข้าโปรเจกต์ Compose

บทแนะนำทีละขั้นตอนนี้นำเข้าโปรเจกต์ Compose ขนาดเล็ก — API ฐานข้อมูล PostgreSQL ที่เก็บบันทึกย่อ และแคช Redis — และบิลด์สิ่งที่นำเข้ามาสำหรับ Docker Compose การนำเข้าร่างทุกไฟล์ที่บิลด์อ่าน โดยนำสิ่งที่ไฟล์ Compose ระบุมาด้วย ได้แก่การเชื่อมต่อของ API ไปยังฐานข้อมูลและแคช ผู้ใช้และชื่อของฐานข้อมูล รหัสผ่านเป็นการอ้างอิงตัวแปร และแท็กของแต่ละอิมเมจ ทุกไฟล์และทุกบล็อกของเอาต์พุตด้านล่างมาจากการรันจริง โปรเจกต์และแต่ละขั้นตอนของบทแนะนำอยู่ในตัวอย่าง import-compose ซึ่งสคริปต์ตรวจสอบจะรันการนำเข้าอีกครั้ง เปรียบเทียบสิ่งที่เขียน สิ่งที่พิมพ์ และบล็อกที่หน้านี้อ้างอิงกับไฟล์ที่เก็บไว้ที่นั่น แล้วตรวจสอบความถูกต้องและบิลด์สิ่งที่การนำเข้าเขียน

ร่างตัวอย่าง

คุณรันการนำเข้านี้ด้วยคำสั่งในหน้านี้ได้ หรือ ให้ผู้ช่วย AI ของคุณรันให้

เขียนตัวอย่างลงในโฟลเดอร์ใหม่ที่ตั้งชื่อตามตัวอย่าง:

Terminal window
aggrega draft --example import-compose

จากนั้นจะพิมพ์คำสั่งที่ต้องรันต่อ คำสั่งแรกเปลี่ยนเข้าโฟลเดอร์ใหม่ ส่วนคำสั่งถัดไปด้านล่างเปลี่ยนเข้าโฟลเดอร์ source ซึ่งมีสิ่งที่หน้านี้นำเข้าและเป็นที่ที่การนำเข้ารัน:

Terminal window
cd import-compose

โฟลเดอร์ source ของตัวอย่างที่ร่างไว้ เปิดอยู่ใน Visual Studio Code แสดงไฟล์ Compose ของโปรเจกต์: API ของบริการ notes ฐานข้อมูล PostgreSQL และแคช Redis

โปรเจกต์

compose.yaml:

# A small notes service: its API, the PostgreSQL database it keeps the notes in,
# and the Redis cache in front of it.
name: notes
services:
api:
image: ghcr.io/example/notes-api:2.3.0
ports:
- "8080:8080"
environment:
DATABASE_HOST: db
DATABASE_USER: notes
DATABASE_NAME: notes
DATABASE_PASSWORD: ${POSTGRES_PASSWORD}
CACHE_HOST: cache
depends_on:
- db
- cache
db:
image: postgres:16
environment:
POSTGRES_USER: notes
POSTGRES_DB: notes
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
volumes:
- db-data:/var/lib/postgresql/data
cache:
image: redis:7
volumes:
db-data:

ข้างไฟล์นั้น .aggrega-config.yaml ทำให้โฟลเดอร์เป็นพื้นที่ทำงาน การนำเข้าจึงเขียนฉบับร่างลงที่นั่น:

# The workspace key marks this directory as the workspace root, so an import run
# here writes its drafts beside compose.yaml.
workspace: {}
# The registry the importer is fetched from the first time an import needs it.
default_registry: hub.aggrega.ai

การนำเข้าเทียบแต่ละบริการกับ Components ของแค็ตตาล็อกของพื้นที่ทำงาน และ --target ร่าง Manifest จาก Recipes ของแค็ตตาล็อก พื้นที่ทำงานไม่ระบุโฟลเดอร์แค็ตตาล็อก การนำเข้าจึงอ่านแค็ตตาล็อกไลบรารีมาตรฐานจากรีจิสทรีที่ดึงตัวนำเข้ามา: สแนปช็อตที่ลงลายเซ็นของแค็ตตาล็อกที่เผยแพร่ ซึ่งการนำเข้าครั้งแรกดึง ยืนยัน และเก็บไว้ในแคชเนื้อหาของพื้นที่ทำงาน และระบุด้วยไดเจสต์ในบันทึกที่การนำเข้าเขียนไว้ข้างฉบับร่าง พื้นที่ทำงานที่ระบุโฟลเดอร์แค็ตตาล็อกของตนเอง — ใน AGGREGA_CATALOG_DIRS หรือภายใต้ workspace.catalog_dirs — อ่านโฟลเดอร์เหล่านั้นก่อน โดยมีสแนปช็อตที่เผยแพร่อยู่ถัดมา

ไฟล์คำตอบ

บิลด์สำหรับ Docker Compose ไม่ต้องใช้สิ่งที่ไฟล์ Compose ไม่ระบุ การนำเข้านี้จึงไม่ถามอะไร บิลด์โปรเจกต์เดียวกันสำหรับ Kubernetes ด้วย --target helm ต้องใช้ค่าหนึ่งที่ไฟล์ Compose ใดไม่มี: ชื่อของ Kubernetes Secret ที่ใช้อ่านรหัสผ่านของฐานข้อมูล เพราะบิลด์สำหรับ Kubernetes อ่านรหัสผ่านจาก Secret ไม่ใช่จาก shell ของคุณ ตัวอย่างเก็บคำตอบนั้นไว้ใน import-answers.yaml ข้าง compose.yaml ซึ่งเป็นที่ที่การนำเข้าหา:

# Values the import could not draft from the source, keyed by the drafted file and the key path inside
# it. Write each one beside its key; an import of the same source, and a sync, read them from here and
# ask nothing. A secret is never written here: its key asks where the secret comes from — a variable
# name, ${NAME}, or the name of a Kubernetes Secret.
release.yaml:
# The origin of the secret value, written by the operator in the Release. Read exactly as
# values.origin is. Write it here or in values.origin, never in both. A secret: name where it comes
# from, never the value.
spec.components.db.features.password.infra.origin: "notes-db-password"

การนำเข้าเขียนไฟล์เองในครั้งแรกที่รันด้วย --target helm: แสดงรายการค่าที่ร่างไม่ได้ โดยคีย์ว่าง แล้วหยุด คำตอบคือชื่อของ Secret ไม่ใช่รหัสผ่าน การนำเข้าสำหรับ Docker Compose ไม่ต้องใช้ค่าดังกล่าว เก็บคำตอบไว้ในไฟล์ และไม่ใช้ ไฟล์คำตอบของการนำเข้า อธิบายไฟล์ คำถามที่การนำเข้าถามที่เทอร์มินัลแทน และสิ่งที่การนำเข้าทำเมื่อไม่มีทั้งสองอย่าง

aggrega import ระบุทุกแฟล็กที่คำสั่งรับ รวมถึง --answers ซึ่งระบุไฟล์คำตอบที่เก็บไว้ที่อื่น

เปลี่ยนเข้าโฟลเดอร์ source:

Terminal window
cd source

รันการนำเข้า

Terminal window
aggrega import compose.yaml --target compose

ที่เอาต์พุตมาตรฐาน การนำเข้าแสดงสิ่งที่เขียน:

wrote aggregate.yaml
wrote release.yaml
wrote manifest.yaml
wrote .aggrega-import.json

ที่ข้อผิดพลาดมาตรฐาน โดยที่ <workspace> แทนโฟลเดอร์ที่คุณรัน:

[… omitted: a warning that the importer runs unverified, printed where its module is read from beside its Plugin entity in a catalog folder rather than fetched]
read as the compose format, with drafted paths relative to <workspace>
warn: the value of environment.POSTGRES_USER of node db is carried as written in the drafts: its format marks the field as no credential, though it can carry one, and setting empty_credentials to all writes it empty
warn: the value of environment.POSTGRES_DB of node db is carried as written in the drafts: its format marks the field as no credential, though it can carry one, and setting empty_credentials to all writes it empty
mapped api (ghcr.io/example/notes-api) to core/compute/server as a reference, NONE
mapped db (postgres) to core/datastore/postgres as a reference, HIGH at 0.9
mapped cache (redis) to core/datastore/redis as a reference, HIGH at 0.9
Pull complete: <workspace>/release.lock (entities=10, source_artifacts=0, external_inputs=0)

การนำเข้าที่รันจากตัวแก้ไข: Aggregate, Release และ Manifest ที่ร่างปรากฏข้างไฟล์ Compose เอาต์พุตของ Aggrega แสดงว่าแมปแต่ละบริการอย่างไรและไฟล์ที่เขียน และการแจ้งเตือนบอกว่าการนำเข้าร่างไฟล์ 3 ไฟล์ โดยรู้จักเป็น compose

  • บรรทัด warn ระบุแต่ละค่าที่ฉบับร่างนำมาตามที่เขียน ตามบริการและตัวแปรของค่านั้น ไม่ระบุด้วยค่า: Compose ไม่ทำเครื่องหมายค่าใดเป็นข้อมูลรับรอง และค่าใดก็อาจมีข้อมูลรับรองได้ ในที่นี้ไม่มี ตัวแปรที่ API อ่านจากบริการอื่นไม่ได้นำมาเป็นค่าเลย — การเชื่อมต่อส่งมอบให้ — และสองตัวที่อ่าน ${POSTGRES_PASSWORD} เป็นการอ้างอิงตัวแปรเท่านั้น จึงไม่มีคำเตือนระบุถึง
  • บรรทัด mapped บอกว่าแต่ละบริการกลายเป็นอะไร api รันอิมเมจของตนเอง จึงเป็นอินสแตนซ์ของเซิร์ฟเวอร์ทั่วไปของไลบรารีมาตรฐาน คือ core/compute/server ส่วน db และ cache รันอิมเมจที่ Compose Recipes ของไลบรารีมาตรฐานรันสำหรับ core/datastore/postgres และ core/datastore/redis แต่ละรายการตรงกับ Component นั้นในระดับสูงที่ 0.9 และฉบับร่างอ้างอิง Component นั้น
  • บรรทัด Pull complete คือการนำเข้าดึงสิ่งที่ Release ที่ร่างพึ่งพา เหมือนที่ aggrega pull ทำ ลงใน release.lock ฉบับร่างจึงตรวจสอบความถูกต้องและบิลด์ได้ทันที ภายใต้ --offline การนำเข้าไม่ pull สิ่งใดและระบุ aggrega pull ที่ต้องรัน

สิ่งที่การนำเข้าเขียน

Aggregate, Release และ Manifest ชื่อ imported/notes ตาม name: ของโปรเจกต์ Compose — ไฟล์ Compose ที่ไม่มี name: ตั้งชื่อตามไฟล์ คือ compose และไดเรกทอรีที่มีไฟล์ซึ่งนำเข้าเป็นไดเรกทอรี ตั้งชื่อตามไดเรกทอรีนั้น ตามที่ Compose ตั้งชื่อโปรเจกต์ — ภายใต้เนมสเปซที่การตั้งค่า target_namespace ของตัวนำเข้ากำหนด คือ imported เว้นแต่คุณตั้งค่าอื่น (การตั้งค่า)

aggregate.yaml — หนึ่งรายการต่อบริการ การเชื่อมต่อที่สภาพแวดล้อมของ API ชี้ให้เห็น และค่ากับรหัสผ่านที่ไฟล์ Compose ตั้ง:

# Not carried, since the draft reads the services and the models alone: volumes.
apiVersion: aggrega.ai/v1beta1
kind: Aggregate
metadata:
name: imported/notes
version: 0.1.0
description: Draft Aggregate imported from a source in the compose format.
spec:
components:
api:
type: core/compute/server
features:
# For the compose target, the lines below declare database_password on api: a secret the Release alone attaches arrives under the key value, so each is declared with a key of its own and no two arrive under one; the Release attaches its origin, naming this type or none.
database_password:
type: core/secret-value
values:
key: DATABASE_PASSWORD
uses:
cache:
cache:
target: cache
# The variables the source sets to what cache publishes, delivered from it under the names the source gives them.
inject:
env:
- in: "CACHE_HOST"
from: host
db:
db:
target: db
# The variables the source sets to what db publishes, delivered from it under the names the source gives them.
inject:
env:
- in: "DATABASE_HOST"
from: host
- in: "DATABASE_USER"
from: user
- in: "DATABASE_NAME"
from: database
db:
type: core/datastore/postgres
# The values of core/datastore/postgres the source sets as POSTGRES_USER, POSTGRES_DB, the variables its Compose Recipes set from them.
values:
user: "notes"
database: "notes"
features:
# For the compose target, the lines below declare password on db: a secret the Release alone attaches arrives under the key value, so each is declared with a key of its own and no two arrive under one; the Release attaches its origin, naming this type or none.
password:
type: core/secret-value
values:
key: POSTGRES_PASSWORD
cache:
type: core/datastore/redis

ตัวแปรของ API แต่ละตัวที่ระบุชื่อบริการอื่นกลายเป็นบล็อก inject ของการเชื่อมต่อนั้น: ไฟล์ Compose ตั้ง DATABASE_HOST เป็น db และการเชื่อมต่อส่งมอบ host ที่ฐานข้อมูลเผยแพร่ภายใต้ชื่อนั้น ค่าจึงตามฐานข้อมูลไปในทุกเป้าหมายโดยไม่ต้องคัดลอก DATABASE_USER และ DATABASE_NAME ตรงกับผู้ใช้และฐานข้อมูลที่บริการ PostgreSQL สร้างขึ้นมา จึงส่งมอบจากสิ่งที่บริการเผยแพร่เช่นกัน และสองค่านั้นกลายเป็น values ของฐานข้อมูลเอง รหัสผ่านแต่ละตัวกลายเป็นการแนบ core/secret-value ที่ประกาศพร้อมคีย์ที่ตัวแปรอ่านภายใต้ชื่อนั้น

release.yaml — สภาพแวดล้อม อิมเมจและพอร์ตของ API เวอร์ชันของเอนจินของแต่ละที่จัดเก็บข้อมูล และที่มาของรหัสผ่านแต่ละตัว:

apiVersion: aggrega.ai/v1beta1
kind: Release
metadata:
name: imported/notes
# environment is a drafted default, since no source names the environment this Release deploys to; change it to name the one it deploys to.
environment: development
version: 0.1.0
description: Draft Release imported from a source in the compose format.
spec:
aggregate: imported/notes
manifest: imported/notes
components:
# The source also states, for the Release to carry where core/compute/server takes it:
# ports:
# - "8080:8080"
api:
infra:
image: "ghcr.io/example/notes-api:2.3.0"
port: 8080
# Published on this machine's loopback address, 127.0.0.1: reachable from this machine only, by any process running on it. Delete this line to keep the service internal.
published_port: 8080
features:
database_password:
type: core/secret-value
infra:
# Attached by reference, never as a value: the value api reads as DATABASE_PASSWORD, read from the variable POSTGRES_PASSWORD, which a target substituting variable references, as Docker Compose does when the stack starts, reads from the environment, so no file carries the value.
origin: "${POSTGRES_PASSWORD}"
# The source states, for the Release to carry where core/datastore/postgres takes it:
# volumes:
# - "db-data:/var/lib/postgresql/data"
db:
infra:
# The tag of the image the source runs, postgres:16, the engine_version of core/datastore/postgres its Compose Recipes run.
engine_version: "16"
features:
password:
type: core/secret-value
infra:
# Attached by reference, never as a value: the password of db, which the source sets as POSTGRES_PASSWORD, read from the variable POSTGRES_PASSWORD, which a target substituting variable references, as Docker Compose does when the stack starts, reads from the environment, so no file carries the value.
origin: "${POSTGRES_PASSWORD}"
cache:
infra:
# The tag of the image the source runs, redis:7, the engine_version of core/datastore/redis its Compose Recipes run.
engine_version: "7"

สภาพแวดล้อม development เป็นค่าเริ่มต้นที่การนำเข้าเขียนเพราะไม่มีไฟล์ Compose ใดระบุ เปลี่ยนเมื่อ Release ปรับใช้ที่อื่น แท็กของอิมเมจกลายเป็นเวอร์ชันของเอนจินที่ Components ของไลบรารีมาตรฐานรับ port ของ API คือพอร์ตที่คอนเทนเนอร์ฟัง และ published_port คือพอร์ตของเครื่องที่รายการ ports เผยแพร่ ทั้งสองเป็น 8080 ตามที่ไฟล์ Compose ระบุ ตามที่ความคิดเห็นของบรรทัดบอก ทุกกระบวนการบนเครื่องนี้เข้าถึง API ที่ http://127.0.0.1:8080 ได้ และไม่มีสิ่งอื่นเข้าถึงได้ และการลบบรรทัดนั้นทำให้ API อยู่บนเครือข่ายของสแตกเท่านั้น สิ่งที่ไม่มีคีย์ใดรับยังคงเป็นความคิดเห็นเหนือรายการของตน: รายการ ports ของซอร์ส ซึ่ง port และ published_port นำมาแล้ว และ volume ที่ตั้งชื่อของฐานข้อมูล

manifest.yaml — Compose Recipe ของแต่ละชนิดของส่วนประกอบ และการทำให้เป็นรูปธรรมที่เขียนการอ้างอิงตัวแปรของรหัสผ่านลงในไฟล์ Compose ตามที่เขียน:

apiVersion: aggrega.ai/v1beta1
kind: Manifest
metadata:
name: imported/notes
version: 0.1.0
spec:
mappings:
components:
type:
core/compute/server: compose/server
core/datastore/postgres: compose/postgres
core/datastore/redis: compose/redis
features:
type:
core/secret-value: compose/dotenv-secret

release.lock บันทึกเอนทิตีในแค็ตตาล็อกที่ Release หาค่าได้ ตามไดเจสต์ และ .aggrega-import.json เป็นบันทึกของสิ่งที่การนำเข้าเขียน ซึ่ง aggrega import --sync ครั้งต่อไปอ่าน

ตรวจสอบความถูกต้องและบิลด์

Terminal window
aggrega validate --release release.yaml
aggrega build --release release.yaml

Release ที่ร่างเปิดอยู่ในตัวแก้ไขหลังตรวจสอบความถูกต้อง โดยเอาต์พุตของ Aggrega และการแจ้งเตือนต่างรายงานว่าไม่พบปัญหา

บิลด์ของ Release ที่ร่าง: โฟลเดอร์ dist ปรากฏใน Explorer เอาต์พุตของ Aggrega แสดงไฟล์ Docker Compose ที่เขียน และการแจ้งเตือนบอกว่าเขียนไฟล์แล้ว

การตรวจสอบความถูกต้องพิมพ์:

notice: LINT: Feature "core/secret-value" at core/secret-value:1.0.0 declares property "origin" in both values: and infra: pillars (path: origin)
release dependency validation passed: release.yaml (schema-pillar + dependency pillars)

และบิลด์ โดยที่ <workspace> แทนโฟลเดอร์ที่คุณรัน:

notice: LINT: Feature "core/secret-value" at core/secret-value:1.0.0 declares property "origin" in both values: and infra: pillars (path: origin)
[… omitted: a total build time, which differs per run]
Build completed: 1 file written to <workspace>/dist

บรรทัด notice เป็นเชิงแนะนำ: Feature ของรหัสผ่านรับ origin จาก values ของ Aggregate หรือ infra ของ Release และการแจ้งให้ทราบรายงานว่ามีค่าระดับใบหนึ่งค่าที่ประกาศไว้ทั้งสองที่ ซึ่งไม่เปลี่ยนคำตัดสิน

บริการ api ใน dist/compose/docker-compose.yaml อ่านสภาพแวดล้อมที่โปรเจกต์ Compose มอบให้ ซึ่งส่งมอบโดยการเชื่อมต่อและการแนบรหัสผ่าน:

api:
image: ghcr.io/example/notes-api:2.3.0
environment:
CACHE_HOST: cache
DATABASE_HOST: db
DATABASE_NAME: notes
DATABASE_PASSWORD: ${POSTGRES_PASSWORD}
DATABASE_USER: notes
ports:
- 127.0.0.1:8080:8080
[… omitted: the labels the build writes on every service, naming the Aggregate, the component, the environment and the compiler's version]
networks:
- compose
depends_on:
- cache
- db
[… omitted: the CPU and memory the server's defaults reserve]

และฐานข้อมูลกับแคชรันอิมเมจที่ไฟล์ Compose ระบุ:

db:
image: postgres:16
environment:
POSTGRES_USER: notes
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: notes
[… omitted: the labels, the network and a health check running PostgreSQL's pg_isready]
cache:
image: redis:7
[… omitted: the labels, the network and a health check running redis-cli ping]

ไฟล์ที่เรนเดอร์แสดงความแตกต่างสองข้อจากโปรเจกต์ที่คุณเริ่มต้น พอร์ตเผยแพร่บนที่อยู่ลูปแบ็กเท่านั้น ตามที่ Compose Recipe ของเซิร์ฟเวอร์ทั่วไปเผยแพร่ และ volume ที่ตั้งชื่อซึ่งไฟล์ Compose เมานต์สำหรับข้อมูลของฐานข้อมูลไม่ปรากฏที่ใดเลย: ฉบับร่างแสดงรายการนั้นในความคิดเห็นเหนือรายการของฐานข้อมูล ไม่มีคีย์ใดของ Component PostgreSQL รับ และบริการฐานข้อมูลที่เรนเดอร์ไม่เมานต์ volume ใดเลย

กับผู้ช่วย AI

ผู้ช่วย AI ที่ ตั้งค่าด้วยเครื่องมือของ Aggrega ทำสิ่งเดียวกันได้: วางพรอมต์นี้ลงไป

ใช้เฉพาะเครื่องมือของ Aggrega และอย่าสร้างโฟลเดอร์เอง หากโฟลเดอร์พื้นที่ทำงานยังไม่มีโฟลเดอร์ `import-compose` เรียกเครื่องมือ `draft` หนึ่งครั้งโดยตั้ง `example` เป็น `import-compose` และไม่ตั้ง `dir`: เครื่องมือนี้เขียนตัวอย่างลงในโฟลเดอร์ `import-compose` ใหม่ ซึ่งโฟลเดอร์ `source` ของโฟลเดอร์นั้นมีสิ่งที่หน้านี้นำเข้า จากนั้นเรียกเครื่องมือ `import` โดยตั้ง `workspace_root` เป็น `import-compose/source` ตั้ง `source` เป็น `compose.yaml` และตั้ง `target` เป็น `compose`: เครื่องมือนี้อ่านคำตอบที่ต้องใช้จาก `import-answers.yaml` ข้าง `compose.yaml` เขียนฉบับร่างลงใน `import-compose/source` และ pull สิ่งที่ฉบับร่างต้องใช้ จากนั้นเรียกเครื่องมือ `validate` โดยตั้ง `workspace_root` เป็น `import-compose/source` และ `release` เป็น `release.yaml` และเครื่องมือ `build` โดยตั้ง `workspace_root` เป็น `import-compose/source` ตั้ง `release` เป็น `release.yaml` และตั้ง `write` เป็น true บอกฉันว่าบิลด์เขียนไฟล์ใดบ้าง ไฟล์เหล่านั้นอยู่ใต้ `import-compose/source/dist/`

เปิดใน VS Code

เมื่อติดตั้ง ส่วนขยาย Aggrega สำหรับ Visual Studio Code แล้ว เปิดโฟลเดอร์ import-compose/source ใน VS Code ซึ่งเป็นที่ที่การนำเข้าเขียนฉบับร่าง โฟลเดอร์มี Release เดียว คือ release.yaml ที่ร่าง ตัวแก้ไขจึงเลือกให้คุณ และตรวจสอบฉบับร่างเทียบกับ Release นั้นขณะที่คุณแก้ไข โดยทำเครื่องหมายสิ่งที่พบในไฟล์และในแผง Problems Aggrega: Show topology graph ใน Command Palette วาดส่วนประกอบของ Release และการเชื่อมต่อระหว่างส่วนประกอบเหล่านั้น Aggrega: Build, Aggrega: Preview build และ Aggrega: Pull ทำงานกับ Release เดียวกัน และแต่ละคำสั่งยังเป็นปุ่มในแผง Commands ของมุมมอง Aggrega ในแถบกิจกรรม Build เขียนไฟล์ลงใน dist/ ของโฟลเดอร์ ใช้ Aggrega ใน Visual Studio Code ครอบคลุมส่วนที่เหลือ

วิธีรัน

ghcr.io/example/notes-api:2.3.0 แทนอิมเมจของ API ของคุณเอง เป็นตัวยึดตำแหน่ง และ Docker Compose หยุดที่อิมเมจนี้พร้อมข้อความ Error Head "https://ghcr.io/v2/example/notes-api/manifests/2.3.0": denied ระบุอิมเมจของ API ของคุณใน infra.image ของ api ใน release.yaml จนกว่า API ของคุณจะพร้อม อิมเมจที่ตอบ HTTP บนพอร์ต 8080 ใช้แทนได้ การรันด้านล่างใช้ mendhak/http-https-echo:39 ซึ่งตอบทุกคำขอด้วยคำอธิบายของคำขอที่ได้รับ:

api:
infra:
image: "mendhak/http-https-echo:39"
port: 8080

บิลด์อีกครั้งพร้อมการเปลี่ยนแปลง Release ไม่ตรงกับ release.lock ที่การนำเข้าเขียนอีกต่อไป บิลด์จึงต้องใช้ --auto-pull ซึ่งหาค่าอีกครั้ง หากไม่มี บิลด์จะหยุดและระบุ aggrega pull Docker Compose เติมการอ้างอิง ${POSTGRES_PASSWORD} ทั้งสองจาก shell ของคุณ ดังนั้นตั้งรหัสผ่านก่อนเริ่มสแตก หากไม่ตั้ง Compose จะเตือน The "POSTGRES_PASSWORD" variable is not set. Defaulting to a blank string. และเขียนค่าว่างแทน

Terminal window
aggrega build --release release.yaml --auto-pull
export POSTGRES_PASSWORD='a password of your own'
docker compose -p notes -f dist/compose/docker-compose.yaml up -d --wait

-p ตั้งชื่อโปรเจกต์ ตามที่ขั้นตอนรันใน วิธีที่การนำเข้าทำงาน อธิบาย --wait กลับมาเมื่อฐานข้อมูลและแคชผ่านการตรวจสอบสถานะการทำงานที่บิลด์กำหนดให้ และคอนเทนเนอร์ของ API กำลังรัน: บิลด์ไม่กำหนดการตรวจสอบสถานะการทำงานให้ API

ทดสอบ

ถาม API ที่ http://127.0.0.1:8080 ซึ่งเป็นที่อยู่ที่ published_port เผยแพร่:

Terminal window
curl -sS http://127.0.0.1:8080/notes
{
"path": "/notes",
"headers": {
"host": "127.0.0.1:8080",
[… omitted: the user agent and the media types the client sent]
},
"method": "GET",
[… omitted: the rest of the request as the stand-in received it, the client's address and the container's host name]
}

สิ่งที่การเชื่อมต่อส่งมอบอยู่ในสภาพแวดล้อมของบริการ API ภายใต้ชื่อที่ API ของคุณอ่าน:

Terminal window
docker compose -p notes -f dist/compose/docker-compose.yaml exec api printenv DATABASE_HOST DATABASE_USER DATABASE_NAME CACHE_HOST
db
notes
notes
cache

หยุดสแตกด้วย:

Terminal window
docker compose -p notes -f dist/compose/docker-compose.yaml down

บน Kubernetes

โปรเจกต์เดียวกันนำเข้าสำหรับ Kubernetes ได้ด้วย --target helm และการนำเข้าอ่านชื่อของ Secret คือ notes-db-password จากไฟล์คำตอบข้างต้น ฉบับร่างสำหรับ Docker Compose อยู่ใน source แล้ว จึงร่างชุดนี้ลงในโฟลเดอร์ของตนเอง ระบุอิมเมจของ API ใน release.yaml ของโฟลเดอร์นั้นเหมือน Docker Compose และบิลด์จากโฟลเดอร์นั้น:

Terminal window
aggrega import compose.yaml --target helm --output-dir helm
cd helm
aggrega build --release release.yaml --auto-pull

บิลด์เขียนแมนิเฟสต์ Kubernetes ธรรมดาไว้ใต้ dist/helm/ ที่รูทของพื้นที่ทำงาน คือ source ซึ่งจาก helm คือ ../dist/helm/ สร้าง Secret notes-db-password ในเนมสเปซที่คุณนำไปใช้ โดยเก็บรหัสผ่านไว้ใต้คีย์ข้อมูล value และนำโฟลเดอร์ไปใช้ ตามที่ขั้นตอนรันใน วิธีที่การนำเข้าทำงาน อธิบาย Service ของ API คือ api-default: ส่งต่อพอร์ตภายในเครื่องไปยัง Service นั้น และปล่อยคำสั่งรันต่อไป:

Terminal window
kubectl port-forward -n <namespace> svc/api-default 8080:8080

จากเทอร์มินัลอื่น คำขอข้างต้นไปถึง API ที่ที่อยู่เดียวกันโดยไม่เปลี่ยน และตัวแทนชั่วคราวตอบแบบเดียวกัน "host": "127.0.0.1:8080" การเชื่อมต่อส่งมอบชื่อของ Services: kubectl exec -n <namespace> deploy/api -- printenv DATABASE_HOST DATABASE_USER DATABASE_NAME CACHE_HOST พิมพ์ db-default, notes, notes และ cache-default API ยังไม่ได้รับรหัสผ่านฐานข้อมูลบน Kubernetes: Secret notes-db-password ถึงฐานข้อมูลเท่านั้น จึงไม่มีการตั้ง DATABASE_PASSWORD ในคอนเทนเนอร์ api

เมื่อไฟล์ Compose เปลี่ยน

เก็บ .aggrega-import.json ไว้ข้างฉบับร่าง และเมื่อไฟล์ Compose เปลี่ยน รัน:

Terminal window
aggrega import compose.yaml --sync

เปลี่ยนเฉพาะสิ่งที่การนำเข้าเขียน และอ่านไฟล์คำตอบเดียวกัน ส่วนที่ซอร์สไม่แตะต้องคงอยู่บนดิสก์ตามเดิม ไม่ว่าจะแก้ไขหรือไม่ หากซอร์สเปลี่ยนส่วนที่คุณแก้ไข การนำเข้าจะหยุด ระบุส่วนนั้น และไม่เขียนสิ่งใด เพื่อให้คุณประสานด้วยมือ aggrega import อธิบายกฎและทุกแฟล็กโดยละเอียด