gpt4 book ai didi

ruby-on-rails - 通过 docker-compose run web rails g Controller 生成的文件没有编辑权限

转载 作者:IT老高 更新时间:2023-10-28 21:25:22 25 4
gpt4 key购买 nike

我正在使用 docker-compose 为开发人员提供环境。该应用程序在 docker-compose build 命令上运行良好,并在 docker-compose up 命令上在 0.0.0.0:3000 上运行。当我尝试运行命令 docker-compose run web rails g controller 以生成具有操作的 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

docker-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.

我遇到的另一个错误是,当捆绑程序开始安装 gems 时,列为公共(public) git repo 的 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. 您会收到一条警告,提示 不要以 root 身份运行 bundle
  3. 您无权访问 github,因为主机 key 验证失败

在下面找到我对每个建议的建议:

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

id -u xyzuser
1000

在上面的示例中,用户 ID 是 1000。然后,以与您在帖子中相同的方式将 useradd 命令添加到您的 Dockerfile,但添加一个附加参数 (-u):

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

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

或者,您也可以使用 USER Dockerfile 中用于在构建期间指定用户的指令,如已删除答案中所述。

2) 错误非常明确,您不能以 root 身份运行 bundler,我想说您的解决方案是更改要在 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 ... 错误,您需要设法将主机添加到容器 known_hosts 文件中。一种方法如下:

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

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

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

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

这是因为您通过 ssh 连接到 github,并且该协议(protocol)使用“指纹”来向客户端识别服务器。如果您想了解更多信息,请查看 Giles 的回答:https://unix.stackexchange.com/questions/42643/ssh-key-based-authentication-known-hosts-vs-authorized-keys

关于ruby-on-rails - 通过 docker-compose run web rails g Controller 生成的文件没有编辑权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32937717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com