Docker로 작성한 Flask 서버, Debug모드 적용하기 Applying Debug Mode to Flask Server in Docker
When running a server deployed in Docker in debug mode, you have to go inside the Docker container to modify the code, which is cumbersome. Files modified inside Docker are lost when the container is stopped or removed, and they don’t sync with your local git repository.
Applying Debug Mode to Docker Server
- Let’s write a simple Flask server using Dockerfile.
Creating a Flask Server
As we’ve covered before, let’s set up a virtual environment and install the necessary packages to start the Flask server.
.env
FLASK_ENV=development
FLASK_DEBUG=True
FLASK_APP=app.py
FLASK_RUN_PORT=5000
app.py
from flask import Flask
app = Flask(__name__)
@app.route('/',methods=['GET'])
def index():
return "hi!"
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000)
Dockerfile
FROM python:3
ADD . /www
WORKDIR /www
RUN python3 -m pip install -U pip
RUN pip3 install flask
RUN pip3 install python-dotenv
CMD ["python3", "-m", "flask", "run", "--host=0.0.0.0"]
Docker Run & Build
$ docker build -t flask_dev -f Dockerfile . && docker run -p 5000:5000 flask_dev
The Dockerfile is created and running, so it works as a server!
However, no matter how much you modify your Flask code, the Docker container doesn’t reflect the changes.
Let’s Add Options to Docker
There are two methods: adding options to the Docker Run command or modifying docker-compose.yml.
Method 1 - Modify the Dockerfile Run Command
$ docker build -t flask_dev -f Dockerfile . && docker run -p 5000:5000 --volume=$(pwd):/www flask_dev
Method 2 - Using docker-compose.yml
version: '3'
services:
flask_dev:
build:
context: .
dockerfile: Dockerfile
env_file:
- .env
ports:
- 5000:5000
volumes:
- ./:/www
$ docker-compose up --build
Now when you modify the source files, you can see the server running in Docker reload every time changes are made.
I used a Flask server for simplicity, but this can be applied to Django, Spring, Node, and many other frameworks.
댓글남기기