I created docker-compose.yml file, which creates MySQL image and sets the password of MySQL root user to "hello".
# docker-compose.yml
version: '3.1'
services:
mysql:
image: mysql:5.6.40
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
- MYSQL_ROOT_PASSWORD=hello
- MYSQL_ALLOW_EMPTY_PASSWORD=hello
- MYSQL_RANDOM_ROOT_PASSWORD=hello
Then I run:
sudo docker-compose up # (1)
... from the directory with this file.
The first problem is that the newly created container starts running in the foreground but not in bash and I can't put it in the background without exiting it, which I can do by Ctrl+C, or somehow enter bash, getting out from this process.
But when I open a new terminal window and run:
sudo docker exec -it bdebee1b8090 /bin/bash # (2)
..., where bdebee1b8090 is the id of the running container, I enter bash, where I can enter MySQL shell as root user, entering password "hello":
mysql -u root -p # (3)
Then I exit MySQL shell and bash shell in the container without stopping the container.
And then I commit changes to the container:
sudo docker commit bdebee1b8090 hello_mysql # (4)
..., creating a image. And then, when I run the image:
sudo docker run -it --rm hello_mysql /bin/bash # (5)
... and try to start MySQL shell again as root user, entering password "hello", I get an error like
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
And even after I restart the MySQL server:
/etc/init.d/mysql restart # (6)
..., I get the same error.
All of the above commands were run on ubuntu.
Why is this happening?
Edit:
When I try to make those steps on MacOS High Sierra, I get stuck on the step (3), because when I try to enter the password "hello", it doesn't get accepted. This error is shown on the screen:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
And when I try to restart MySQL server in the container
/etc/init.d/mysql restart
..., the container restarts in the background
..., but when I run it again and try to repeat steps (2) and (3) it gives the same error, and when I restart the MySQL server again, the container restarts in the background again...
Edit2:
After I removed lines:
- MYSQL_ALLOW_EMPTY_PASSWORD=hello
- MYSQL_RANDOM_ROOT_PASSWORD=hello -
...it started to work on Mac, but still after I commit the container, and try to enter the MySQL shell, it gives and error like:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
.... again.




- MYSQL_ALLOW_EMPTY_PASSWORD=hello- what do you mean by this? and that- MYSQL_RANDOM_ROOT_PASSWORD=hello?MYSQL_ALLOW_EMPTY_PASSWORD=hello MYSQL_RANDOM_ROOT_PASSWORD=hello