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?

magic_turn
  • 25
  • 7
  • That's weird, Sanic depends on ujson. Line 82: https://github.com/huge-success/sanic/blob/master/setup.py. Not sure whether sanic falls back to json if it didn't find ujson though.. – Charming Robot Mar 23 '19 at 09:57
  • Yes, it will fall back to the regular json module if ujson is not available. But you should be able to get it running, I've not had problems on alpine. In fact, the "official" Sanic image is based on alpine. https://hub.docker.com/r/sanicframework/sanic – Adam Hopkins May 03 '19 at 14:20

1 Answers1

1

try to install ujson from git master source

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

feisan
  • 192
  • 6