简体   繁体   中英

Docker image with Sanic over alpine:latest(3.9) uses python json instead of ujson

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?

try to install ujson from git master source

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM