【问题标题】:How to fake cpu architecture in docker container?如何在 docker 容器中伪造 cpu 架构?
【发布时间】:2014-12-16 22:16:16
【问题描述】:

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

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 架构?

【问题讨论】:

    标签: centos docker centos5


    【解决方案1】:

    我希望有一种更优雅的方式来做到这一点,但我就是这样做的:在你想运行的任何命令前加上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')
    

    或者甚至更好,使用配置linux32的docker镜像(或构建你自己的),因为它是ENTRYPOINT,例如:

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

    【讨论】:

    • 我之前尝试使用它,但发现只有 64 位包,现在我在包 setarch 中找到了它,它工作正常,谢谢。我将它设置为入口点,它应该可以解决我的问题。
    • 好点 - 我已经更新了答案以包含 ENTRYPOINT 建议。
    • 请注意:我已经向 toopher/centos-i386:centos6 请求了 pull-request,所以它现在开箱即用:github.com/toopher/toopher-docker/pull/1
    【解决方案2】:

    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 中对RUN 的每次调用都在linux32 下运行。除非您使用绕过调用/bin/shRUN ["command"...] 格式。在这种情况下,您必须手动添加 linux32 前缀。

    【讨论】:

      【解决方案3】:

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

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

      (就像我在the pull-request to 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')
      

      【讨论】:

      • 这太棒了!谢谢。
      猜你喜欢
      • 2022-06-15
      • 2016-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-18
      相关资源
      最近更新 更多