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

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.

PASSWORD=$(zenity --password --title="Docker" 2>/dev/null)

will open a popup, asking for password, and return it. No password stored in the script

If you have a docker container where you need to set a password, without caring to much about security, you could add a statement in the Dockerfile:

RUN echo "root:root" | chpasswd

This is not related to Docker. You need to explicitly say passwd that you are going to provide password from stdin.

chpasswd <<<"$user:$pass"

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:

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