从 rhel6.5 镜像构建 docker 镜像

Build docker image from rhel6.5 image

我正在尝试构建一个基于 python3 的容器并安装一些要求。但是 python3 的构建失败了 这是我的 Dockerfile

FROM rhel6.5
ADD ./* /app/
WORKDIR /app/Python
CMD ["./configure"]
CMD ["make"]
CMD ["altinstall"]
WORKDIR /app
CMD ["python", "-m", "pip",  "install" , "-r" , "requirement.txt"]

当我构建后 运行

sudo docker run myimage python -v 给出 2.6 什么时候,我 运行 bash 并尝试再次编译 python

sudo docker run -i myimage /bin/bash
./configure

我得到了

./configure
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... linux
checking for --without-gcc... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/app':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

终于

yum install gcc

同样失败

Loaded plugins: product-id, subscription-manager
Setting up Install Process
No package gcc available.
Error: Nothing to do

您使用的是 RHEL 映像; Red Hat Enterprise Linux 是一种商业产品,需要付费订阅才能访问更新。除非您使用 subscription-manager attach.

配置一些订阅,否则您不会配置任何存储库

如果您目前不是付费的 RHEL 客户并且您没有需要 RHEL 的令人信服的理由,您可以通过使用 CentOS 来有效地访问同一组软件包。

另请注意,当前 fedoraubuntu 图像都有 Python 3 个可用(作为 Fedora 的可安装包,默认情况下包含 Ubuntu ).

你的Dockerfile是错误的。您正在使用 CMD 而不是 RUN。请参阅有关 Dockerfile 的参考文档:https://docs.docker.com/reference/builder/

The main purpose of a CMD is to provide defaults for an executing container.

构建时 CMD 中的任何内容都不会执行;它只是元数据,当 运行 来自图像的容器时,只有最后一个被选择执行。

如果您使用以下内容,docker 将无法构建(但有充分的理由)。

FROM rhel6.5
ADD ./* /app/
WORKDIR /app/Python
RUN ./configure
RUN make
RUR altinstall
WORKDIR /app
RUN python -m pip install -r requirement.txt

您可能想要添加一个步骤来安装编译器和所需的依赖项,以便能够在您的 rhel 中构建 python。但我会听从@larsks 和@Adrian Mouat 的建议。

搭建python的步骤肯定比较多,下面是我最近用的Python2.7.9的步骤(相信3.4很容易适应):

  • sudo yum groupinstall "Development tools"
  • sudo yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
  • curl https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz -o ~/Python-2.7.9.tgz
  • tar-xvfPython-2.7.9.tgz
  • cd Python-2.7.9
  • ./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
  • sudo vi /etc/ld.so.conf
  • 在新行添加 /usr/local/lib 并保存。
  • sudo /sbin/ldconfig
  • 制作
  • sudo make altinstall
  • 删除 Python-2.7.9 文件夹和 Python-2.7.9.tgz 存档

因为你正在写 Dockerfile 你应该:

  • 使用 yum install -y 安装依赖项
  • 因为你是 altinstalling,python 解释器在 /usr/local/bin/python2.7 或 python3.4 中可用,具体取决于你使用的版本安装。
  • make altinstall 而不是 make install 的全部目的是确保您不会破坏 python 可执行文件的系统依赖性。

@Teodore 的回复和 https://registry.hub.docker.com/u/danzilla/centos6-python3-django/dockerfile/

深受启发

并添加

ADD ./* /app/
RUN pip3.4 install -r /app/requirement.txt 

在文件的和处。其中 requirement 是我的项目所需的所有要求。

sudo docker run python3-redhat /usr/local/bin/python3.4 -c "import dja
ngo; print(django.get_version())"

按预期给出 1.8.1.