大网数据广告活动
服务器运维问答

How can I set the root password in a docker container from a script?

· 52CE 我爱测

I have a script that runs on the host and creates/starts/stops a docker container. I'd like the script to change the password of the root user within the container. Since the container is an ssh server, I tried: sshpass -p 'OLDPA…

问题原文

原帖链接 · Chris · CC BY-SA 3.0

I have a script that runs on the host and creates/starts/stops a docker container. I'd like the script to change the password of the root user within the container.

Since the container is an ssh server, I tried: sshpass -p 'OLDPASS' ssh root@<container-IP> 'echo -e "NEWPASS\nNEWPASS" | passwd root'

but it doesn't work. Before going ahead and spending more time debugging it, I'd like to know if there's a smarter way to do it.

I understand that the proper "docker way" is to make a script that is run by the Dockerfile, which pulls the password from a shared volume and sets it as the root password. This sounds complicated, but I know how to do it and works well for another docker image I use. But I don't want to do it for this one.

I just need a command that uses Docker or ssh to change a user's password non-interactively.

已采纳解答

原帖链接 · Chris · CC BY-SA 3.0

This works flawlessly on Ubuntu 14.04.4 LTS:

In the script that rebuilds the container (which should be running on the "host"), add these lines:

$PASS='<a-good-password>'
echo -e "$PASS\n$PASS" | sudo docker exec -i <container-id-or-name> passwd

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-04-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。