I'm starting to study docker and I'm trying setup a docker app (python + flask + gunicorn) which sends logs to a graylog server. As the client, I'm using the graypy python lib.
When I use only flask+gunicorn, I can successfully send the log (my_logger.debug('Hello Graylog2.')
to the graylog local server. But when I use docker to containerize my application, only the following gunicorn initialization is sendo do graylog:
[2021-02-12 21:48:40 +0000] [7] [INFO] Starting gunicorn 20.0.4
[2021-02-12 21:48:40 +0000] [7] [INFO] Listening at: http://0.0.0.0:8080 (7)
[2021-02-12 21:48:40 +0000] [7] [INFO] Using worker: threads
[2021-02-12 21:48:40 +0000] [10] [INFO] Booting worker with pid: 10
[2021-02-12 21:48:40 +0000] [11] [INFO] Booting worker with pid: 11
My app.py
...
import graypy
my_logger = logging.getLogger('my_logger')
my_logger.setLevel(logging.DEBUG)
handler = graypy.GELFUDPHandler('localhost', 12201)
my_logger.addHandler(handler)
my_logger.debug('Hello Graylog2.')
def create_app():
app = Flask(__name__)
@app.route('/alive', methods=['GET'])
def alive():
my_logger.debug('alive')
return jsonify({
"msg":"alive"
})
return app
app = create_app()
if __name__ == "__main__":
app.run(port=8080, host='0.0.0.0', processes=1, threaded=True)
...
Dockerfile
FROM python:3.7.9-slim
COPY requirements.txt /
RUN python -m pip install -r /requirements.txt
ARG FLASK_ENV="production"
ENV FLASK_ENV="${FLASK_ENV}" \
PYTHONUNBUFFERED=1 \
PYTHONIOENCODING=UTF-8
COPY . /app
WORKDIR /app
EXPOSE 8080 12201
CMD gunicorn -b 0.0.0.0:8080 --timeout 3600 --workers=2 --threads=2 'app:app' --preload
To run docker
sudo docker run -it -p 8080:8080 --log-driver=gelf --log-opt gelf-address=udp://127.0.0.1:12201 image_app
How can I use graypy with docker? (the graypy successfully send the log do gralog server when I use only gunicor+flask, without docker)
Docker is able to send logs from its standart output into graylog using gelf logger:
https://docs.docker.com/config/containers/logging/gelf/
When you dockerize your app, your app's localhost is not localhost of host machine. Simply remove all your fancy gelf-logger-libs from your python-code and put logs to stdout. Then docker will catch your logs and will send it to the graylog host.