flatnotes icon indicating copy to clipboard operation
flatnotes copied to clipboard

Dockerfile build time

Open modem7 opened this issue 11 months ago • 1 comments

Heya,

I've been looking at the Dockerfile as part of #101, and I think I may have made some improvements to build speed and layer count.

# syntax = docker/dockerfile:latest

ARG BUILD_DIR=/build
ARG NODE_MAJOR=20


# Build Container
FROM --platform=$BUILDPLATFORM python:3.11-slim-bookworm AS build

ARG BUILD_DIR
ARG NODE_MAJOR

# Install NPM + Create directories
RUN <<EOF
    set -x
    apt-get update
    apt-get install -y \
            build-essential \
            ca-certificates \
            curl \
            gnupg 
    mkdir -p /etc/apt/keyrings
    mkdir -p ${BUILD_DIR}/flatnotes
    curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
    echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
    apt-get update
    apt-get install -y nodejs
EOF

WORKDIR ${BUILD_DIR}

COPY --link package.json package-lock.json .htmlnanorc ./
COPY --link flatnotes/src ./flatnotes/src

RUN <<EOF
    set -x
    npm install -g npm
    npm ci
    npm run build
EOF
 

# Runtime Container
FROM python:3.11-slim-bookworm

ARG BUILD_DIR

ENV PUID=1000 \
    PGID=1000 \
    APP_PATH=/app \
    FLATNOTES_PATH=/data

RUN <<EOF
    set -x
    apt update && apt install -y gosu
    pip install pipenv
    mkdir -p ${APP_PATH}
    mkdir -p ${FLATNOTES_PATH}
    rm -rf /var/lib/apt/lists/*
EOF

WORKDIR ${APP_PATH}

COPY --link LICENSE Pipfile Pipfile.lock ./
COPY --link flatnotes ./flatnotes
COPY --link --chmod=755 entrypoint.sh /
RUN pipenv install --deploy --ignore-pipfile --system

COPY --link --from=build ${BUILD_DIR}/flatnotes/dist ./flatnotes/dist

VOLUME /data
EXPOSE 8080/tcp

ENTRYPOINT [ "/entrypoint.sh" ]

I thought I'd put it here first for discussion prior to making a PR.

@dullage If you have any thoughts, that'd be awesome.

In my tests, it cut build time by just under half (even more so if previous image cache was leveraged (and updated to bookworm)).

The other, even faster alternative is to use the node:current-bookworm-slim image for the build stage, but then there's a loss of control of Python versions.

Cheers!

modem7 avatar Sep 06 '23 12:09 modem7