【问题标题】:Can't install pip and python & ansible using Dockerfile in CentOS无法在 CentOS 中使用 Dockerfile 安装 pip 和 python & ansible
【发布时间】:2022-01-09 00:38:27
【问题描述】:

我正在尝试使用 Dockerfile 安装 python 和 pip & Ansible,但出现此错误

/bin/sh: 1: python: not found
The command '/bin/sh -c curl -O https://bootstrap.pypa.io/pip/2.7/get-pip.py &&     python get-pip.py &&     python -m pip install --upgrade "pip < 21.0" &&     pip install ansible --upgrade' returned a non-zero code: 127
ERROR: Service 'jenkins' failed to build : Build failed

这是我的 Dockerfile:

FROM jenkins/jenkins

USER root



RUN curl -O https://bootstrap.pypa.io/pip/2.7/get-pip.py && \
    python get-pip.py && \
    python -m pip install --upgrade "pip < 21.0" && \
    pip install ansible --upgrade





USER jenkins

注意:我在另一个 Dockerfile 上使用了相同的指令,并且没有错误。这是来自 CentOS 镜像的 Dockerfile:

FROM centos:7

RUN yum update -y && \
    yum -y install openssh-server && \
    yum install -y passwd

RUN useradd remote_user && \
    echo "password" | passwd remote_user  --stdin && \
    mkdir /home/remote_user/.ssh && \
    chmod 700 /home/remote_user/.ssh

COPY remote-key.pub /home/remote_user/.ssh/authorized_keys

RUN chown remote_user:remote_user   -R /home/remote_user && \
    chmod 600 /home/remote_user/.ssh/authorized_keys

RUN /usr/sbin/sshd-keygen

RUN yum -y install mysql

RUN curl -O https://bootstrap.pypa.io/pip/2.7/get-pip.py && \
    python get-pip.py && \
    python -m pip install --upgrade "pip < 21.0" && \
    pip install awscli --upgrade

CMD /usr/sbin/sshd -D

【问题讨论】:

  • 请不要使用“已解决!”进行编辑。您提供了一个自我回答,这是继续进行的好方法。您将能够在几个小时内接受该自我回答。
  • python 没有安装在你的基础镜像中......你为什么不在启动所有这些 python 命令之前简单地安装它?

标签: python docker jenkins ansible dockerfile


【解决方案1】:

由于我不完全确定我的 cmets 是否完全可以理解,因此我将在您当前的基础映像 jenkins/jenkins 中安装 ansible

注意事项:

  • 我将标签固定为lts,因为从最新开始构建有点过时了。您可以将其更改为适合您需要的任何标签。
  • 该基础映像本身基于 Ubuntu,而不是您标题中报告的 CentOS(因此使用 apt 而不是 yum/dnf
  • 我使用了两个 RUN 指令(一个用于安装 python,另一个用于 ansible),但如果您想进一步限制层数,可以将它们合并到一条指令中。
FROM jenkins/jenkins:lts

USER root

RUN apt-get update && \
    apt-get install -y python3-pip && \
    rm -rf /var/lib/apt/lists/*

RUN pip install --upgrade pip && \
    pip install ansible && \
    pip cache purge

USER jenkins

【讨论】:

    【解决方案2】:

    我删除了 RUN 指令并将其替换为:

    RUN apt-get update
    RUN apt-get install -y ansible
    

    工作就像一个魅力。

    【讨论】:

    • 1) 这会在您的映像中为单个软件包安装创建 2 层 2) 您不会自行清理,这会将所有 apt 缓存留在层中 3) 使用 OS 软件包管理器安装 ansible是迄今为止我最不推荐的选择。您最终会得到一个过时的 ansible 版本,该版本很可能永远不会升级,并且添加可选组件可能会变得一团糟。检查上图后,部署的 ansible 版本是 2.10,最新的是 2.12。通过 pip 安装是 IMO 最灵活的选择。
    • 非常感谢 Zeitounator,正如您所说,不建议这样做,但我必须记下您的笔记
    猜你喜欢
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 2022-12-15
    • 2021-01-29
    相关资源
    最近更新 更多