我有这个 Dockerfile :
FROM python:3.7 CMD ["/bin/bash"]
和这个 Jenkinsfile :
pipeline { agent { dockerfile { filename 'Dockerfile' } } stages { stage('Install') { steps { sh 'pip install --upgrade pip' } } }
这将导致以下错误:
The directory '/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. The directory '/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. Collecting pip Downloading https://files.pythonhosted.org/packages/d8/f3/413bab4ff08e1fc4828dfc59996d721917df8e8583ea85385d51125dceff/pip-19.0.3-py2.py3-none-any.whl (1.4MB) Installing collected packages: pip Found existing installation: pip 19.0.2 Uninstalling pip-19.0.2: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/local/bin/pip' Consider using the `--user` option or check the permissions.
我尝试使用--user,但没有成功。
--user
我在--user 0:0docker jenkinsfile声明上使用args 时有些运气,但这会创建root拥有的目录和文件,这些文件和目录不能由用户Jenkins在下次运行时删除。
--user 0:0
我不想pip install在Dockerfile上进行操作,因为实际上Install步骤正在运行一个make文件,而不是我想在其他上下文中使用的简化文件。
pip install
我还看到了更改的建议HOME environment var,这似乎可以解决有关前两级警告不属于当前用户但不Errno 13属于该部分的前2条警告。
HOME environment var
Errno 13
正如我在此评论中提到的,解决方案应在容器内添加适当的用户。Jenkins984:984在我的机器上用于uid / gid(但可能与您的机器不同-登录到运行Jenkins的主机并执行sudo -u jenkins id-a以检测到它们),因此您需要将其复制到应由Jenkins运行的容器中:
984:984
sudo -u jenkins id-a
FROM python:3.7 RUN mkdir /home/jenkins RUN groupadd -g 984 jenkins RUN useradd -r -u 984 -g jenkins -d /home/jenkins jenkins RUN chown jenkins:jenkins /home/jenkins USER jenkins WORKDIR /home/jenkins CMD ["/bin/bash"]
当然,由于您root不再是容器中的用户,因此请创建一个虚拟环境:
root
$ docker run --rm -it jenkins/python /bin/bash jenkins@d0dc87c39810:~$ python -m venv myenv jenkins@d0dc87c39810:~$ source myenv/bin/activate jenkins@d0dc87c39810:~$ pip install numpy
或使用--user参数:
$ docker run --rm -it jenkins/python /bin/bash jenkins@d0dc87c39810:~$ pip install --user --upgrade pip jenkins@d0dc87c39810:~$ pip install --user numpy
等等
或者,您 可以 (但在大多数情况下不应该)以方式输入容器root,但使用jenkins组:
jenkins
$ docker run --user 0:984 ...
这样,尽管修改后的文件仍将更改所有者,但它们的组所有权仍将保持不变,因此Jenkins将能够清理文件(或者您可以自己通过
sh 'rm -f modified_file'
在Jenkinsfile。
Jenkinsfile