簡體   English   中英

如何在保留所有者和權限的同時從數據容器掛載卷?

[英]How can I mount a volume from a data container while preserving the owner and permissions?

我正在使用 Fig 並嘗試使用數據卷容器在 Rails Web 服務器和在另一個容器中運行的 Resque worker 之間共享上傳的文件。 為此,數據卷容器定義了一個/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用戶擁有,並且root的權限阻止rails用戶寫入該目錄。

作為參考,有兩個 Dockerfiles 用於制作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 實例上運行它們。 謝謝!

我會稍微修改一下你的形象。 編寫一個 shell 腳本,將 /usr/bin/start-server 命令包裝在您的 fig.yml 中,並將其放置在您的容器中。

然后你可以在啟動服務器之前 chown rails 任何你需要的東西。

也不需要使用默認用戶 rails 運行容器,只要您以 rails 用戶身份啟動服務器: sudo -u rails /usr/bin/start-server (或類似的東西)。

個人還沒有使用 litdistco-base 鏡像,所以不知道它是如何工作的所有細節。

我認為您需要按以下方式修改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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM