varabyte / kobweb

A modern framework for full stack web apps in Kotlin, built upon Compose HTML
https://kobweb.varabyte.com
Apache License 2.0
1.53k stars 68 forks source link

Create `varabyte/kobweb` docker image #99

Open bitspittle opened 2 years ago

bitspittle commented 2 years ago

Currently, a full docker file looks something like:

FROM debian:stable-slim
USER root

# Copy the project code to app dir
COPY . /app

# Install OpenJDK-11 (earliest JDK kobweb can run on)
RUN apt-get update \
    && apt-get install -y openjdk-11-jdk \
    && apt-get install -y ant \
    && apt-get clean

# Fix certificate issues
RUN apt-get update \
    && apt-get install ca-certificates-java \
    && apt-get clean \
    && update-ca-certificates -f

# Setup JAVA_HOME -- needed by kobweb / gradle
ENV JAVA_HOME /usr/lib/jvm/java-11-openjdk-amd64/
RUN export JAVA_HOME
RUN java -version

# Add Chrome (for export)
RUN apt-get update \
    && apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    --no-install-recommends \
    && curl -sSL https://dl.google.com/linux/linux_signing_key.pub | apt-key add - \
    && echo "deb https://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list \
    && apt-get update && apt-get install -y \
    google-chrome-stable \
    fontconfig \
    fonts-ipafont-gothic \
    fonts-wqy-zenhei \
    fonts-thai-tlwg \
    fonts-kacst \
    fonts-symbola \
    fonts-noto \
    fonts-freefont-ttf \
    --no-install-recommends

# Install kobweb
RUN apt-get update && apt-get install -y wget unzip

RUN wget https://github.com/varabyte/kobweb/releases/download/cli-v0.9.4/kobweb-0.9.4.zip \
    && unzip kobweb-0.9.4.zip \
    && rm -r kobweb-0.9.4.zip
ENV PATH="/kobweb-0.9.4/bin:${PATH}"

WORKDIR /app

RUN kobweb export --mode dumb

RUN export PORT=$(kobweb conf server.port)
EXPOSE $PORT

# Purge all the things we don't need anymore

RUN apt-get purge --auto-remove -y curl gnupg wget unzip \
    && rm -rf /var/lib/apt/lists/*

# Keep container running because `kobweb run --mode dumb` doesn't block
CMD kobweb run --mode dumb --env prod && tail -f /dev/null

But it would be much nicer if you could just do:

FROM varabyte:kobweb
USER root

with minimal fuss and it would just work

bitspittle commented 2 years ago

(Still want to do this at some point but it's reduced priority for now because static layouts and docs are probably what people need more right now)

Xaseron commented 2 years ago

IMHO with the chrome dependency it is not a feasible idea to have a single base image. I would suggest splitting it into two base images a builder and a runner and use multi-stage build. You can see this in my PR https://github.com/varabyte/kobweb-site/pull/9

A slim Dockerfile could look like this.

FROM varabyte/kobweb-builder as builder
USER root

COPY . /app
WORKDIR /app

RUN kobweb export --mode dumb

FROM varabyte/kobweb-runner
USER root

COPY --from=builder /app /app
WORKDIR /app

ENV PORT=8080
EXPOSE $PORT

# Keep container running because `kobweb run --mode dumb` doesn't block
CMD kobweb run --mode dumb --env prod && tail -f /dev/null

Would it be possible to replace the kobweb export chrome with https://github.com/skrapeit/skrape.it#scrape-a-client-side-rendered-page ?

bitspittle commented 2 years ago

Hey, thank you so much for this comment! I'll look at your PR on Monday.

I'm honestly a Docker beginner so if you don't mind, I may reach out to you with more questions if I have them.

As an aside, I found myself getting charged by GCP way more than I expected (~$15 / month without any traffic), so I have since pivoted to using static hosting for now, and postponing server work until I am absolutely ready for it. As a result, I've personally deprioritized Docker for the moment (kobweb-site itself has been updated to use Firebase, although I left the Docker file there for possible future use). If you had an urgent usecase for Docker sooner than, say, a couple of months in the future, please let me know.

bitspittle commented 2 years ago

Re: the comment about replacing kobweb export chrome, I'm never against updating to a better solution. If I could remove the hard Chrome dependency I'd be ecstatic.

I checked https://docs.skrape.it/docs/dsl/extract-client-side-rendered-data and it looks like it uses Chrome itself under the hood, but emulated? To me, this either means it still requires Chrome on your system to use it, OR it has some pretty hefty dependencies that might vastly balloon the size of the kobweb Gradle plugin.

I'll create a new issue to track experimenting with this.

Edit: Created at #145

bitspittle commented 1 year ago

In practice, most people are using Kobweb + static layouts, which doesn't even need a custom server.

It would still be great to create a docker image for Kobweb but it might not be as high a priority as I originally intended, at a time when I assumed most people would be running their own kobweb servers.