2

Dockerfile:

FROM alpine:latest

RUN apk add --no-cache python3 \
    && python3 -m ensurepip \
    && rm -r /usr/lib/python*/ensurepip \
    && pip3 install -U pip setuptools ez_setup \
    && rm -r /root/.cache/* \
    && apk add --no-cache gcc autoconf python3-dev musl-dev make openssl-dev \
    && pip3 install -U sanic \
    && apk del gcc autoconf python3-dev musl-dev make openssl-dev \
    && rm -rf /var/cache/apk/* /var/tmp/* /tmp/* /root/.cache/*

WORKDIR /app
COPY . /app

EXPOSE 8000
CMD ["python3", "./app.py"]

Sanic server:

from sanic import Sanic
from sanic.response import json
from datetime import datetime as dt

app = Sanic()


@app.route("/")
async def test(request):
    return json({
        "hello": "world",
        "date_is": dt.utcnow()
    })

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000)

Running Sanic server using docker image returns 500:

TypeError: Object of type 'datetime' is not JSON serializable

but running this server app from command line works just fine.

The more interesting thing is that if Alpine linux version 3.8 is used that server app works fine with it.

I think Sanic server can not find ujson package and use default python json.

Does someone have any suggestion how to fix this?

4

1 回答 1

1

try to install ujson from git master source

https://github.com/esnme/ultrajson/issues/326

于 2019-05-03T05:58:17.337 回答