본문 바로가기
웹 개발

[Docker] FastAPI container 설치 및 실행 with llama

by doraemin_dev 2025. 2. 3.

도커 이미지 빌드 & 테스트

FastAPI.zip
0.00MB

# 0. 먼저, Docker Desktop 앱 실행하자.
# Docker Desktop이 설치 안 되어있다면, https://www.docker.com/get-started/ 에서 설치.

# 1. FestAPI.zip 압축파일의 압축 풀고 vsCode 실행

# 2. 터미널을 통해 도커 이미지 빌드 & 실행해보자.

# 도커 이미지 빌드
docker build --tag fastapi-image .
# 도커 이미지 빌드 확인
docker images # 잘 설치되었는지 확인
# 도커 실행
docker run -itd -p 8000:80 -v /c/Users/032/SKT_AI/FastAPI/app/:/usr/src/app/ --restart always --name fastapi fastapi-image
# >>> Enter 치기

# 3. 접속
# http://localhost:8000/docs 접속

# + 도커에 다시 접속 할려면
# docker exec -it fastapi bash

더보기
PS C:\Users\032\SKT_AI\FastAPI> docker build --tag fastapi-image .
[+] Building 186.2s (12/12) FINISHED                                                                                   docker:desktop-linux
 => [internal] load build definition from Dockerfile                                                                                   0.0s
 => => transferring dockerfile: 646B                                                                                                   0.0s
 => [internal] load metadata for docker.io/library/python:3.9                                                                          3.7s
 => [auth] library/python:pull token for registry-1.docker.io                                                                          0.0s
 => [internal] load .dockerignore                                                                                                      0.0s
 => => transferring context: 2B                                                                                                        0.0s
 => [1/6] FROM docker.io/library/python:3.9@sha256:70bb4fa90a3b9a375946ef1b8fec25b46f3e87337484cc98b184274cb0cc6210                  165.8s
 => => resolve docker.io/library/python:3.9@sha256:70bb4fa90a3b9a375946ef1b8fec25b46f3e87337484cc98b184274cb0cc6210                    0.0s
 => => sha256:3f511b5e7aa017811521574bb9595a7d32c3fa75b68071ead68c5c35477505d6 250B / 250B                                             0.3s
 => => sha256:3373c61599dad94e4be6bd4fb45943536edbdcf705ee411df9fa4aaa83ecb4fe 19.85MB / 19.85MB                                      32.4s
 => => sha256:3c7779685a1253fc9152e10d8b52a2a397c800854d2b6b1130f8208238f561cd 6.16MB / 6.16MB                                         8.5s
 => => sha256:fbf93b646d6b4d6612a25d50599f4d8e3c7785f83c6ba085219f9d4d334e8aa3 211.33MB / 211.33MB                                   161.4s
 => => sha256:684a51896c8291a1769034cf6e10971c82a82c43b6b4588d1c71d215953eaa61 64.40MB / 64.40MB                                     111.7s 
 => => sha256:bf571be90f05e10949e4ae13071c81d3182077d926e3f84169a12e0ce2aec209 24.06MB / 24.06MB                                      40.7s
 => => sha256:fd0410a2d1aece5360035b61b0a60d8d6ce56badb9d30a5c86113b3ec724f72a 48.48MB / 48.48MB                                      78.3s 
 => => extracting sha256:fd0410a2d1aece5360035b61b0a60d8d6ce56badb9d30a5c86113b3ec724f72a                                              1.2s 
 => => extracting sha256:bf571be90f05e10949e4ae13071c81d3182077d926e3f84169a12e0ce2aec209                                              0.4s 
 => => extracting sha256:684a51896c8291a1769034cf6e10971c82a82c43b6b4588d1c71d215953eaa61                                              1.4s 
 => => extracting sha256:fbf93b646d6b4d6612a25d50599f4d8e3c7785f83c6ba085219f9d4d334e8aa3                                              3.6s 
 => => extracting sha256:3c7779685a1253fc9152e10d8b52a2a397c800854d2b6b1130f8208238f561cd                                              0.2s 
 => => extracting sha256:3373c61599dad94e4be6bd4fb45943536edbdcf705ee411df9fa4aaa83ecb4fe                                              0.5s 
 => => extracting sha256:3f511b5e7aa017811521574bb9595a7d32c3fa75b68071ead68c5c35477505d6                                              0.0s 
 => [internal] load build context                                                                                                      0.1s 
 => => transferring context: 1.26kB                                                                                                    0.0s 
 => [2/6] RUN echo "alias ll='ls -l'" >> ~/.bashrc                                                                                     0.9s 
 => [3/6] WORKDIR /usr/src                                                                                                             0.0s 
 => [4/6] COPY ./requirements.txt /usr/src/requirements.txt                                                                            0.0s 
 => [5/6] COPY ./app /usr/src/app                                                                                                      0.0s 
 => [6/6] RUN pip install --no-cache-dir --upgrade -r requirements.txt                                                                13.3s 
 => exporting to image                                                                                                                 2.1s 
 => => exporting layers                                                                                                                1.6s 
 => => exporting manifest sha256:7f2564a3f96d31177d915d48202d2c3e7ad44cd674958cd199f5c25933046077                                      0.0s 
 => => exporting config sha256:c9d9d16263950d9dc8b8f56ae364552bdc6a1d188b68e7f6576ae47503cc2ff8                                        0.0s 
 => => exporting attestation manifest sha256:9894221ff934cf516b21fbba0ecef0bed509b6cbc4e277c65f2328ad9b1b6160                          0.0s 
 => => exporting manifest list sha256:50c95407a2976fa374b1b8926c777bcfc4567e69d3971b110ee93674e887ce59                                 0.0s 
 => => naming to docker.io/library/fastapi-image:latest                                                                                0.0s 
 => => unpacking to docker.io/library/fastapi-image:latest                                                                             0.4s 
PS C:\Users\032\SKT_AI\FastAPI> docker images
REPOSITORY                    TAG       IMAGE ID       CREATED         SIZE
fastapi-image                 latest    50c95407a297   2 minutes ago   1.5GB
guestbook-app                 latest    0f1dc83f97b7   2 weeks ago     1.35GB
crkmkm.azurecr.io/guestbook   v1        a6efe6193dae   2 weeks ago     1.35GB
<none>                        <none>    cc76c98e1990   2 weeks ago     1.35GB
firstnode-app                 1.0       6affa8f4f930   2 weeks ago     1.35GB
alpine                        latest    56fa17d2a7e7   3 weeks ago     12.1MB
wordpress                     latest    79dd7ea093b2   2 months ago    993MB
mysql                         5.7       4bc6bc963e6d   13 months ago   689MB

PS C:\Users\032\SKT_AI\FastAPI> docker run -itd -p 8000:80 -v /c/Users/032/SKT_AI/FastAPI/app/:/usr/src/app/ --restart always --name fastapi
 fastapi-image
>>
c40a0e51c8642cea31d3df538a0c2662b5d6802bbca66c99e9048f024da32b56

라마3.1 모델과 FastAPI와 연동하기

 라마 설치가 안 되어있다면, https://ollama.com/ 에서 설치.

 

Ollama

Get up and running with large language models.

ollama.com

# Power Shell에서 

# 라마 실행해보기
ollama run llama3.2:3b

# ollama 실행
PS C:\Users\032> ollama serve

 

FastAPI 필요 라이브러리 설치

PS C:\Users\032\SKT_AI\FastAPI> docker exec -it fastapi bash      
>> 
root@c40a0e51c864:/usr/src# pip install --upgrade pip
root@c40a0e51c864:/usr/src# pip install requests

root@c40a0e51c864:/usr/src# exit
exit

PS C:\Users\032\SKT_AI\FastAPI> 

# ※ 도커 컨테이너에 모듈을 설치 후에는 반드시 도커 컨테이너를 재시작 해야지만 반영이 됩니다.

더보기
PS C:\Users\032\SKT_AI\FastAPI> docker exec -it fastapi bash      
>> 
root@c40a0e51c864:/usr/src# pip install --upgrade pip
Requirement already satisfied: pip in /usr/local/lib/python3.9/site-packages (23.0.1)
Collecting pip
  Downloading pip-25.0-py3-none-any.whl (1.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 10.7 MB/s eta 0:00:00
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 23.0.1
    Uninstalling pip-23.0.1:
      Successfully uninstalled pip-23.0.1
Successfully installed pip-25.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
root@c40a0e51c864:/usr/src# pip install requests
Collecting requests
  Downloading requests-2.32.3-py3-none-any.whl.metadata (4.6 kB)
Collecting charset-normalizer<4,>=2 (from requests)
  Downloading charset_normalizer-3.4.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (35 kB)
Collecting idna<4,>=2.5 (from requests)
  Downloading idna-3.10-py3-none-any.whl.metadata (10 kB)
Collecting urllib3<3,>=1.21.1 (from requests)
  Downloading urllib3-2.3.0-py3-none-any.whl.metadata (6.5 kB)
Collecting certifi>=2017.4.17 (from requests)
  Downloading certifi-2025.1.31-py3-none-any.whl.metadata (2.5 kB)
Downloading requests-2.32.3-py3-none-any.whl (64 kB)
Downloading certifi-2025.1.31-py3-none-any.whl (166 kB)
Downloading charset_normalizer-3.4.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (146 kB)
Downloading idna-3.10-py3-none-any.whl (70 kB)
Downloading urllib3-2.3.0-py3-none-any.whl (128 kB)
Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests
Successfully installed certifi-2025.1.31 charset-normalizer-3.4.1 idna-3.10 requests-2.32.3 urllib3-2.3.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager, possibly rendering your system unusable. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv. Use the --root-user-action option if you know what you are doing and want to suppress this warning.

 

FastAPI의 main.py를 아래 코드로 변경하자.

from fastapi import FastAPI, Form
from pydantic import BaseModel
import requests
import json

app = FastAPI()

class RequestModel(BaseModel):
    model: str
    prompt: str

@app.post("/llama")
async def llama(prompt: str = Form(...)):
    # 요청할 URL
    url = "http://host.docker.internal:11434/api/generate"
    # 요청에 포함할 데이터
    data = {
        "model": "llama3.2:3b",
        "prompt": prompt
    }    
    # 요청 헤더
    headers = {
        "Content-Type": "application/json"
    }
    # POST 요청 보내기
    response = requests.post(url, headers=headers, data=json.dumps(data))

    if response.status_code == 200:
        # 개별 JSON 객체로 분할, 이후 부터 추가된 코드
        json_objects = response.content.decode().strip().split("\n")
        # 각 JSON 객체를 Python 사전으로 변환
        data = [json.loads(obj) for obj in json_objects]
        res_text = ''
        # 변환된 데이터 출력
        for item in data:
            print(item)
            res_text += item['response']
        # return response.content.decode()
    else:
        res_text = "Error Code : " + response.status_code
    
    return res_text

 

vsCode 터미널에서, main.py 코드 실행

PS C:\Users\032\SKT_AI\FastAPI> docker exec -it fastapi bash
>>
root@c40a0e51c864:/usr/src# ls
app  requirements.txt
root@c40a0e51c864:/usr/src# cd app
root@c40a0e51c864:/usr/src/app# ls
__init__.py  __pycache__  main.py
root@c40a0e51c864:/usr/src/app# python main.py

 

http://localhost:8000/docs 접속하기   

완료!


이후, llama와 flutter로 chatbot 만들기!

 

https://doraemin.tistory.com/121

 

[llama] Flutter로 chatBot App 만들기

STEP 0. 백엔드 준비 (이전 글 참조)https://doraemin.tistory.com/120 [Docker] FastAPI container 설치 및 실행 with llama도커 이미지 빌드 & 테스트# 0. 먼저, Docker Desktop 앱 실행하자.# Docker Desktop이 설치 안 되어있다

doraemin.tistory.com


참고 문서 ; 이경용 대표님 강의자료

OpenAI - ChatGPT

Llama 모델 챗봇 앱 만들기

라마 모델 사용하기 https://cafe.naver.com/aiclubcafe/823

라마 모델과 FastAPI와 연동하기 https://cafe.naver.com/aiclubcafe/943

라마 연동 플러터 챗봇 앱 만들기 https://cafe.naver.com/aiclubcafe/945

플러터 STT, TTS 라이브러리 활용해서 음성 챗봇 만들기 https://cafe.naver.com/aiclubcafe/946