The MySQL team has been maintaining a MySQL image on Docker. They have been listening to requests from the community and reacting quickly. So far, they have fixed two bugs that I reported and introduced a feature request that I suggested to make the server more secure.
My latest request was about password management in a MySQL container. I have mentioned in previous posts the compatibility problems introduced by MySQL 5.7 security enhancements. Let me recap the main issues here:
MySQL is secure by default.
The recommended method to install MySQL is
mysqld --initialize, which will generate a random password that the DBA will then use to access the server and change it. This method would be acceptable if we were still in the 1990s, where installing servers one by one was a common practice. In the world of automated deployed servers, this practice would kill productivity in most environments.
MySQL provides a script-friendly alternative,
mysql --initialize-insecure, which will not generate any password, and leave the server with old root without password, ready for another automated process to set a secure password. Despite the intimidating name, this method is not more insecure than it was before, because it is used in automated processes, where the security does not depend on the vendor decisions but on a careful deployment plan that include password generation and management at company level.
MySQL on Docker is insecure by default
The above problem came to bite the MySQL team when they released an image for Docker. Here, to deploy a container you need to execute:
docker run -e MYSQL_ROOT_PASSWORD=something -d mysql/mysql-server
Meaning that you need to pass your valuable password on the command line. There is an alternative. Instead of
MYSQL_ROOT_PASSWORD, you can pass the variable
MYSQL_RANDOM_ROOT_PASSWORD, which will generate a random password and you would then see the password in the logs:
$ docker logs mybox Initializing database Database initialized MySQL init process in progress... GENERATED ROOT PASSWORD: Ix.oqdovoj!AGSEtYBnOnebag]u
Here the situation is worse than the regular case where we install with an automated tool, because containers are not supposed to be modified post installation. They should work out of the box. The password that we pass on the command line should be the real one, but given the exposure it could only be temporary and then require a change with a further operation. The random password in the logs does not make the process simple for a script.
The latest change by the MySQL team, though, fixes the problem. Now you can pass a file name inside
MYSQL_ROOT_PASSWORD and the server installation procedure will get the password from that file. This way, there will be no more exposure, and a reliable password could be used from the beginning.
Here is an example of a script that creates a container with a secure password, which nobody needs to see on screen at any moment:
#!/bin/bash # Generate a random password (for demo purposes only: there are more secure methods) RANDOM_PASSWORD=$(echo $RANDOM | sha256sum | cut -c 1-10 ) # Save the random password to a file echo $RANDOM_PASSWORD > secretpassword # Create the .my.cnf file echo '[client]' > home_my_cnf echo 'user=root' >> home_my_cnf echo "password=$RANDOM_PASSWORD" >> home_my_cnf # set -x docker run --name mybox \ -v $PWD/secretpassword:/root/secretpassword \ -v $PWD/home_my_cnf:/root/home_my \ -e MYSQL_ROOT_PASSWORD=/root/secretpassword -d mysql/mysql-server $@
This script creates a random password, saves it to a file, and stores the file inside the container (using the
-v volume option). Inside the container, the installer reads the password from the file and proceeds like before. The difference is that this password has not been shown on screen or in the logs. Notice that this script has also written the password to a second file, which is in the format required by MySQL for its options (same as
/etc/my.cnf.) We can use this file to connect without typing a password.:
$ docker exec -ti mybox mysql --defaults-file=~/home_my Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.10 MySQL Community Server (GPL) Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
We can't deploy the configuration file as /root/.my.cnf because the server would read it before installing, i.e. before the password has been set, and thus the installation will fail. Once the installation is concluded, though, we can do it, and access mysql without a password.
$ docker exec -ti mybox sh -c 'cp ~/home_my ~/.my.cnf' $ docker exec -ti mybox mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.10 MySQL Community Server (GPL) Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>