From 5ed688f0fb8259b5431f9a2c32e33885983676bb Mon Sep 17 00:00:00 2001 From: Paul van Tilburg Date: Tue, 10 Jan 2023 16:09:56 +0100 Subject: [PATCH] Add support for building and running a Docker image --- .dockerignore | 15 +++++++++++++++ Dockerfile | 47 ++++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 25 ++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e183625 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,15 @@ +# Local build and dev artifacts +target + +# Docker files +Dockerfile* +docker-compose* + +# Git folder +.git + +# Dot files +.gitignore + +# TOML files +Rocket.toml* diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d42f783 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,47 @@ +# Using multistage build: +# https://docs.docker.com/develop/develop-images/multistage-build/ +# https://whitfin.io/speeding-up-rust-docker-builds/ + +########################## BUILD IMAGE ########################## +# Rust build image to build Solar Grabber's statically compiled binary +FROM docker.io/rust:1 as builder + +# Build the dependencies first +RUN USER=root cargo new --bin /usr/src/solar-grabber +WORKDIR /usr/src/solar-grabber +COPY ./Cargo.* ./ +RUN cargo build --release +RUN rm src/*.rs + +# Add the real project files from current folder +ADD . ./ + +# Build the actual binary from the copied local files +RUN rm ./target/release/deps/solar_grabber* +RUN cargo build --release + +########################## RUNTIME IMAGE ########################## +# Create new stage with a minimal image for the actual runtime image/container +FROM docker.io/debian:bullseye-slim + +# Install CA certificates +RUN apt-get update && \ + apt-get upgrade -y && \ + apt-get install -y --no-install-recommends ca-certificates && \ + rm -rf /var/lib/apt/lists/* + +# Copy the binary from the "builder" stage to the current stage +RUN adduser --system --disabled-login --home /app --gecos "" --shell /bin/bash solar-grabber +COPY --from=builder /usr/src/solar-grabber/target/release/solar-grabber /app + +# Standard port on which Rocket launches +EXPOSE 8000 + +# Set user to www-data +USER solar-grabber + +# Set container home directory +WORKDIR /app + +# Run Solar Grabber +ENTRYPOINT [ "/app/solar-grabber" ] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..04d3755 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,25 @@ +version: '3' + +services: + server: + image: solar-grabber:latest + build: . + restart: unless-stopped + ports: + - 2399:8000 + # Use a `Rocket.toml` or configure the credentials using environment variables below + volumes: + - ./Rocket.toml:/app/Rocket.toml + environment: + ROCKET_LOG_LEVEL: normal # Available levels are: off, debug, normal, critical + # For My Autarco, use the these variabels and uncomment them + # ROCKET_KIND: MyAutarco + # ROCKET_USERNAME: foo@domain.tld + # ROCKET_PASSWORD: secret + # ROCKET_SITE_ID: abc123de + # For Hoymiles, use the these variabels and uncomment them + # ROCKET_KIND: HoyMiles + # ROCKET_USERNAME: foo@domain.tld + # ROCKET_PASSWORD: secret + # ROCKET_SID: 123456 + shm_size: '2gb'