Add support for building and running a Docker image

This commit is contained in:
Paul van Tilburg 2023-01-10 16:09:56 +01:00
parent 669c9285ad
commit 5ed688f0fb
Signed by: paul
GPG Key ID: C6DE073EDA9EEC4D
3 changed files with 87 additions and 0 deletions

15
.dockerignore Normal file
View File

@ -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*

47
Dockerfile Normal file
View File

@ -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" ]

25
docker-compose.yml Normal file
View File

@ -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'