Py学习  »  docker

如何使用jenkins pipline步骤在docker映像中安装pip?

tiagosilva • 5 年前 • 1510 次点击  

我有这个 文档文件 :

FROM python:3.7

CMD ["/bin/bash"]

而这 詹金斯档案 :

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 ,但没有成功。

我用args有点运气 --user 0:0 在docker jenkinsfile声明中,但这会创建根目录和根目录下的文件,用户jenkins在下次运行时无法删除这些目录和文件。

我不想做 pip install 在dockerfile上,因为实际上安装步骤运行的是make文件,而不是我上面使用的简化,我希望在其他上下文中使用。

我也看到了改变 HOME environment var ,这似乎修复了有关父目录不属于当前用户的前两个警告,但没有 Errno 13 部分。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43802
 
1510 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Rohit Jindal
Reply   •   1 楼
Rohit Jindal    6 年前

似乎这个问题和用户更相关。当Docker代理和根用户一起运行时,Stage CMD和Jenkins中配置的相应用户一起运行(是根用户吗?).

在Docker文件中创建一个类似的用户,并将运行的容器分配给该用户。

文档文件

   FROM python:3.7

   ARG Jenkins_user=XXXXXX

   RUN useradd -ms /bin/bash $Jenkins_user

   USER $Jenkins_user

   CMD ["/bin/bash"] 

编辑:jenkins_用户将与pip cmd一起在容器中运行。要检查您可以放置的用户

sh'echo$用户'

在安装阶段部分。然后用准确的用户更新dokerfile。

hoefling
Reply   •   2 楼
hoefling    6 年前

如我所说 in this comment ,解决方案应该是在容器中添加适当的用户。詹金斯使用 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

等。


或者,你 可以 (但在大多数情况下不应该)作为 但是 jenkins 组:

$ docker run --user 0:984 ...

这样,虽然修改后的文件仍将改变所有者,但它们的组所有权仍然完好无损,因此詹金斯将能够清理文件(或者您可以自己通过,通过

sh 'rm -f modified_file'

Jenkinsfile .