2003

[WEB] flask 프로젝트를 docker 컨테이너화 하기 본문

개발

[WEB] flask 프로젝트를 docker 컨테이너화 하기

saya. 2024. 2. 13. 15:51

DOCKER 컨테이너화

 

1. requirement.txt 생성

pip freeze > requirements.txt

 

2. Dockerfile 작성

# Base image
FROM python:3.12

# Set working directory
WORKDIR /[working directory]

# Copy requirements.txt file
COPY requirements.txt .

# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy application files
COPY . .

# Set environment variables
ENV FLASK_APP=app.py

# Expose port
EXPOSE 5000

# Run the application
CMD ["python", "app.py"]

 

3. app.py 수정

app = Flask(__name__)
host_addr = "0.0.0.0"
host_port = 5000

###

if __name__ == "__main__":
    app.run(host=host_addr,port=host_port)

 

4. docker image build

docker build -t [이미지 이름] .

 

5. docker 컨테이너 실행

docker run -it -p 5000:5000 [이미지 이름]

 

6. 해당 URL로 드가서 잘 실행되는지 확인하기