如何在 rails docker 图像中安装新的 gem 而无需重建它
How to install new gems in a rails docker image without rebuilding it
我正在尝试在 docker 环境中创建我的 rails 应用程序。我已经使用卷将源目录从主机挂载到容器内的目标路径。该应用程序处于开发阶段,我需要不断向其添加新的 gems。我从我的 运行 容器的 bash 安装了一个 gem,它安装了 gem 和所需的依赖项。但是当我删除 运行 容器(docker-compose down)然后再次实例化它们(docker-compose up)时,我的 rails 网络图像显示缺少 gem秒。我知道重新构建图像会添加 gem,但是有什么方法可以在不重建图像的情况下添加 GEMS 吗?
我关注 docker-撰写用于设置 rails 应用程序的文档
https://docs.docker.com/compose/rails/#define-the-project
DOCKERFILE
FROM ruby:2.7.1-slim-buster
LABEL MAINTAINER "Prayas Arora" "<prayasa@mindfiresolutions.com>"
# 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
一个简单的解决方案是将 gem 存储在 docker 卷中。您可以在 docker 中创建一个卷并将其附加到捆绑宝石的路径。这将保持共享状态,并且您不需要在您旋转的每个容器中安装 gem。
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
- bundle_cache:/usr/local/bundle
networks:
- proxy
- internal
.
.
volumes:
bundle_cache:
此外,a/c 到 bundler.io,Ruby 的官方 Docker 图片假定您将只使用一个应用程序,一个 Gemfile,没有其他 gem或 Ruby 应用程序将安装或 运行 在您的容器中。因此,一旦您添加了应用程序开发所需的所有 gem,就可以删除此 bundle_cache 卷并使用最终的 Gemfile 重建映像。
我正在尝试在 docker 环境中创建我的 rails 应用程序。我已经使用卷将源目录从主机挂载到容器内的目标路径。该应用程序处于开发阶段,我需要不断向其添加新的 gems。我从我的 运行 容器的 bash 安装了一个 gem,它安装了 gem 和所需的依赖项。但是当我删除 运行 容器(docker-compose down)然后再次实例化它们(docker-compose up)时,我的 rails 网络图像显示缺少 gem秒。我知道重新构建图像会添加 gem,但是有什么方法可以在不重建图像的情况下添加 GEMS 吗?
我关注 docker-撰写用于设置 rails 应用程序的文档 https://docs.docker.com/compose/rails/#define-the-project
DOCKERFILE
FROM ruby:2.7.1-slim-buster
LABEL MAINTAINER "Prayas Arora" "<prayasa@mindfiresolutions.com>"
# 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
一个简单的解决方案是将 gem 存储在 docker 卷中。您可以在 docker 中创建一个卷并将其附加到捆绑宝石的路径。这将保持共享状态,并且您不需要在您旋转的每个容器中安装 gem。
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
- bundle_cache:/usr/local/bundle
networks:
- proxy
- internal
.
.
volumes:
bundle_cache:
此外,a/c 到 bundler.io,Ruby 的官方 Docker 图片假定您将只使用一个应用程序,一个 Gemfile,没有其他 gem或 Ruby 应用程序将安装或 运行 在您的容器中。因此,一旦您添加了应用程序开发所需的所有 gem,就可以删除此 bundle_cache 卷并使用最终的 Gemfile 重建映像。