如何在Docker容器中伪造CPU架构?

当我创build32位CentOS 5 docker映像时,我想将cpu架构报告为i386 / i686(在此容器中testing的安装程序检查体系结构并安装64位二进制文​​件而不是32位)。 我设置yumvariables并创builduname包装,所以yum和检查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') 

或者,您可以在linux32的调用中使用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镜像(或者自己构build)来configurationlinux32ENTRYPOINT ,比如:

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

Evan的答案确实Dockerfile ,但是你仍然需要在你的Dockerfile中用linux32几乎每一个RUN行添加前缀。 为了避免这种情况,我在Dockerfile的顶部附近添加了以下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的符号链接。 这将用下面的脚本replace符号链接:

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

这使得Dockerfile每个RUNDockerfilelinux32 。 除非使用绕过调用/bin/shRUN ["command"...]格式。 在这种情况下,您将不得不手动添加linux32前缀。

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

 +RUN echo "i686" > /etc/yum/vars/arch && \ + echo "i386" > /etc/yum/vars/basearch 

(就像我在toopher / centos-i386:centos6的pull request一样 )

 $ 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')