MySQL server in a docker container doesn't work after commiting the container

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


MySQL server in a docker container doesn't work after commiting the container



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)



enter image description here



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)



enter image description here



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)



b



And when I try to restart MySQL server in the container


/etc/init.d/mysql restart



..., the container restarts in the background



enter image description here



..., 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.





Did you read the manual for MySQL container? - MYSQL_ALLOW_EMPTY_PASSWORD=hello - what do you mean by this? and that - MYSQL_RANDOM_ROOT_PASSWORD=hello ?
– Alex
2 days ago


- MYSQL_ALLOW_EMPTY_PASSWORD=hello


- MYSQL_RANDOM_ROOT_PASSWORD=hello





Ok, I was being stupid, when I wrote this: MYSQL_ALLOW_EMPTY_PASSWORD=hello MYSQL_RANDOM_ROOT_PASSWORD=hello
– ibodi
yesterday




MYSQL_ALLOW_EMPTY_PASSWORD=hello MYSQL_RANDOM_ROOT_PASSWORD=hello




2 Answers
2



According to official documentation for MySQL docker images



https://hub.docker.com/r/mysql/mysql-server/



The boolean variables including MYSQL_RANDOM_ROOT_PASSWORD,
MYSQL_ONETIME_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD, and
MYSQL_LOG_CONSOLE are made true by setting them with any strings of
non-zero lengths. Therefore, setting them to, for example, “0”,
“false”, or “no” does not make them false, but actually makes them
true. This is a known issue of the MySQL Server containers.



Which means your config:


- MYSQL_ALLOW_EMPTY_PASSWORD=hello
- MYSQL_RANDOM_ROOT_PASSWORD=hello



equal to:


- MYSQL_ALLOW_EMPTY_PASSWORD = true
- MYSQL_RANDOM_ROOT_PASSWORD = true



Which bring us to the point:



MYSQL_RANDOM_ROOT_PASSWORD: When this variable is true (which is its
default state, unless MYSQL_ROOT_PASSWORD is set or
MYSQL_ALLOW_EMPTY_PASSWORD is set to true), a random password for the
server's root user is generated when the Docker container is started.
The password is printed to stdout of the container and can be found by
looking at the container’s log.



So either check your container log file to find random password generated
or just remove config parameter MYSQL_RANDOM_ROOT_PASSWORD from your file.


MYSQL_RANDOM_ROOT_PASSWORD





After commiting the container even without those two silly options MYSQL_ALLOW_EMPTY_PASSWORD=hello MYSQL_RANDOM_ROOT_PASSWORD=hello, mysql still doesn't work.
– ibodi
21 hours ago


MYSQL_ALLOW_EMPTY_PASSWORD=hello MYSQL_RANDOM_ROOT_PASSWORD=hello





@ibodi post your full docker compose file please
– Alex
21 hours ago





this docker file in the post is the one. Just delete lines MYSQL_ALLOW_EMPTY_PASSWORD=hello MYSQL_RANDOM_ROOT_PASSWORD=hello
– ibodi
9 hours ago


MYSQL_ALLOW_EMPTY_PASSWORD=hello MYSQL_RANDOM_ROOT_PASSWORD=hello



Docker images are generally designed to run a single process or server, in the foreground, until they exit. The standard mysql image works this way: if you docker run mysql without -d you will see all of the server logs printed to stdout, and there’s not an immediate option to get a shell. You should think of interactive shells in containers as a debugging convenience, and not the usual way Docker containers run.


mysql


docker run mysql


-d



When you docker run --rm -it /bin/bash, it runs the interactive shell instead of the database server, and that’s why you get the “can’t connect to server” error. As a general rule you should assume things like init.d scripts just don’t work in Docker; depending on the specific setup they might, but again, they’re not the usual way Docker containers run.


docker run --rm -it /bin/bash



You can install a mysql client binary on your host or elsewhere and use that to interact with the server running in the container. You don’t need an interactive shell in a container to do any of what you’re describing here.


mysql





But I want to create a db in a container, commit it and then, after running the created image, have access to the same db.
– ibodi
21 hours ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

1M023NRXSsQSpx3uak0HB6iJvsIghghdM
l,ON9Ci1nDK

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

415 Unsupported Media Type while sending json file over REST Template