Skip to content

Commit

Permalink
Dockerfile: build psycopg2 in separate container
Browse files Browse the repository at this point in the history
This avoids having to install development libraries
in the production container, which makes the image
~300M smaller.
  • Loading branch information
pjonsson authored and omad committed Dec 4, 2023
1 parent 16ba446 commit 88780f7
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ghcr.io/osgeo/gdal:ubuntu-small-3.8.1
FROM ghcr.io/osgeo/gdal:ubuntu-small-3.8.1 as builder

ENV DEBIAN_FRONTEND=noninteractive \
LC_ALL=C.UTF-8 \
Expand All @@ -11,32 +11,48 @@ RUN apt-get update && \
apt-get install -y --no-install-recommends \
build-essential \
git \
vim \
nano \
# For Psycopg2
libpq-dev \
python3-dev \
python3-pip

WORKDIR /build

RUN python3.10 -m pip --disable-pip-version-check -q wheel --no-binary psycopg2 psycopg2

FROM ghcr.io/osgeo/gdal:ubuntu-small-3.8.1

# Apt installation
RUN apt-get update && \
apt-get upgrade -y && \
apt-get install -y --no-install-recommends \
git \
# For Psycopg2
libpq5 \
tini \
wget \
postgresql-client \
python3-pip \
# For Psycopg2
libpq-dev python3-dev \
&& apt-get autoclean && \
apt-get autoremove && \
rm -rf /var/lib/{apt,dpkg,cache,log}


# Environment can be whatever is supported by setup.py
# so, either deployment, test
ARG ENVIRONMENT=deployment
# ARG ENVIRONMENT=test

RUN echo "Environment is: $ENVIRONMENT" && \
pip install pip-tools pytest-cov
[ "$ENVIRONMENT" = "deployment" ] || pip install pip-tools pytest-cov

# Set up a nice workdir and add the live code
ENV APPDIR=/code
WORKDIR $APPDIR
COPY . $APPDIR

COPY --from=builder --link /build/*.whl ./
RUN python3.10 -m pip --disable-pip-version-check -q install *.whl && \
rm *.whl

# These ENVIRONMENT flags make this a bit complex, but basically, if we are in dev
# then we want to link the source (with the -e flag) and if we're in prod, we
# want to delete the stuff in the /code folder to keep it simple.
Expand Down

0 comments on commit 88780f7

Please sign in to comment.