我有这个 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
,但没有成功。我在docker jenkinsfile声明上使用了args
--user 0:0
感到很幸运,但这会创建root拥有的目录和文件,而用户Jenkins在下一次运行时无法删除这些目录和文件。我不想在Dockerfile上执行
pip install
,因为实际上Install步骤正在运行一个make文件,而不是我想在其他上下文中使用的简化。我还看到了更改
HOME environment var
的建议,这似乎可以解决有关前两级警告而不是当前用户拥有,但Errno 13
部分不存在的前2条警告。 最佳答案
正如我提到的in this comment一样,解决方案应该在容器内添加适当的用户。 Jenkins将984:984
用于我的机器上的uid/gid(但可能与您的不同)-登录运行Jenkins的主机并执行sudo -u jenkins id -a
进行检测),因此您需要将其复制到应由Jenkins运行的容器中:
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
用户,因此可以创建一个虚拟环境:$ 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
组:$ docker run --user 0:984 ...
这样,尽管修改后的文件仍将更改所有者,但它们的组所有权仍将保持不变,因此Jenkins将能够清理文件(或者您可以自己通过
sh 'rm -f modified_file'
在
Jenkinsfile
中。关于docker - 如何通过jenkins管道步骤在docker镜像中进行pip安装?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54812697/