How to reduce Docker Image Size of Barong?


FROM ruby:2.7-slim AS builder

ENV RAILS_ENV=production
WORKDIR /app

RUN apt-get update && apt-get install -y \
  build-essential \
  ruby-dev \
  git \
  curl \
  libpq-dev \
  libmariadb-dev \
  libmariadb-dev-compat \
  libssl-dev \
  zlib1g-dev \
  libxml2-dev \
  libpq5 \
  libmariadb3 \
  ca-certificates \
  libxslt1-dev \
  && rm -rf /var/lib/apt/lists/*

COPY Gemfile Gemfile.lock ./

RUN gem install bundler -v 2.1.4 \
 && bundle _2.1.4_ config set without 'development test' \
 && bundle _2.1.4_ install --jobs=4

COPY . .

# ---------- RUNTIME STAGE ----------
FROM ruby:2.7-slim

ENV RAILS_ENV=production \
    APP_HOME=/home/app

WORKDIR $APP_HOME

# runtime only
RUN apt-get update && apt-get install -y \
  libpq5 \
  libmariadb3 \
  ca-certificates \
  && rm -rf /var/lib/apt/lists/*

# non-root user (Kubernetes best practice)
RUN groupadd -r app && useradd -r -g app -m app

COPY --from=builder /usr/local/bundle /usr/local/bundle
COPY --from=builder /app $APP_HOME

RUN chown -R app:app $APP_HOME

USER app

EXPOSE 8080

CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]

542MB with above Dockerfile for Barong is already decent, but we can still realistically push it down to:

πŸ‘‰ ~300–380MB (safe)
πŸ‘‰ ~250MB (aggressive, still stable)

Below is exactly how to do it.


πŸš€ WHY YOUR IMAGE IS STILL 542MB

Your biggest size contributors are:

1. ❌ Build gems leaked into runtime

COPY --from=builder /usr/local/bundle /usr/local/bundle

πŸ‘‰ includes dev dependencies + cache


2. ❌ No bundler cleanup

  • gem cache remains
  • documentation included
  • unnecessary files inside gems

3. ❌ runtime packages too heavy

You install:

  • wget
  • curl
  • ca-certificates (ok)
  • but also could be reduced

4. ❌ full build dependency set still affects bundle install output


🧠 TARGET OPTIMIZATION PLAN

We will reduce size using:

βœ… 1. Clean bundler properly

βœ… 2. Remove gem docs

βœ… 3. Install only production gems

βœ… 4. Strip build artifacts

βœ… 5. Reduce runtime packages

βœ… 6. Fix COPY strategy


πŸš€ OPTIMIZED DOCKERFILE (drop to ~300MB)

πŸ”₯ FINAL VERSION Size 542 MB


FROM ruby:2.7-slim AS builder

ENV RAILS_ENV=production
WORKDIR /app

RUN apt-get update && apt-get install -y \
  build-essential \
  ruby-dev \
  git \
  curl \
  libpq-dev \
  libmariadb-dev \
  libmariadb-dev-compat \
  libssl-dev \
  zlib1g-dev \
  libxml2-dev \
  libpq5 \
  libmariadb3 \
  ca-certificates \
  libxslt1-dev \
  && rm -rf /var/lib/apt/lists/*

COPY Gemfile Gemfile.lock ./

RUN gem install bundler -v 2.1.4 \
 && bundle _2.1.4_ config set without 'development test' \
 && bundle _2.1.4_ install --jobs=4

COPY . .

# ---------- RUNTIME STAGE ----------
FROM ruby:2.7-slim

ENV RAILS_ENV=production \
    APP_HOME=/home/app

WORKDIR $APP_HOME

# runtime only
RUN apt-get update && apt-get install -y \
  libpq5 \
  libmariadb3 \
  ca-certificates \
  && rm -rf /var/lib/apt/lists/*

# non-root user (Kubernetes best practice)
RUN groupadd -r app && useradd -r -g app -m app

COPY --from=builder /usr/local/bundle /usr/local/bundle
COPY --from=builder /app $APP_HOME

RUN chown -R app:app $APP_HOME

USER app

EXPOSE 8080

CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]






πŸ“‰ EXPECTED SIZE AFTER THIS

VersionSize
your current542MB
optimized300–380MB
aggressive tuning250MB

πŸ”₯ EXTRA (BIGGEST WIN YOU’RE MISSING)

❌ You are copying ALL of /app

This line is expensive:

COPY --from=builder /app $APP_HOME

πŸ‘‰ includes:

  • tests
  • docs
  • assets
  • node modules (if any)
  • temp files

πŸ’‘ BETTER approach

Only copy required runtime folders:

COPY --from=builder /app/app $APP_HOME/app
COPY --from=builder /app/config $APP_HOME/config
COPY --from=builder /app/db $APP_HOME/db
COPY --from=builder /app/bin $APP_HOME/bin

⚑ EVEN MORE REDUCTION (advanced)

If you want to go further (~250MB):

1. Strip native extensions

RUN find /usr/local/bundle -name "*.c" -delete
RUN find /usr/local/bundle -name "*.o" -delete

2. Remove documentation completely

Already done via:

gem: --no-document

3. Use smaller runtime libs

Replace:

libmariadb3

with minimal required runtime only.


🧠 REAL LIMIT (important truth)

For Barong/Peatio:

LayerMinimum realistic size
Ruby runtime~80MB
gems (Rails + deps)~180–250MB
OS libs~50–80MB

πŸ‘‰ So absolute realistic floor is:

πŸ”₯ ~250MB–300MB (production safe)


πŸš€ BEST NEXT STEP (recommended)

If you want serious production setup, I can upgrade this into:

☸️ Kubernetes production grade:

  • Helm chart
  • HPA autoscaling
  • Redis + Postgres separation
  • secrets management
  • ingress + TLS
  • zero-downtime deploys

Just say:

πŸ‘‰ β€œmake full production Kubernetes stack for Barong”

and I’ll build the full exchange-grade setup.

Leave a Reply

Your email address will not be published. Required fields are marked *