你报错的信息应该是
Unable to find user test1
原因是你images里没有-u的username的用户,所以才会报错。
下面是我做的测试,你可以参考一下。
先登录一个现有的镜像里,然后创建test_new4用户。
[root@ip-10-10-125-6 docker_code]# docker exec -it test_new4 bash
root@b8efc9a9471a:/# useradd test_new4
root@b8efc9a9471a:/# id
uid=0(root) gid=0(root) groups=0(root)
root@b8efc9a9471a:/# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh
proxy:x:13:13:proxy:/bin:/bin/sh
www-data:x:33:33:www-data:/var/www:/bin/sh
backup:x:34:34:backup:/var/backups:/bin/sh
list:x:38:38:Mailing List Manager:/var/list:/bin/sh
irc:x:39:39:ircd:/var/run/ircd:/bin/sh
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh
nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
libuuid:x:100:101::/var/lib/libuuid:/bin/sh
sshd:x:101:65534::/var/run/sshd:/usr/sbin/nologin
root@b8efc9a9471a:/# useradd test_new4
root@b8efc9a9471a:/# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh
proxy:x:13:13:proxy:/bin:/bin/sh
www-data:x:33:33:www-data:/var/www:/bin/sh
backup:x:34:34:backup:/var/backups:/bin/sh
list:x:38:38:Mailing List Manager:/var/list:/bin/sh
irc:x:39:39:ircd:/var/run/ircd:/bin/sh
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh
nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
libuuid:x:100:101::/var/lib/libuuid:/bin/sh
sshd:x:101:65534::/var/run/sshd:/usr/sbin/nologin
test_new4:x:1000:1000::/home/test_new4:/bin/sh
然后提交此镜像
[root@ip-10-10-125-6 docker_code]# docker commit --pause=false b8efc9a9471a test_new4:1.0
be9fbe7a33ab03230b6bce5d3e83c901f7f6d0f8f16a6db21d196a6a2b4d73b1
[root@ip-10-10-125-6 docker_code]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
test_new4 1.0 be9fbe7a33ab 4 minutes ago 269.8 MB
之后在使用此镜像生产一个容器24,然后启动的时候指定使用test_new4用户
[root@ip-10-10-125-6 docker_code]# docker run --restart always -d -u test_new4 --name='24' test_new4:1.0 /usr/bin/supervisord
a639b74bd664c34466e257ecd898374230658d0c6e81d9a71d458ff6bdec3f4a
[root@ip-10-10-125-6 docker_code]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a639b74bd664 test_new4:1.0 "/usr/bin/supervisor 3 seconds ago Up 2 seconds 24
fb2fdbed8f4d docker.ops-chukong.com:5000/ubuntu-12.04-base:1.0 "/usr/bin/supervisor 19 hours ago Up 19 hours cluster-test2.0
68f4f61af646 docker.ops-chukong.com:5000/ubuntu-12.04-base:1.0 "/usr/bin/supervisor 39 hours ago Up 39 hours cluster-test1.0
b8efc9a9471a docker.ops-chukong.com:5000/ubuntu-12.04-base:1.0 "/usr/bin/supervisor 2 weeks ago Up 2 weeks test_new4
最后进入容器测试看看
[root@ip-10-10-125-6 docker_code]# docker exec -it 24 /bin/bash
test_new4@a639b74bd664:/$ id
uid=1000(test_new4) gid=1000(test_new4) groups=1000(test_new4),0(root)
test_new4@a639b74bd664:/$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
1000 1 0.1 0.0 46936 13524 ? Ss 02:39 0:00 /usr/bin/python /usr/bin/supervisord
1000 11 0.0 0.0 18048 3236 ? Ss 02:39 0:00 /bin/bash
1000 58 0.0 0.0 15292 2116 ? R+ 02:43 0:00 ps aux
test_new4@a639b74bd664:/$ exit
exit
关于-u的介绍,可以看官方文档,也可以参考下面的
The default user within a container is root (id = 0), but if the developer created additional users, those are accessible too. The developer can set a default user to run the first process with the Dockerfile USER instruction, but the operator can override it: