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

Solution 1:

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

Solution 2:

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

user='root'
pass='newpassword'
chpasswd <<<"$user:$pass"

Solution 3:

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