localhost의 MySQL DB를 Docker 컨테이너의 FastAPI 애플리케이션에서 사용하고 싶습니다.
본문
안녕하세요. 그누보드(6.0.7)사용자 입니다.
'FastAPI 애플리케이션을 자유롭게 사용하고자'하던 중 조언(https://sir.kr/qa/536846)을 듣고,
그누보드6을 컨테이너에 담아 보려는데, localhost의 MySQL DB를 Docker 컨테이너의
FastAPI 애플리케이션(그누보드6)에서는 사용할 수가 없군요.
>>> DB를 읽지 못하고(http://w.glitter.kr/) 새로이 만든 DB를 연결하여도
이런 에러(https://policy.glitter.kr/qa_php/db-error-3.png)가 발생합니다.
여러모로 시도하였으나, 사용하는 방법을 찾지 못 하고 이렇게 조언을 구합니다.
=시스템 정보 =
1 . localhost
root@HumanpcNAS:~# uname && uname -r
Linux 6.1.20-efm-standard
.
root@HumanpcNAS:~# python3 -V
Python 3.9.9
.
root@HumanpcNAS:~# cat requirements.txt
fastapi>=0.111.0
.
root@HumanpcNAS:~# httpd -v
Server version: Apache/2.4.52 (Unix) .
2. Docker / Container
root@HumanpcNAS:/mnt/VOL1/Docker# docker --version
Docker version 20.10.8-ce, build 62eae52c2a
.
Python 3.11.2
.
fastapi>=0.111.0
.
일반인이 접하기엔 어려움이 있어 조언을 구합니다.
Dockerfile
# Use Python 3.11.2 base image
FROM python:3.11.2-slim
# Install necessary packages
COPY requirements.txt /
RUN pip install --no-cache-dir -r /requirements.txt
# Copy application code and Gnuboard source files
COPY . /
# Run FastAPI application
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
docker-compose.yml
version: '3.8'
services:
app:
build: .
container_name: glitter
ports:
- "8000:8000"
volumes:
- .:/app
environment:
- PYTHONUNBUFFERED=1
감사합니다.
!-->!-->!-->!-->
답변 4
웹서버는 연결이 되어있습니다.
데이타베이스 연결이 않되니 확인해보시기바랍니다.
데이타베이스 포트 가열려있는지 포트가 보안에 오픈되어잇는지 확인해보시기바랍니다.
localhost 대신 glitter 이것을 넣어야 하는것 아닌가요?
테스터 하는 중에 막으셔서 못해봤어요..
일 주일을 헤매다, 답을 찾았다.
★★★ Localhost에 Docker 컨테이너 이미지로 만들 수 없는 Host들이 있어,
MySQL DB를 컨테이너 이미지로 만들 수 없는 상황이라는 점이 문제였다. ★★★
그누보드6_컨테이너가 호스트(MySQL DB)의 네트워크와 동일한 네트워크 스택을 공유하게 하여야,
Localhost의 MySQL DB를 인식하고 사용할 수 있었다. ㅠ ㅠ ㅠ ㅠ
※ ※ ※ Docker 컨테이너 이미지(그누보드6)를 만들기 전에 ~ ※ ※ ※
!. 그누보드6을 사용자가 특정한 디렉터리(/mnt/VOL1/base2)에 설치 완료 한다.
!. 보드초기설치시; MySQL DB의 "DB_사용자:사용자_비번@localhost:3306/DB_이름" 설정을 메모.
!. 그누보드6 사용중에 추가된 모듈(패키지)를 메모.
!. Docker 서비스 정상 작동 확인 -
sudo systemctl daemon-reload
sudo systemctl status docker
Dockerfile
# Use Python base image
FROM python:3.12.4
# Set the working directory
WORKDIR /mnt/VOL1/base2
# Copy the requirements file into the container
COPY requirements.txt .
# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the application code into the container
COPY . .
# Expose the port the app runs on
EXPOSE 8000
# Command to run the application
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
-그누보드6 작업 디렉터리(/mnt/VOL1/base2) = 그누보드6 소스파일
docker-compose.yml
version: '3.8'
services:
m_glitter:
container_name: con_m_glitter
build:
context: .
environment:
- DATABASE_URL=mysql+pymysql://DB_사용자:사용자_비번@localhost:3306/DB_이름
network_mode: host # Use host network mode
volumes:
- /mnt/VOL1/base2:/mnt/VOL1/base2
- /mnt/VOL1/hwi:/mnt/VOL1/hwi
※ 그누6설치시 DB의 - "DB_이름", "호스트명(localhost)", "DB_사용자", "사용자_비번" ;
"DATABASE_URL=mysql+pymysql://DB_사용자:사용자_비번@localhost:3306/DB_이름" ,
이 내용과 일치하게 작성한다.
위의 두 파일과 그누보드6 소스파일 Root에 있는 requirements.txt를
자신의 컨테이너_디렉터리로 옮긴다.
cd <자신의 컨테이너_디렉터리>
사용자 모듈(패키지)을 requirements.txt에 추가한다. (그누보드6이 설치 되었다는 상황임)
글쓴이는 aiohttp==3.9.5 등을 추가하였다.
root@HumanpcNAS:/mnt/VOL1/Docker/m_glitter# vi requirements.txt
aiohttp==3.9.5
.
fastapi>=0.111.0
docker-compose up -d --build
. . .
컨테이너를 구성하는 명령어들은 생략한다.
호스트의 디렉터리(/mnt/VOL1/base2, /mnt/VOL1/hwi)를 컨테이너에
같은 디렉터리(/mnt/VOL1/base2, /mnt/VOL1/hwi)로 마운트하여 기존의 코딩 내용을 유지하였다.
>> 디렉터리(/mnt/VOL1/hwi)는 이 localhost내의 모든 host들이 공유하는 디렉터리로,
robots.txt, site_map.xml, image, css, 등의 stasic 파일들을 구조적으로 관리하는 폴더.
stasic 파일들을 한적한 곳에 두어, Upgrade시 편리함 등이 있음.
알고 보면 가벼운데, 몇 일을 고생하고 돌아 보니 ㅠ ㅠ.
+++ Docker 기본 설정이 본인의 시스템에 잘 어울리게 구성한 후, 시도하시는 것이 ~~ +++
※ 초보의 심정으로 ~~ 필자의 디렉터리 구조이다.
│
/mnt/VOL1/base2/ ( 그누보드6 설치 디렉터리 )
│ ├── .env
│ ├── . . .
│ └── main.py
│
/mnt/VOL1/Docker/ ( 도커설치 디렉터리 )
│ ├── m_glitter/ ( 그누보드6 컨테이너 )
│ │ ├── alembic/
│ │ ├── alembic.ini
│ │ ├── Dockerfile
│ │ ├── docker-compose.yml
│ │ └── requirements.txt
│ ├── network/
│ ├── overlay2/
│ └── network/
│
/mnt/VOL1/hwi/ ( static 파일 디렉터리 )
│ ├── gimg/
│ │ ├── img/
│ │ └── favicon/
│ ├── css/
│ │ ├── glitter.css
│ │ ├── m.glitter_default.css
│ │ ├── . . .
│ │ └── policy.glitter_default.css
│ ├── robots.txt
│ ├── m.robots.txt
│ ├── policy.robots.txt
│ ├── . . .
│ ├── glitter_sitemap.xml
│ ├── m.glitter_sitemap.xml
│ ├── policy.glitter_sitemap.xml
│ └── site_map.xml
│