IT/ServerSide
docker fastapi m1 맥북 에어
안선생 Dr.Ahn
2024. 6. 8. 22:20
728x90
반응형
1. main.py 파일 생성
2. requirements.txt 파일 생성
내용:
uvicorn
fastapi
3. Dockerfile 생성
FROM python:latest
WORKDIR /app/
COPY ./main.py /app/
COPY ./requirements.txt /app/
RUN pip install -r requirements.txt
CMD uvicorn --host=0.0.0.0 --port 8000 main:app
4. 3번 위치에서 다음 명령어 실행
% docker build --tag fastapi:0.1 .
- 중간에 에러생겼는데 requirements 파일 이름 틀린거였음. 이름 확인할 것.
---->
What's Next?
View a summary of image vulnerabilities and recommendations → docker scout quickview
라는게 출력됨.
- 궁금하니까
% docker scout quickview
실행해봄
i New version 1.9.3 available (installed version is 1.8.0) at https://github.com/docker/scout-cli
⠹ Storing image for indexing
하면서 빙글빙글빙글
i New version 1.9.3 available (installed version is 1.8.0) at https://github.com/docker/scout-cli
✓ Image stored for indexing
✓ Indexed 608 packages
Target │ local://fastapi:0.1 │ 1C 6H 3M 114L 2?
digest │ aa79780239fd │
Base image │ python:3 │ 1C 6H 3M 114L 2?
Updated base image │ python:3-slim │ 0C 1H 0M 28L
│ │ -1 -5 -3 -86 -2
What's next:
View vulnerabilities → docker scout cves local://fastapi:0.1
View base image update recommendations → docker scout recommendations local://fastapi:0.1
Include policy results in your quickview by supplying an organization → docker scout quickview local://fastapi:0.1 --org <organization>
..그렇다고 한다.
5. 포트 지정
Docker Container 생성
Dockerfile의 정의를 보면 기본 포트는 8000.
Container를 실행 할 때 Port 포워딩을 통해 원하는 포트를 지정하여 실행 할 수 있.
> docker run --rm -p 8080:8000 fastapi:0.1
INFO: Started server process [8]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
--rm : 이미지가 stop 되면 자동으로 Container가 삭제 됩니다.
-p 8080:8000 : FastAPI의 기본 포트인 8000과 자신의 로컬 포트 8080을 포워딩 합니다.
*ref. 퍼옴
8080포트와 8000포트 포워딩
% docker run --rm -p 8080:8000 fastapi:0.1
INFO: Started server process [8]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
6. localhost:8080
["hello root"]
7. http://localhost:8080/world
["hello world"]
INFO: 192.168.65.1:54849 - "GET / HTTP/1.1" 200 OK
INFO: 192.168.65.1:54849 - "GET /favicon.ico HTTP/1.1" 404 Not Found
INFO: 192.168.65.1:53940 - "GET /world HTTP/1.1" 200 OK
404는 왜떴지?
아무튼 localhost는 잘 떴다.
- 근데 왜 안꺼지냐. control + z 로 끈다음 ps -a 로 pid 확인하고 kill -9 [pid] 로 강제 킬함.
8. docker-compose
여러 컨테이너를 하나의 파일로 관리해서 띄우기 위해.
도커 이미지 생성 및 실행 자동화 가능.
docker-compose.yml 파일 생성
services:
fastapi:
build:
context: ..
dockerfile : ./Dockerfile
ports:
- 8080:8000
#build위치는 dockerfile 있는 폴더의 하위폴더이므로 context 를 ..로 지정함
9. 명령어
% docker-compose up
뭐 아무튼 잘 되는것 같다. 뭔지 모르겠지만..
https://jandari91.github.io/posts/fastapi-docker-build/
FastAPI Docker 이미지 제작
소개
jandari91.github.io
728x90
반응형