Python 在 docker 中安装 pip

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36611052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:07:19  来源:igfitidea点击:

Install pip in docker

pythondockerpip

提问by scubbo

I'm not able to install pip in Docker.

我无法在 Docker 中安装 pip。

Here's my Dockerfile:

这是我的 Dockerfile:

FROM ubuntu:14.04

# Install dependencies
RUN apt-get update -y
RUN apt-get install -y git curl apache2 php5 libapache2-mod-php5 php5-mcrypt php5-mysql python3.4 python-pip

When building, I get:

构建时,我得到:

Sending build context to Docker daemon 109.6 kB
Step 1 : FROM ubuntu:14.04
 ---> b549a9959a66
Step 2 : RUN apt-get update -y
 ---> Using cache
 ---> 84577471562c
Step 3 : RUN apt-get install -y git curl apache2 php5 libapache2-mod-php5 php5-mcrypt php5-mysql python3.4 python-pip
 ---> Running in 49252a6d0eb1
Reading package lists...
Building dependency tree...
Reading state information...
python3.4 is already the newest version.
The following extra packages will be installed:
  apache2-bin apache2-data binutils build-essential ca-certificates cpp
  cpp-4.8 dpkg-dev fakeroot g++ g++-4.8 gcc gcc-4.8 git-man krb5-locales
  libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl
  libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libasan0
  libasn1-8-heimdal libatomic1 libc-dev-bin libc6-dev libcloog-isl4 libcurl3
  libcurl3-gnutls libdpkg-perl libedit2 liberror-perl libfakeroot
  libfile-fcntllock-perl libgcc-4.8-dev libgmp10 libgomp1 libgssapi-krb5-2
  libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal
  libheimntlm0-heimdal libhx509-5-heimdal libidn11 libisl10 libitm1
  libk5crypto3 libkeyutils1 libkrb5-26-heimdal libkrb5-3 libkrb5support0
  libldap-2.4-2 libmcrypt4 libmpc3 libmpfr4 libmysqlclient18 libpython-stdlib
  libpython2.7-minimal libpython2.7-stdlib libquadmath0 libroken18-heimdal
  librtmp0 libsasl2-2 libsasl2-modules libsasl2-modules-db libstdc++-4.8-dev
  libtimedate-perl libtsan0 libwind0-heimdal libx11-6 libx11-data libxau6
  libxcb1 libxdmcp6 libxext6 libxml2 libxmuu1 linux-libc-dev lsof make
  manpages manpages-dev mysql-common openssh-client openssl patch php5-cli
  php5-common php5-json php5-readline psmisc python python-chardet
  python-chardet-whl python-colorama python-colorama-whl python-distlib
  python-distlib-whl python-html5lib python-html5lib-whl python-minimal
  python-pip-whl python-pkg-resources python-requests python-requests-whl
  python-setuptools python-setuptools-whl python-six python-six-whl
  python-urllib3 python-urllib3-whl python-wheel python2.7 python2.7-minimal
  python3-pkg-resources rsync sgml-base ssl-cert xauth xml-core xz-utils
Suggested packages:
  www-browser apache2-doc apache2-suexec-pristine apache2-suexec-custom ufw
  apache2-utils binutils-doc cpp-doc gcc-4.8-locales debian-keyring
  g++-multilib g++-4.8-multilib gcc-4.8-doc libstdc++6-4.8-dbg gcc-multilib
  autoconf automake1.9 libtool flex bison gdb gcc-doc gcc-4.8-multilib
  libgcc1-dbg libgomp1-dbg libitm1-dbg libatomic1-dbg libasan0-dbg
  libtsan0-dbg libquadmath0-dbg gettext-base git-daemon-run
  git-daemon-sysvinit git-doc git-el git-email git-gui gitk gitweb git-arch
  git-bzr git-cvs git-mediawiki git-svn php-pear glibc-doc krb5-doc krb5-user
  libmcrypt-dev mcrypt libsasl2-modules-otp libsasl2-modules-ldap
  libsasl2-modules-sql libsasl2-modules-gssapi-mit
  libsasl2-modules-gssapi-heimdal libstdc++-4.8-doc make-doc man-browser
  ssh-askpass libpam-ssh keychain monkeysphere ed diffutils-doc
  php5-user-cache python-doc python-tk python-genshi python-lxml
  python-distribute python-distribute-doc python2.7-doc binfmt-support
  python3-setuptools openssh-server sgml-base-doc openssl-blacklist debhelper
Recommended packages:
  ssh-client python-dev-all
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data binutils build-essential ca-certificates
  cpp cpp-4.8 curl dpkg-dev fakeroot g++ g++-4.8 gcc gcc-4.8 git git-man
  krb5-locales libalgorithm-diff-perl libalgorithm-diff-xs-perl
  libalgorithm-merge-perl libapache2-mod-php5 libapr1 libaprutil1
  libaprutil1-dbd-sqlite3 libaprutil1-ldap libasan0 libasn1-8-heimdal
  libatomic1 libc-dev-bin libc6-dev libcloog-isl4 libcurl3 libcurl3-gnutls
  libdpkg-perl libedit2 liberror-perl libfakeroot libfile-fcntllock-perl
  libgcc-4.8-dev libgmp10 libgomp1 libgssapi-krb5-2 libgssapi3-heimdal
  libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal
  libhx509-5-heimdal libidn11 libisl10 libitm1 libk5crypto3 libkeyutils1
  libkrb5-26-heimdal libkrb5-3 libkrb5support0 libldap-2.4-2 libmcrypt4
  libmpc3 libmpfr4 libmysqlclient18 libpython-stdlib libpython2.7-minimal
  libpython2.7-stdlib libquadmath0 libroken18-heimdal librtmp0 libsasl2-2
  libsasl2-modules libsasl2-modules-db libstdc++-4.8-dev libtimedate-perl
  libtsan0 libwind0-heimdal libx11-6 libx11-data libxau6 libxcb1 libxdmcp6
  libxext6 libxml2 libxmuu1 linux-libc-dev lsof make manpages manpages-dev
  mysql-common openssh-client openssl patch php5 php5-cli php5-common
  php5-json php5-mcrypt php5-mysql php5-readline psmisc python python-chardet
  python-chardet-whl python-colorama python-colorama-whl python-distlib
  python-distlib-whl python-html5lib python-html5lib-whl python-minimal
  python-pip python-pip-whl python-pkg-resources python-requests
  python-requests-whl python-setuptools python-setuptools-whl python-six
  python-six-whl python-urllib3 python-urllib3-whl python-wheel python2.7
  python2.7-minimal python3-pkg-resources rsync sgml-base ssl-cert xauth
  xml-core xz-utils
0 upgraded, 130 newly installed, 0 to remove and 4 not upgraded.
Need to get 61.9 MB of archives.
After this operation, 213 MB of additional disk space will be used.
Err http://archive.ubuntu.com/ubuntu/ trusty-updates/main libroken18-heimdal amd64 1.6~git20131207+dfsg-1ubuntu1.1
  Could not resolve 'archive.ubuntu.com'
Err http://archive.ubuntu.com/ubuntu/ trusty-updates/main libasn1-8-heimdal amd64 1.6~git20131207+dfsg-1ubuntu1.1
  Could not resolve 'archive.ubuntu.com'

.
.
.

Err http://archive.ubuntu.com/ubuntu/ trusty-security/main python2.7 amd64 2.7.6-8ubuntu0.2
  Could not resolve 'archive.ubuntu.com'
E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/main/h/heimdal/libroken18-heimdal_1.6~git20131207+dfsg-1ubuntu1.1_amd64.deb  Could not resolve 'archive.ubuntu.com'

E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/main/h/heimdal/libasn1-8-heimdal_1.6~git20131207+dfsg-1ubuntu1.1_amd64.deb  Could not resolve 'archive.ubuntu.com'

E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/main/k/krb5/libkrb5support0_1.12+dfsg-2ubuntu5.2_amd64.deb  Could not resolve 'archive.ubuntu.com'

E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/main/s/ssl-cert/ssl-cert_1.0.33_all.deb  Could not resolve 'archive.ubuntu.com'

E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
The command '/bin/sh -c apt-get install -y git curl apache2 php5 libapache2-mod-php5 php5-mcrypt php5-mysql python3.4 python-pip' returned a non-zero code: 100

Having read this answer, I tried adding the line RUN "sh" "-c" "echo nameserver 8.8.8.8 >> /etc/resolv.conf"- the error this time is:

阅读此答案后,我尝试添加该行RUN "sh" "-c" "echo nameserver 8.8.8.8 >> /etc/resolv.conf"- 这次的错误是:

...
...
Reading state information...
E: Unable to locate package php5-mcrypt
E: Unable to locate package python-pip
The command '/bin/sh -c apt-get install -y git curl apache2 php5 libapache2-mod-php5 php5-mcrypt php5-mysql python3.4 python-pip' returned a non-zero code: 100

What am I doing wrong? I've seen some claims that I should try a different version of Ubuntu, but switching to 13.1 didn't change anything.

我究竟做错了什么?我看到有人声称我应该尝试不同版本的 Ubuntu,但切换到 13.1 并没有改变任何东西。

回答by mxscho

While T. Arboreus's answer might fix the issues with resolving 'archive.ubuntu.com', I think the last error you're getting says that it doesn't know about the packages php5-mcryptand python-pip. Nevertheless, the reduced Dockerfile of you with just these two packages worked for me (using Debian 8.4 and Docker 1.11.0), but I'm not quite sure if that could be the case because my host system is different than yours.

虽然 T. Arboreus 的回答可能会解决解决“archive.ubuntu.com”的问题,但我认为你得到的最后一个错误是它不知道包php5-mcryptpython-pip. 尽管如此,仅使用这两个软件包减少的 Dockerfile 对我有用(使用 Debian 8.4 和 Docker 1.11.0),但我不太确定是否是这种情况,因为我的主机系统与您的不同。

FROM ubuntu:14.04

# Install dependencies
RUN apt-get update && apt-get install -y \
    php5-mcrypt \
    python-pip

However, according to this answeryou should think about installing the python3-pippackage instead of the python-pippackage when using Python 3.x.

但是,根据此答案,您应该考虑在使用 Python 3.x 时安装python3-pip包而不是python-pip包。

Furthermore, to make the php5-mcryptpackage installation working, you might want to add the universe repository like it's shown right here. I had trouble with the add-apt-repositorycommand missing in the Ubuntu Docker image so I installed the package software-properties-commonat first to make the command available.

此外,为了使php5-mcrypt包安装工作,你可能要添加宇宙库像它的显示在这里。我add-apt-repository在 Ubuntu Docker 映像中缺少命令时遇到了问题,因此我首先安装了该软件包software-properties-common以使该命令可用。

Splitting up the statements and putting apt-get updateand apt-get installinto one RUNcommand is also recommended here.

此处还建议将语句拆分apt-get updateapt-get install放入一个RUN命令中。

Oh and by the way, you actually don't need the -yflag at apt-get updatebecause there is nothing that has to be confirmed automatically.

哦,顺便说一句,你实际上不需要-y标志,apt-get update因为没有什么需要自动确认的。

Finally:

最后:

FROM ubuntu:14.04

# Install dependencies
RUN apt-get update && apt-get install -y \
    software-properties-common
RUN add-apt-repository universe
RUN apt-get update && apt-get install -y \
    apache2 \
    curl \
    git \
    libapache2-mod-php5 \
    php5 \
    php5-mcrypt \
    php5-mysql \
    python3.4 \
    python3-pip

Remark: The used versions (e.g. of Ubuntu) might be outdated in the future.

备注:使用的版本(例如 Ubuntu)将来可能会过时。

回答by serv-inc

An alternative is to use the Alpine Linux containers, e.g. python:2.7-alpine. They offer pipout of the box (and have a smaller footprint which leads to faster builds etc).

另一种方法是使用 Alpine Linux 容器,例如python:2.7-alpine. 它们pip开箱即用(并且占地面积更小,从而可以加快构建速度等)。

回答by T. Arboreus

Try this:

尝试这个:

  1. Uncomment the following line in /etc/default/docker DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"
  2. Restart the Docker service sudo service docker restart
  3. Delete any images which have cached the invalid DNS settings.
  4. Build again and the problem should be solved.
  1. 在 /etc/default/docker DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4" 中取消注释以下行
  2. 重启 Docker 服务 sudo service docker restart
  3. 删除所有缓存了无效 DNS 设置的图像。
  4. 再次构建,应该可以解决问题。

From this question.

这个问题

回答by Rishich Mishra

You might want to change the DNS settings of the Docker daemon. You can edit (or create) the configuration file at /etc/docker/daemon.jsonwith the dns key, as

您可能想要更改 Docker 守护程序的 DNS 设置。您可以/etc/docker/daemon.json使用 dns 键编辑(或创建)配置文件,如

{
    "dns": ["your_dns_address", "8.8.8.8"]
}

In the example above, the first element of the list is the address of your DNS server. The second item is the Google's DNS which can be used when the first one is not available.

在上面的示例中,列表的第一个元素是您的 DNS 服务器的地址。第二项是 Google 的 DNS,当第一项不可用时可以使用它。

Before proceeding, save daemon.json and restart the docker service.

在继续之前,保存 daemon.json 并重新启动 docker 服务。

sudo service docker restart

Once fixed, retry to run the build command.

修复后,重试运行构建命令。