3

问题示例:

docker run -ti -v my_passwd:/etc/passwd -v my_shadow:/etc/shadow --rm centos
[root@681a5489f3b0 /]# useradd test # does not work !?
useradd: failure while writing changes to /etc/passwd
[root@681a5489f3b0 /]# ll /etc/passwd /etc/shadow # permission check
-rw-r--r-- 1 root root 157 Oct  8 10:17 /etc/passwd
-rw-r----- 1 root root 100 Oct  7 18:02 /etc/shadow

使用 passwd 时也会出现类似的问题:

[root@681a5489f3b0 /]# passwd test
Changing password for user test.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: Authentication token manipulation error

我曾尝试使用 ubuntu 映像,但出现了同样的问题。

我可以从容器内手动编辑 passwd 文件和影子文件。

我在以下两台机器上遇到了同样的问题:

主机操作系统:CentOS 7 - SELinux 禁用
Docker 版本:1.8.2,构建 0a8c2e3

主机操作系统:CoreOS 766.4.0
Docker 版本:1.7.1,构建 df2f73d-dirty

我还在 GitHub 上打开了问题:https ://github.com/docker/docker/issues/16857

4

1 回答 1

5

它失败了,因为passwd操作了一个临时文件,然后尝试将其重命名为/etc/shadow. 这失败了,因为/etc/shadow它是一个无法替换的挂载点,这会导致此错误(使用 捕获strace):

102   rename("/etc/nshadow", "/etc/shadow") = -1 EBUSY (Device or resource busy)

您可以从命令行轻松地重现这一点:

# cd /etc
# touch foo
# mv foo shadow
mv: cannot move 'foo' to 'shadow': Device or resource busy

您可以通过在其他地方安装一个包含my_shadowand的目录,然后在容器中适当地符号链接和来解决这个问题:my_passwd/etc/passwd/etc/shadow

$ docker run -it --rm -v $PWD/my_etc:/my_etc centos
[root@afbc739f588c /]# ln -sf /my_etc/my_passwd /etc/passwd
[root@afbc739f588c /]# ln -sf /my_etc/my_shadow /etc/shadow
[root@afbc739f588c /]# ls -l /etc/{shadow,passwd}
lrwxrwxrwx. 1 root root 17 Oct  8 17:48 /etc/passwd -> /my_etc/my_passwd
lrwxrwxrwx. 1 root root 17 Oct  8 17:48 /etc/shadow -> /my_etc/my_shadow
[root@afbc739f588c /]# passwd root
Changing password for user root.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@afbc739f588c /]# 
于 2015-10-08T17:49:42.417 回答