Skip to content

Instantly share code, notes, and snippets.

@hopsoft
Created September 7, 2023 14:36
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hopsoft/9a0bf00be2816cbe036fae5aa3d85b73 to your computer and use it in GitHub Desktop.
Save hopsoft/9a0bf00be2816cbe036fae5aa3d85b73 to your computer and use it in GitHub Desktop.
Ruby + SQLite Dockerfile
FROM ruby:3.2.2-alpine
# ============================================================================================================
# Install system packages
# ============================================================================================================
RUN apk add --no-cache --update \
bash \
build-base \
curl \
gcompat \
libc6-compat \
libsass \
linux-headers \
sqlite-dev \
tzdata \
vim
# setup directories for external mounted volumes
RUN mkdir -p /mnt/external/bundle /mnt/external/databases
ENV GEM_HOME="/mnt/external/bundle"
RUN gem update --system && gem install bundler
# ============================================================================================================
# Compile and install sqlite3 (for performance turning and build customizations)
# SEE: https://www.sqlite.org/compile.html
# NOTE: The sqlite3 Ruby gem will not work with the following compile time flags
# * -DSQLITE_OMIT_DEPRECATED
# ============================================================================================================
# download and extract
RUN curl --remote-name --remote-header-name https://www.sqlite.org/2023/sqlite-amalgamation-3410200.zip && unzip sqlite-amalgamation-3410200.zip
WORKDIR /sqlite-amalgamation-3410200
ENV CFLAGS="\
-DSQLITE_DEFAULT_MEMSTATUS=0 \
-DSQLITE_DEFAULT_PAGE_SIZE=16384 \
-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \
-DSQLITE_DQS=0 \
-DSQLITE_ENABLE_FTS5 \
-DSQLITE_LIKE_DOESNT_MATCH_BLOBS \
-DSQLITE_MAX_EXPR_DEPTH=0 \
-DSQLITE_OMIT_PROGRESS_CALLBACK \
-DSQLITE_OMIT_SHARED_CACHE \
-DSQLITE_USE_ALLOCA"
# compile the executable
RUN gcc $CFLAGS shell.c sqlite3.c -lpthread -ldl -lm -o /usr/local/bin/sqlite3
# compile and setup shared library
RUN gcc $CFLAGS shell.c sqlite3.c -lpthread -ldl -lm -fPIC -shared -o /usr/local/lib/libsqlite3.so && \
chmod 755 /usr/local/lib/libsqlite3.so && \
ln -s /usr/local/lib/libsqlite3.so /usr/local/lib/libsqlite3.so.0
# copy header/include files
RUN mkdir -p /usr/local/include/sqlite3 && \
cp sqlite3.h /usr/local/include/sqlite3/ && \
cp sqlite3ext.h /usr/local/include/sqlite3/
# configure bundler to be aware of the custom sqlite3 installation
ENV BUILD_SQLITE3="true"
RUN bundle config set --global build.sqlite3 --enable-system-libraries --with-sqlite3-include=/usr/local/include/sqlite3 --with-sqlite3-lib=/usr/local/lib
# cleanup
ENV CFLAGS=""
RUN rm -rf /sqlite-amalgamation-3410200 /sqlite-amalgamation-3410200.zip
# ============================================================================================================
# Application and Deployment setup
# ============================================================================================================
COPY . /app
WORKDIR /app
# default command that runs when the container is started
CMD /app/bin/docker/run
@flavorjones
Copy link

An alternative that I think might be simpler: you can set your CFLAGS environment variable to contain the -DSQLITE_* flags at gem installation time and they will be built into the gem's sqlite:

$ CFLAGS=-DSQLITE_DEFAULT_PAGE_SIZE=16300 gem install sqlite3 --platform=ruby
Fetching sqlite3-1.6.4.gem
Building native extensions. This could take a while...
Successfully installed sqlite3-1.6.4
1 gem installed

$ ruby -rsqlite3 -e 'puts SQLite3::Database.new(":memory:").execute("pragma page_size")'
16300

@flavorjones
Copy link

See upstream feature request: sparklemotion/sqlite3-ruby#401

@hopsoft
Copy link
Author

hopsoft commented Sep 10, 2023

This is great @flavorjones. Thank you for sharing.

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