Centos 如何在docker容器中伪造cpu架构?

Centos 如何在docker容器中伪造cpu架构?,centos,docker,centos5,Centos,Docker,Centos5,当我创建32位CentOS 5 docker映像时,我希望cpu体系结构报告为i386/i686(在该容器中测试的安装程序检查体系结构并安装64位二进制文件,而不是32位)。 我设置了yum变量并创建了uname包装器,因此yum和checks-in-bash脚本可以正常工作: bash-3.2# uname -a Linux c538cf9bf508 3.13.0-24-generic #47-Ubuntu SMP Fri May 2 23:30:00 UTC 2014 i686 i686 i

当我创建32位CentOS 5 docker映像时,我希望cpu体系结构报告为i386/i686(在该容器中测试的安装程序检查体系结构并安装64位二进制文件,而不是32位)。 我设置了yum变量并创建了uname包装器,因此yum和checks-in-bash脚本可以正常工作:

bash-3.2# uname -a
Linux c538cf9bf508 3.13.0-24-generic #47-Ubuntu SMP Fri May 2 23:30:00 UTC 2014 i686 i686 i386 GNU/Linux
bash-3.2# uname -p -m -i
i686 i686 i386
bash-3.2# cat /etc/yum/vars/arch && cat /etc/yum/vars/basearch
i686
i386
但是python仍然报告64位

bash-3.2# python
Python 2.4.3 (#1, Jan  9 2013, 06:49:54) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, platform
>>> platform.machine()
'x86_64'
>>> os.uname()
('Linux', 'c538cf9bf508', '3.13.0-24-generic', '#47-Ubuntu SMP Fri May 2 23:30:00 UTC 2014', 'x86_64')

有没有办法在任何地方伪造cpu体系结构?

我希望有一种更优雅的方法来做到这一点,但我要做的是:为要使用
linux32
运行的任何命令做准备。例如:

$ docker run -t -i toopher/centos-i386:centos6 /bin/bash
[root@b027ad7830ac /]# uname -a
Linux b027ad7830ac 3.16.4-tinycore64 #1 SMP Thu Oct 23 16:14:24 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@b027ad7830ac /]# linux32 uname -a
Linux b027ad7830ac 3.16.4-tinycore64 #1 SMP Thu Oct 23 16:14:24 UTC 2014 i686 i686 i386 GNU/Linux
[root@b027ad7830ac /]# linux32 python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:37:14) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, platform
>>> platform.machine()
'i686'
>>> os.uname()
('Linux', 'b027ad7830ac', '3.16.4-tinycore64', '#1 SMP Thu Oct 23 16:14:24 UTC 2014', 'i686')
或者,您可以在调用
docker run
时使用
linux32

$ docker run -t -i toopher/centos-i386:centos6 /usr/bin/linux32 /bin/bash
[root@0f289d955fe1 /]# uname -a
Linux 0f289d955fe1 3.16.4-tinycore64 #1 SMP Thu Oct 23 16:14:24 UTC 2014 i686 i686 i386 GNU/Linux
[root@0f289d955fe1 /]# python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:37:14) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, platform
>>> platform.machine()
'i686'
>>> os.uname()
('Linux', '0f289d955fe1', '3.16.4-tinycore64', '#1 SMP Thu Oct 23 16:14:24 UTC 2014', 'i686')
或者更好的是,使用docker映像(或构建您自己的映像),将
linux32
配置为
入口点,例如:

FROM toopher/centos-i386:centos6
ENTRYPOINT ["linux32"]

Evan的答案确实有效,但您仍然需要在
Dockerfile
中的几乎每一行
RUN
前面加上
linux32
。为了避免这种情况,我在我的
Dockerfile
顶部附近添加了以下内容:

RUN rm /bin/sh && \
  echo -e '#!/bin/bash\n/usr/bin/linux32 -- /bin/bash "$@"' > /bin/sh && \
  chmod +x /bin/sh
/bin/sh
通常是指向
/bin/bash
的符号链接。这将用以下脚本替换符号链接:

#!/bin/bash
/usr/bin/linux32 -- /bin/bash "$@"

这使得在
Dockerfile
中对
的每次调用都在
linux32
下运行。除非您使用绕过调用
/bin/sh
运行[“命令”…]
格式。在这种情况下,您必须手动添加
linux32
前缀。

对于CentOS,您只需要修改以下文件:

+RUN echo "i686" > /etc/yum/vars/arch && \
+    echo "i386" > /etc/yum/vars/basearch
(就像我在电影里做的那样)


我以前试图使用它,但只找到64位的软件包,现在我在软件包setarch中找到了它,它工作正常,谢谢。我正在将其设置为entrypoint,它应该可以解决我的问题。要点很好-我已经更新了答案,以包含
entrypoint
建议。只需注意:我已请求拉至toopher/centos-i386:centos6,因此它现在可以开箱即用:这太棒了!谢谢
$ docker run -it --rm toopher/centos-i386:centos6 sh
sh-4.1# python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:37:14)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.uname()
('Linux', 'dc8d1dc46702', '3.16.0-4-amd64', '#1 SMP Debian 3.16.7-ckt11-1+deb8u6 (2015-11-09)', 'i686')