Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dockerfile for Stats #1

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

pan93412
Copy link

@pan93412 pan93412 commented Mar 3, 2024

This PR adds the Dockerfile for Stats that simplifies the deployment. The layer caching has been optimized.

Note that this is in WIP: I am still working on the server listening issues (it did not respond to any requests)

@pan93412 pan93412 marked this pull request as ready for review March 3, 2024 12:04
@pan93412 pan93412 marked this pull request as draft March 3, 2024 12:04
@ColeTownsend
Copy link

@pan93412 here's what it took for me to deploy it on fly.io

Dockerfile

# Use cargo-chef to cache dependencies, speeding up builds significantly
FROM lukemathwalker/cargo-chef:latest-rust-1 AS chef
WORKDIR /app

FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

FROM chef AS builder
COPY --from=planner /app/recipe.json recipe.json
# Build dependencies first to cache them
RUN cargo chef cook --release --recipe-path recipe.json
# Build the actual application
COPY . .
RUN cargo build --release --bin stats

# Build diesel CLI for running migrations
FROM rust AS diesel-builder
RUN apt update && \
  apt install -y libsqlite3-dev && \
  rm -rf /var/lib/apt/lists/*
WORKDIR /app

RUN cargo install diesel_cli --no-default-features --features sqlite --root /app

# The final image starts here
FROM debian:bookworm-slim AS runtime
WORKDIR /app

# Environment variables for your application
ENV APP_URL=http://127.0.0.1:8080
ENV SERVICE_PORT=8080
ENV DATABASE_URL=/app/data/stats.sqlite
ENV PROCESSING_BATCH_SIZE=500
ENV CORS_DOMAINS=http://localhost:5775,https://www.twnsnd.co


RUN apt update && \
  apt install -y libsqlite3-0 && \
  rm -rf /var/lib/apt/lists/*

# Copy necessary files to /data
WORKDIR /app

# Add GeoLite2-City.mmdb and cities5000.txt directly from the web sources
ADD https://git.io/GeoLite2-City.mmdb /app/data/GeoLite2-City.mmdb
ADD https://github.com/PrismaPhonic/filter-cities-by-country/raw/master/cities5000.txt /app/data/cities5000.txt

# Copy necessary files and binaries from previous stages
COPY migrations/ /app/migrations
COPY ui/ /app/ui
COPY --from=diesel-builder /app/bin/diesel /app
COPY --from=builder /app/target/release/stats /app
COPY --chmod=0755 docker-entrypoint.sh /app

# Copy your entrypoint script into the image and make sure it's executable
COPY docker-entrypoint.sh /app
RUN chmod +x /app/docker-entrypoint.sh

# Set the PATH to include the app directory
ENV PATH="/app:${PATH}"

ENTRYPOINT ["/app/docker-entrypoint.sh"]

Main changes here are using a standard 8080 which happens to be what Fly wants.

Data

fly volumes create data_volume --size 2 --region iad

Fly Config (Optional)

app = 'my-app- name'
primary_region = 'iad'

[http_service]
  internal_port = 8080
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 0
  processes = ['app']

[[vm]]
  memory = '1gb'
  cpu_kind = 'shared'
  cpus = 1

[[mounts]]
source = "data_volume"
destination = "/app/data"

@pan93412
Copy link
Author

pan93412 commented Mar 17, 2024

@pan93412 here's what it took for me to deploy it on fly.io

CleanShot 2024-03-17 at 15 22 36@2x

CleanShot 2024-03-17 at 15 21 04@2x

Not sure why my Stats can't work with this dockerfile. @UdaraJay could you help me take a look with this?

@DMcConnell
Copy link

Hi @pan93412 , I was running into the same issue when trying to run the app in the official rust image. I was able to fix it by rebinding the server to 0.0.0.0 instead of 127.0.0.1 here

@pan93412
Copy link
Author

Hi @pan93412 , I was running into the same issue when trying to run the app in the official rust image. I was able to fix it by rebinding the server to 0.0.0.0 instead of 127.0.0.1 here

Good investigation! Let me PR a HOST change later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants