# 시스템 패키지 업데이트 및 필수 패키지 설치 RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ && rm -rf /var/lib/apt/lists/*
# Poetry 설치 RUN pip install poetry
# 로컬 패키지 설치 (project-name_beta_~.whl 위치를 /app/library로 복사) COPY ../aipin_library/aipin_beta_~.whl /app/library/ RUN poetry add /app/library/aipin_beta_~.whl
# 프로젝트 파일 복사 COPY . .
# 스크립트 실행 권한 부여 RUNchmod +x start.sh
# 컨테이너 시작 시 실행할 명령어 CMD ["./start.sh"]
단계 2: Docker 이미지 빌드
위의 Dockerfile을 프로젝트 루트 디렉토리에 저장한 후, Docker 이미지를 빌드합니다. 터미널에서 다음 명령어를 실행한다.
# 시스템 패키지 업데이트 및 필수 패키지 설치 RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ ca-certificates \ && rm -rf /var/lib/apt/lists/*
# 인증서 문제 해결을 위한 환경 변수 설정 ENV PIP_CERT /etc/ssl/certs/ca-certificates.crt
# Poetry 설치 RUN pip install poetry --trusted-host pypi.org --trusted-host files.pythonhosted.org
# 로컬 패키지 설치 (aipin_beta_~.whl 위치를 /app/library로 복사) COPY aipin_library/aipin_beta_2024-0.1.25-py3-none-any.whl /app/library/ RUN poetry add /app/library/aipin_beta_2024-0.1.25-py3-none-any.whl
# 프로젝트 파일 복사 COPY . .
# 스크립트 실행 권한 부여 RUNchmod +x start.sh
# 컨테이너 시작 시 실행할 명령어 CMD ["./start.sh"]
하지만 아래와 같은 에러가난다.
1 2 3 4 5 6 7
ERROR [6/8] RUN poetry add /app/library/aipin_beta_2024-0.1.25-py3-none-any.whl 0.6s ------ > [6/8] RUN poetry add /app/library/aipin_beta_2024-0.1.25-py3-none-any.whl: #10 0.507 #10 0.507 Poetry could not find a pyproject.toml file in /app or its parents ------ executor failed running [/bin/sh -c poetry add /app/library/aipin_beta_2024-0.1.25-py3-none-any.whl]: exit code:
poetry는 pyproject.toml 파일이 있는 디렉토리에서 실행되어야 한다.
Dockerfile에서 poetry를 실행하기 전에 pyproject.toml 파일이 있는 디렉토리로 이동해야 한다.
현재 Dockerfile의 구조를 보면, pyproject.toml 파일은 /app 디렉토리로 복사되기 전에 poetry add 명령이 실행되고 있는 것 같다.
# 시스템 패키지 업데이트 및 필수 패키지 설치 RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ ca-certificates \ && rm -rf /var/lib/apt/lists/*
# 인증서 문제 해결을 위한 환경 변수 설정 ENV PIP_CERT /etc/ssl/certs/ca-certificates.crt
# Poetry 설치 RUN pip install poetry --trusted-host pypi.org --trusted-host files.pythonhosted.org
# 프로젝트 파일 복사 (pyproject.toml 파일 포함) COPY . .
# 로컬 패키지 설치 (aipin_beta_~.whl 위치를 /app/library로 복사) COPY aipin_library/aipin_beta_2024-0.1.25-py3-none-any.whl /app/library/
# Poetry를 사용하여 로컬 패키지 추가 RUN poetry add /app/library/aipin_beta_2024-0.1.25-py3-none-any.whl
# 스크립트 실행 권한 부여 RUNchmod +x start.sh
# 컨테이너 시작 시 실행할 명령어 CMD ["./start.sh"]
이렇게 바꿔서 실행하면…
이번엔 이런 에러가 나타난다.
1 2 3 4 5 6 7 8 9 10
ERROR [7/8] RUN poetry add /app/library/aipin_beta_2024-0.1.25-py3-none-any.whl 0.7s ------ > [7/8] RUN poetry add /app/library/aipin_beta_2024-0.1.25-py3-none-any.whl: #11 0.623 Path /aipin_library/aipin_beta_2024-0.1.25-py3-none-any.whl for aipin-beta-2024 does not exist #11 0.644 The currently activated Python version 3.9.19 is not supported by the project (^3.10). #11 0.644 Trying to find and use a compatible version. #11 0.677 #11 0.677 Poetry was unable to find a compatible version. If you have one, you can explicitly use it via the "env use" command. ------ executor failed running [/bin/sh -c poetry add /app/library/aipin_beta_2024-0.1.25-py3-none-any.whl]: exit code: 1
이 에러는 두 가지 문제를 나타낸다:
pyproject.toml 파일에서 Python 버전이 ^3.10로 설정되어 있고, 현재 Docker 이미지에서 사용 중인 Python 버전은 3.9.19이다.
Poetry가 해당 .whl 파일을 찾지 못하고 있다.
이 문제들을 해결하기 위해 다음과 같이 진행한다
Python 버전 변경: Dockerfile에서 Python 3.10 이미지를 사용하도록 변경한다.
# 시스템 패키지 업데이트 및 필수 패키지 설치 RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ ca-certificates \ && rm -rf /var/lib/apt/lists/*
# 인증서 문제 해결을 위한 환경 변수 설정 ENV PIP_CERT /etc/ssl/certs/ca-certificates.crt
# Poetry 설치 RUN pip install poetry --trusted-host pypi.org --trusted-host files.pythonhosted.org
# 프로젝트 파일 복사 (pyproject.toml 파일 포함) COPY . .
# 로컬 패키지 설치 (aipin_beta_~.whl 위치를 /app/library로 복사) COPY aipin_library/aipin_beta_2024-0.1.25-py3-none-any.whl /app/library/
# Poetry 환경 설정: Python 3.10 사용 강제 RUN poetry env use python3.10
# Poetry를 사용하여 로컬 패키지 추가 RUN poetry add /app/library/aipin_beta_2024-0.1.25-py3-none-any.whl
# 스크립트 실행 권한 부여 RUN chmod +x start.sh
# 컨테이너 시작 시 실행할 명령어 CMD ["./start.sh"]
이렇게 수정하여 이미지를 빌드하였고, 이미지가 잘 만들어졌다.
ECR애 Docker 이미지 푸시
단계1:AWS CLI 설정
먼저 AWS CLI가 설정되어 있어야 한다. 설정되지 않았다면, 다음 명령을 사용하여 AWS CLI를 설정한다.
$ aws eks update-kubeconfig --region [region명 예 : ap-northeast-2] --name [클러스터 명 예 : eks-prod-ct01-tap-01] An error occurred (UnrecognizedClientException) when calling the DescribeCluster operation: The security token included in the request is invalid
해결책 Cloud9 우측 최상단의 톱니바퀴 클릭 > AWS Settings > Credential 부분을 X로 변경 > 터미널 종료 후 다시 시도
namespace 생성
1 2
$ kubectl create ns aipin namespace/aipin created
Kubernetes 매니페스트 파일 작성
이제 EKS에 배포하기 위한 Kubernetes 매니페스트 파일을 작성한다. deployment.yaml과 service.yaml 파일을 준비한다.
AWS의 Elastic Container Registry에서 이미지 탭에 들어가 배포할 이미지 목록 리스트를 선택 후 URL 복사를 눌러 이미지 태그를 복사한다.
Installing the current project: orchestrator (0.1.0) Traceback (most recent call last): File "/app/src/main.py", line 3, in <module> import controller File "/app/src/controller.py", line 2, in <module> from orchestrator import orchestrator File "/app/src/orchestrator.py", line 1, in <module> from aipin.Plugin.plugin_manager import * File "/root/.cache/pypoetry/virtualenvs/orchestrator-9TtSrW0h-py3.10/lib/python3.10/site-packages/aipin/__init__.py", line 1, in <module> from .orchestrator import Orchestrator File "/root/.cache/pypoetry/virtualenvs/orchestrator-9TtSrW0h-py3.10/lib/python3.10/site-packages/aipin/orchestrator.py", line 1, in <module> from aipin.Plugin import * File "/root/.cache/pypoetry/virtualenvs/orchestrator-9TtSrW0h-py3.10/lib/python3.10/site-packages/aipin/Plugin/__init__.py", line 2, in <module> from .plugin_manager import PluginManager File "/root/.cache/pypoetry/virtualenvs/orchestrator-9TtSrW0h-py3.10/lib/python3.10/site-packages/aipin/Plugin/plugin_manager.py", line 3, in <module> from aipin.Plugin.plugin import * File "/root/.cache/pypoetry/virtualenvs/orchestrator-9TtSrW0h-py3.10/lib/python3.10/site-packages/aipin/Plugin/plugin.py", line 5, in <module> from aipin.data import ChatHistory, ChatRequest File "/root/.cache/pypoetry/virtualenvs/orchestrator-9TtSrW0h-py3.10/lib/python3.10/site-packages/aipin/data/__init__.py", line 3, in <module> from .vector_retriever import PGVectorRetriever, PGVectorConfig File "/root/.cache/pypoetry/virtualenvs/orchestrator-9TtSrW0h-py3.10/lib/python3.10/site-packages/aipin/data/vector_retriever.py", line 2, in <module> from langchain_postgres import PGVector File "/root/.cache/pypoetry/virtualenvs/orchestrator-9TtSrW0h-py3.10/lib/python3.10/site-packages/langchain_postgres/__init__.py", line 3, in <module> from langchain_postgres.chat_message_histories import PostgresChatMessageHistory File "/root/.cache/pypoetry/virtualenvs/orchestrator-9TtSrW0h-py3.10/lib/python3.10/site-packages/langchain_postgres/chat_message_histories.py", line 13, in <module> import psycopg File "/root/.cache/pypoetry/virtualenvs/orchestrator-9TtSrW0h-py3.10/lib/python3.10/site-packages/psycopg/__init__.py", line 9, in <module> from . import pq # noqa: F401 import early to stabilize side effects File "/root/.cache/pypoetry/virtualenvs/orchestrator-9TtSrW0h-py3.10/lib/python3.10/site-packages/psycopg/pq/__init__.py", line 118, in <module> import_from_libpq() File "/root/.cache/pypoetry/virtualenvs/orchestrator-9TtSrW0h-py3.10/lib/python3.10/site-packages/psycopg/pq/__init__.py", line 110, in import_from_libpq raise ImportError( ImportError: no pq wrapper available. Attempts made: - couldn't import psycopg 'c' implementation: No module named 'psycopg_c' - couldn't import psycopg 'binary' implementation: No module named 'psycopg_binary' - couldn't import psycopg 'python' implementation: libpq library not found
로그에 나타난 에러 메시지를 분석해 보면, 문제는 psycopg 패키지가 필요한 libpq 라이브러리를 찾을 수 없기 때문에 발생하는 것으로 보인다. 이는 PostgreSQL과 상호작용하는 Python 라이브러리인 psycopg가 제대로 설치되지 않았거나, 필요한 시스템 종속성이 누락된 경우에 발생한다.
해결법
기본 이미지에 libpq 설치
libpq 라이브러리가 누락된 경우, 이를 설치해 주어야 합니다. Dockerfile에 해당 라이브러리를 설치하는 명령을 추가한다.