Skip to content

Instantly share code, notes, and snippets.

@simonw
Last active October 16, 2024 13:35
Show Gist options
  • Save simonw/ee63bc5e7feb6e8bb3af82f67a7a36fe to your computer and use it in GitHub Desktop.
Save simonw/ee63bc5e7feb6e8bb3af82f67a7a36fe to your computer and use it in GitHub Desktop.
The Dockerfile used by the new Datasette Publish to generate images that are smaller than 100MB
FROM python:3.6-slim-stretch as csvbuilder
# This one uses csvs-to-sqlite to compile the DB, and then uses datasette
# inspect to generate inspect-data.json Compiling pandas takes way too long
# under alpine so we use slim-stretch for this one instead.
RUN apt-get update && apt-get install -y python3-dev gcc
COPY *.csv csvs/
RUN pip install csvs-to-sqlite datasette
RUN csvs-to-sqlite csvs/names.csv data.db -f "name" -c "legislature" -c "country"
ADD metadata.json metadata.json
RUN datasette inspect data.db --inspect-file inspect-data.json
########
FROM python:3.6-alpine as buildit
# This one installs and compiles Datasette + its dependencies
RUN apk add --no-cache gcc python3-dev musl-dev alpine-sdk
RUN pip install uvloop
RUN pip install datasette
# Plugins go here:
RUN pip install datasette-vega==0.3
# We clean up a lot of space by deleting rogue .c files etc:
RUN find /usr/local/lib/python3.6 -name '*.c' -delete
RUN find /usr/local/lib/python3.6 -name '*.pxd' -delete
RUN find /usr/local/lib/python3.6 -name '*.pyd' -delete
# Cleaning up __pycache__ gains more space
RUN find /usr/local/lib/python3.6 -name '__pycache__' | xargs rm -r
########
FROM python:3.6-alpine
# This one builds the final container, copying from the previous steps
COPY --from=buildit /usr/local/lib/python3.6 /usr/local/lib/python3.6
COPY --from=buildit /usr/local/bin/datasette /usr/local/bin/datasette
COPY --from=csvbuilder inspect-data.json inspect-data.json
COPY --from=csvbuilder data.db data.db
EXPOSE 8001
CMD ["datasette", "serve", "data.db", "--host", "0.0.0.0", "--cors", "--inspect-file", "inspect-data.json"]
@simonw
Copy link
Author

simonw commented Nov 19, 2018

Here's the previous, much less efficient Dockerfile https://gist.github.com/simonw/365294fb51765fb07bc99fe5eb7fee22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment