ruby-on-rails docker 镜像 632mb

问题描述 投票:0回答:1

我已经对 Rails 应用程序进行了多级 Docker 化,但图像大小超过 600mb,太大了。

Dockerfile

# Dockerfile for local development.

# Default Ruby version for this project.
ARG RUBY_VERSION=3.1.0

# Base image
FROM ruby:$RUBY_VERSION-alpine AS base

# Set environment variables for the username,
# app directory, and the language.
ENV USER app
ENV APP_DIR /app

# Set env variables for dev
ENV BUNDLER_VERSION 2.3.3
ENV GEM_HOME=/usr/local/bundle
ENV BUNDLE_PATH=$GEM_HOME
ENV BUNDLE_APP_CONFIG=$BUNDLE_PATH
ENV BUNDLE_JOBS 4
ENV BUNDLE_RETRY 3
ENV RAILS_ENV development
ENV RACK_ENV development
ENV PATH=$APP_DIR/bin:$PATH
ENV LANG C.UTF-8

# Add PostgreSQL, timezone libraries,
# and git for development.
RUN apk add --no-cache --update \
      libpq-dev \
      tzdata \
      git \
    && rm -rf /var/cache/apk/*

# Start building a new image called "dependencies"
# from the "base" image.
FROM base AS dependencies

# Add libraries required for installing gems.
RUN apk add --no-cache  --update \
      build-base \
    && rm -rf /var/cache/apk/*

# Copy the Gemfile and Gemfile.lock
# files to the current directory.
COPY Gemfile Gemfile.lock ./

# Install bundler with specified version.
RUN gem install bundler -v $BUNDLER_VERSION

# "frozen" option means that the exact versions of gems
# specified in the Gemfile.lock file will be used,
# and any updates to those gems will be ignored.
#
# Install gems with ENV options from above,
# remove unnecessary files from gems.
RUN bundle config --global frozen 1 && \
    bundle install && \
    rm -rf $BUNDLE_PATH/cache/*.gem && \
    rm -rf $BUNDLE_PATH/ruby/*/cache && \
    find $BUNDLE_PATH/gems/ -name "*.c" -delete && \
    find $BUNDLE_PATH/gems/ -name "*.o" -delete

# Start building a new image from the "base" image.
FROM base

# Copy entrypoint, make it executable.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]

# Add new user in the system.
RUN adduser -D $USER

# Create directory for rails application,
# set user as the owner.
RUN mkdir -p $APP_DIR && \
    chown $USER $APP_DIR

# Set the working directory to the app directory.
WORKDIR $APP_DIR

# Switch from root to specified user.
USER $USER

# Copy the bundle directory from the "dependencies"
# image and copy all files to the current directory,
# setting the ownership to the specified user.
COPY --from=dependencies $BUNDLE_PATH $BUNDLE_PATH
COPY --chown=$USER . ./

# Expose port 3000 for the application.
EXPOSE 3000

# Run the command to start the Rails server.
CMD ["rails", "s", "-b", "0.0.0.0"]

如何进行更改以缩小图像尺寸?

ruby-on-rails ruby dockerfile docker-image
1个回答
0
投票

您可以使用 libpq 而不是 libpq-dev。

使用

docker history image
检查哪一层更大并尝试优化该层。

但我认为你的捆绑层占用了大部分容量。

参考:

© www.soinside.com 2019 - 2024. All rights reserved.