2

我正在使用 Fig 并尝试使用数据卷容器在 Rails Web 服务器和在另一个容器中运行的 Resque 工作人员之间共享上传的文件。为此,数据卷容器定义了一个/rails/public/system用于共享这些文件的卷。Rails 和 Resque 进程以rails用户身份在各自的容器中运行,这些容器都基于markb/litdistco映像。fig.yml 看起来像这样:

redis:
  image: redis:2.8.17
  volumes_from:
    - file
web:
  image: markb/litdistco
  command: /usr/bin/start-server /opt/nginx/sbin/nginx
  ports:
    - 80:8000
    - 443:4430
  environment:
    DATABASE_URL:
  links:
   - redis
  volumes_from:
    - file
worker:
  image: markb/litdistco
  command: /usr/bin/start-server "bundle exec rake environment resque:work QUEUE=litdistco_offline RAILS_ENV=production"
  environment:
    DATABASE_URL:
  links:
   - redis
  volumes_from:
    - file
file:
  image: markb/litdistco
  command: echo "datastore"
  volumes:
    - /var/redis
    - /rails/log
    - ./config/container/ssl:/etc/ssl

webworker容器运行时,我可以在两者中看到/rails/public/system目录,但是它由root两个容器中的用户拥有,并且目录的权限阻止rails用户写入该目录。

作为参考,有两个 Dockerfile 用于制作markb/litdistco容器。第一个定义了我用于本地开发的基础映像 ( Dockerfile):

# This Dockerfile is based on the excellent blog post by SteveLTN:
#
#    http://steveltn.me/blog/2014/03/15/deploy-rails-applications-using-docker/
#
# KNOWN ISSUES:
#
# * Upgrading passenger or ruby breaks nginx directives with absolute paths

# Start from Ubuntu base image
FROM ubuntu:14.04

MAINTAINER Mark Bennett <mark@burmis.ca>

# Update package sources
RUN apt-get -y update

# Install basic packages
RUN apt-get -y install build-essential libssl-dev curl

# Install basics
RUN apt-get -y install tmux vim
RUN apt-get install -y libcurl4-gnutls-dev

# Install libxml2 for nokogiri
RUN apt-get install -y libxslt-dev libxml2-dev

# Install mysql-client
RUN apt-get -y install mysql-client libmysqlclient-dev

# Add RVM key and install requirements
RUN command curl -sSL https://rvm.io/mpapis.asc | gpg --import -
RUN curl -sSL https://get.rvm.io | bash -s stable
RUN /bin/bash -l -c "rvm requirements"

# Create rails user which will run the app
RUN useradd rails --home /rails --groups rvm

# Create the rails users home and give them permissions
RUN mkdir /rails
RUN chown rails /rails

RUN mkdir -p /rails/public/system
RUN chown rails /rails/public/system
# Add configuration files in repository to filesystem
ADD config/container/start-server.sh /usr/bin/start-server
RUN chown rails /usr/bin/start-server
RUN chmod +x /usr/bin/start-server

# Make a directory to contain nginx and give rails user permission
RUN mkdir /opt/nginx
RUN chown rails /opt/nginx

# Switch to rails user that will run app
USER rails

# Install rvm, ruby, bundler
WORKDIR /rails
ADD ./.ruby-version /rails/.ruby-version
RUN echo "gem: --no-ri --no-rdoc" > /rails/.gemrc
RUN /bin/bash -l -c "rvm install `cat .ruby-version`"
RUN /bin/bash -l -c "gem install bundler --no-ri --no-rdoc"

# Install nginx
RUN /bin/bash -l -c "gem install passenger --no-ri --no-rdoc"
RUN /bin/bash -l -c "passenger-install-nginx-module"
ADD config/container/nginx-sites.conf.TEMPLATE /opt/nginx/conf/nginx.conf.TEMPLATE
ADD config/container/set-nginx-paths.sh /rails/set-nginx-paths.sh
RUN /bin/bash -l -c "source /rails/set-nginx-paths.sh"

# Copy the Gemfile and Gemfile.lock into the image.
# Temporarily set the working directory to where they are.
WORKDIR /tmp
ADD Gemfile Gemfile
ADD Gemfile.lock Gemfile.lock

# bundle install
RUN /bin/bash -l -c "bundle install"

# Add rails project to project directory
ADD ./ /rails

# set WORKDIR
WORKDIR /rails

# Make sure rails has the right owner
USER root
RUN chown -R rails:rails /rails

# Publish ports
EXPOSE 3000
EXPOSE 4430
EXPOSE 8000

这被标记为litdistco-base图像,然后我config/containers/production/Dockerfile用来生成我标记为markb/litdistco并在登台和生产中运行的图像。

# Start from LitDistCo base image
FROM litdistco-base

MAINTAINER Mark Bennett <mark@burmis.ca>

USER rails

# Setup volumes used in production
VOLUME ["/rails/log", "/rails/public/system"]

# Build the application assets
WORKDIR /rails
RUN /bin/bash -l -c "touch /rails/log/production.log; chmod 0666 /rails/log/production.log"
RUN /bin/bash -l -c "source /etc/profile.d/rvm.sh; bundle exec rake assets:precompile"

谁能解释我如何让数据容器卷安装为rails用户可写的。我非常希望避免以 root 身份运行任何 Ruby 进程,即使在容器内也是如此。

在某些情况下,我还应该提到我正在 Mac OS X 上的 boot2docker 中的 Docker 中开发图像,然后在 Ubuntu 14.04 主机上的 Google Compute Engine 实例上运行它们。谢谢!

4

2 回答 2

2

我会稍微修改您的图像。编写一个 shell 脚本,将 /usr/bin/start-server 命令包装在您的 fig.yml 中,并将其放置在您的容器中。

然后,您可以在启动服务器之前 chown rails 任何您需要的东西。

也不需要使用默认用户 rails 运行容器,只要您以 rails 用户身份启动服务器: sudo -u rails /usr/bin/start-server (或类似的东西)。

个人还没有使用过 litdistco-base 镜像,所以不知道它是如何工作的所有细节。

于 2015-02-26T19:28:33.480 回答
-1

我认为您需要通过litdistco-base以下方式修改图像,以便两个目录都归rails

# Start from LitDistCo base image
FROM litdistco-base

MAINTAINER Mark Bennett <mark@burmis.ca>

RUN mkdir -p /rails/log
RUN mkdir -p /rails/public/system
RUN chown -R rails:rails /rails/log /rails/public/system

USER rails

# Setup volumes used in production
VOLUME ["/rails/log", "/rails/public/system"]

# Build the application assets
WORKDIR /rails
RUN /bin/bash -l -c "touch /rails/log/production.log; chmod 0666 /rails/log/production.log"
RUN /bin/bash -l -c "source /etc/profile.d/rvm.sh; bundle exec rake assets:precompile"
于 2020-10-29T10:47:33.590 回答