如何在Rails docker映像中安装新的gem,而无需重建它

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

我试图对Rails应用程序进行Docker化。该映像成功构建,并且我已使用卷从主机在容器内的目标路径上装载源目录。一切正常,我从运行容器的bash安装了一个gem,它安装了gem和所需的依赖项。但是,当我删除正在运行的容器(docker-compose down),然后再次实例化它们(docker-compose up)时,我的rails Web图像显示缺少宝石的错误。我知道重建图像会添加宝石,但是有什么方法可以在不重建图像的情况下添加宝石?

DOCKERFILE

FROM ruby:2.7.1-slim-buster
LABEL MAINTAINER "Prayas Arora" "<[email protected]>"

# Install apt based dependencies required to run Rails as
# well as RubyGems. As the Ruby image itself is based on a
# Debian image, we use apt-get to install those.
RUN apt-get update \
    && apt-get install -qq -y --no-install-recommends \
       build-essential \
       libpq-dev \
       netcat \
       postgresql-client \
       nodejs \
    && rm -rf /var/lib/apt/lists/*


ENV APP_HOME /var/www/repository/repository_api

# Configure the main working directory. This is the base
# directory used in any further RUN, COPY, and ENTRYPOINT
# commands.
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME

# Copy the Gemfile as well as the Gemfile.lock and install
# the RubyGems. This is a separate step so the dependencies
# will be cached unless changes to one of those two files
# are made.
COPY ./repository_api/Gemfile $APP_HOME/Gemfile
COPY ./repository_api/Gemfile.lock $APP_HOME/Gemfile.lock
RUN bundle install

# Copy the main application.
COPY ./repository_api $APP_HOME 

# Add a script to be executed every time the container starts.
COPY ./repository_docker/development/repository_api/entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]

# Expose port 3000 to the Docker host, so we can access it
# from the outside.
EXPOSE 3000

# The main command to run when the container starts. Also
# tell the Rails dev server to bind to all interfaces by
# default.
CMD ["rails","server","-b","0.0.0.0"]

docker-compose.yml

    container_name: repository_api
    build:
      context: ../..
      dockerfile: repository_docker/development/repository_api/Dockerfile
    user: $UID
    env_file: .env
    stdin_open: true
    environment:
      DB_NAME: ${POSTGRES_DB}
      DB_PASSWORD: ${POSTGRES_PASSWORD}
      DB_USER: ${POSTGRES_USER}
      DB_HOST: ${POSTGRES_DB}
    volumes:
      - ../../repository_api:/var/www/repository/repository_api
    networks:
      - proxy
      - internal
    depends_on:
      - repository_db
ruby-on-rails ruby docker docker-compose bundle
1个回答
0
投票

您可以使用docker commit <container_id> <image_name>保存对该图像或新图像的更改。

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