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

ref(docker): use a single Dockerfile with multistage #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
.git/*
.dockerignore
.gitignore
**Dockerfile
**.Dockerfile

/target/

*db*
cache/
**/.DS_Store
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/charts
**/docker-compose*
**/compose.y*ml
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/secrets.dev.yaml
**/values.dev.yaml
/bin
/target
LICENSE
README.md
8 changes: 7 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
DATABASE_URL="sqlite://prisma/test.db"
POSTGRES_DB=mydb
POSTGRES_USER=myuser
POSTGRES_PASSWORD=mypassword
DATABASE_URL="postgres://myuser:mypassword@db:5432/mydb"
ETH_RPC_URL=""
OPT_RPC_URL=""
SQLX_OFFLINE=true
117 changes: 92 additions & 25 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,25 +1,92 @@
FROM lukemathwalker/cargo-chef:latest-rust-1.78 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
RUN apt-get update && apt-get install -y libclang-dev
# Build dependencies (this is the caching Docker layer)
RUN cargo chef cook --release --recipe-path recipe.json
# Build application
COPY . .
RUN cargo build --release --bin indexers

# You do not need the Rust toolchain to run the binary!
FROM ubuntu:24.04 AS runtime
RUN apt-get update && \
apt-get upgrade -y && \
apt-get install ca-certificates -y
WORKDIR /app
COPY --from=builder /app/config.json ./config.json
COPY --from=builder /app/target/release/indexers /usr/local/bin
CMD ["/usr/local/bin/indexers"]
# syntax=docker/dockerfile:1

ARG RUST_VERSION=1.81.0
ARG APP_HOME=/app

FROM rust:${RUST_VERSION}-bookworm AS build

ARG APP_HOME
WORKDIR ${APP_HOME}

# Install host build dependencies.
RUN apt-get -qq update && \
apt-get -qq install -y --no-install-recommends \
libclang-dev \
ca-certificates \
&& rm -rf /var/lib/apt/lists/* /tmp/*

# Build arguments and variables set for tracelog levels and debug information
#
# We set defaults to all variables.
ARG RUST_LOG
ENV RUST_LOG=${RUST_LOG:-info}

ARG RUST_BACKTRACE
ENV RUST_BACKTRACE=${RUST_BACKTRACE:-1}

ARG RUST_LIB_BACKTRACE
ENV RUST_LIB_BACKTRACE=${RUST_LIB_BACKTRACE:-1}

ENV CARGO_HOME="${APP_HOME}/.cargo/"

# Application specific environment variables.
ARG SQLX_OFFLINE=true
ENV SQLX_OFFLINE=${SQLX_OFFLINE}
ARG ETH_RPC_URL
ENV ETH_RPC_URL=${ETH_RPC_URL}
ARG OPT_RPC_URL
ENV OPT_RPC_URL=${OPT_RPC_URL}

# Build the application leveraging cache mounts to speed up the build process.
RUN --mount=type=bind,source=src,target=src \
--mount=type=bind,source=abis,target=abis \
--mount=type=bind,source=.sqlx,target=.sqlx \
--mount=type=bind,source=Cargo.toml,target=Cargo.toml \
--mount=type=bind,source=Cargo.lock,target=Cargo.lock \
--mount=type=bind,source=config.json,target=config.json \
--mount=type=cache,target=/app/target/ \
--mount=type=cache,target=/usr/local/cargo/git/db \
--mount=type=cache,target=/usr/local/cargo/registry/ \
cargo build --locked --release --bin indexers && \
cp ./target/release/indexers /bin/indexers

########################################
# Create a new image that only contains the runtime artifacts.
########################################
FROM debian:bookworm AS runtime

ARG APP_HOME
WORKDIR ${APP_HOME}

RUN apt-get -qq update && \
apt-get -qq install -y --no-install-recommends \
ca-certificates \
&& rm -rf /var/lib/apt/lists/* /tmp/*

# Create a non-privileged user that the app will run under.
ARG USER=appuser
ARG UID=10001
ARG GID=10001
RUN addgroup --system --gid ${GID} ${USER} \
&& adduser \
--system \
--disabled-login \
--shell /bin/bash \
--home ${APP_HOME} \
--uid "${UID}" \
--gid "${GID}" \
${USER}

RUN chown ${UID}:${UID} ${APP_HOME}

USER ${USER}

# Copy the executable from the "build" stage.
COPY --from=build /bin/indexers /usr/local/bin
COPY config.json ./config.json

# Expose the port that the application listens on.
EXPOSE 3000

# What the container should run when it is started.
CMD ["indexers"]
70 changes: 0 additions & 70 deletions Dockerfile.dev

This file was deleted.

17 changes: 11 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
version: '3.8'
services:
db:
image: postgres:16
restart: always
environment:
POSTGRES_DB: mydb
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
ports:
- "5432:5432"
healthcheck:
test: [ "CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U postgres" ]
test: [ "CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}" ]
interval: 2s
timeout: 5s
retries: 1
Expand All @@ -26,9 +25,15 @@ services:
condition: service_healthy
build:
context: .
dockerfile: Dockerfile.dev
dockerfile: Dockerfile
args:
- SQLX_OFFLINE=${SQLX_OFFLINE}
- ETH_RPC_URL=${ETH_RPC_URL}
- OPT_RPC_URL=${OPT_RPC_URL}
env_file:
- .env
environment:
- RUST_BACKTRACE=1
ports:
- "3000:3000"
dns:
Expand Down