通过docker-compose生成的文件运行web rails g控制器没有权限编辑

我正在使用docker-compose为开发人员提供环境。 docker-compose build命令运行正常,运行在0.0.0.0:3000 docker-compose up命令的0.0.0.0:3000上。 当我试图运行命令docker-compose run web rails g controller生成一个操作的控制器,比生成的文件,但没有权限在主机上编辑。

Dockerfile

 FROM ubuntu:14.04 FROM ruby:2.2.1 # Run updates RUN apt-get update -qq && apt-get install -y build-essential libpq-dev # Set up working directory RUN mkdir /xyz/ WORKDIR /xyz/ # Set up gems ADD Gemfile /xyz/Gemfile ADD Gemfile.lock /xyz/Gemfile.lock RUN bundle install # Finally, add the rest of our app's code # (this is done at the end so that changes to our app's code # don't bust Docker's cache) ADD . /xyz 

我甚至试图添加一个用户xyzuser但没有工作。

 # Create a user imliuser to run app that is not root RUN useradd --create-home --home-dir /xyz --shell /bin/bash xyzuser RUN chown -R xyzuser /xyz 

泊坞窗,compose.yml

 db: image: postgres ports: - "5432" redis: image: redis ports: - "6379" web: build: . command: bundle exec rails s -b 0.0.0.0 volumes: - .:/xyz:rw ports: - "3000:3000" links: - db - redis 

当运行docker-compose build ,也会收到警告, don't run bundler as root

我得到的另外一个错误是,当捆绑商开始安装gem时,被列为公共git仓库的gem无法安装。

 gem 'workflow', :git => 'git@github.com:xyz/workflow.git', :branch =>'feature_state_to_integer' 

获取以下错误。

 Host key verification failed. fatal: Could not read from remote repository. 

即使存储库是公开的

你在你的问题中暴露三个问题:

  1. 您无权访问装入卷中由docker写入的文件。
  2. 你会得到一个警告,说don't run bundle as root
  3. 你没有访问Github,因为Host key verification failed

find下面我的build议为每个人:

1)如果您有root访问权限,您可以访问主机系统中的任何文件。 所以我假设你没有,但你的用户是在docker组,允许运行docker命令。 首先,find这样的用户的用户ID:

 id -u xyzuser 1000 

在上面的例子中,用户ID是1000 。 然后,添加useradd命令到你的Dockerfile中,就像你在文章中一样,但是增加了一个额外的参数( -u ):

 RUN useradd -u 1000 --create-home --home-dir /xyz --shell /bin/bash xyzuser 

最后,用xyzuser运行你的xyzuser容器(见下一点)。 因此,您已经使主机和docker工具容器明白他们使用的是同一个用户,而不是两个具有相同名称的不同用户。

或者,也可以使用Dockerfile中的USER指令在构build过程中指定用户,如发布在已删除的答案中。

2)错误是非常明确的,你不能以root身份运行bundler,我想说你的解决scheme是改变将要在docker容器中运行命令的用户。 您可以使用.yaml文件中的user参数来更改它。 例:

 web: build: . user: xyzuser command: bundle exec rails s -b 0.0.0.0 volumes: # Avoid to use relative paths during # testing stage - /abs/path/to/current:/xyz ports: - "3000:3000" links: - db - redis 

3)要解决Host key verification ...错误,您需要pipe理添加主机到容器known_hosts文件。 一个方法如下:

 ssh-keyscan -H github.com >> ~/.ssh/known_hosts 

在您的docker文件中,您可以添加以下行:

 RUN ssh-keyscan github.com >> /root/.ssh/known_hosts 

之前的gem设置。 确保将主机密钥添加到要连接到github的实际用户的.ssh目录中。

原因是因为你通过ssh连接到github,这个协议使用“指纹”来识别服务器到客户端。 看看Giles的答案,如果你想知道更多关于这个: https : //unix.stackexchange.com/questions/42643/ssh-key-based-authentication-known-hosts-vs-authorized-keys

Docker容器默认以root用户身份运行你的应用程序。 您必须sudo chown将其设置为您的用户,或者使用相同的用户标识在图像中创build一个用户。