2

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.

2
  • 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 ? Commented Jul 25, 2018 at 13:29
  • Ok, I was being stupid, when I wrote this: MYSQL_ALLOW_EMPTY_PASSWORD=hello MYSQL_RANDOM_ROOT_PASSWORD=hello Commented Jul 25, 2018 at 20:54

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 post your full docker compose file please
this docker file in the post is the one. Just delete lines MYSQL_ALLOW_EMPTY_PASSWORD=hello MYSQL_RANDOM_ROOT_PASSWORD=hello
1

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.

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.

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.

1 Comment

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

Bit late here: i also faced this issue. i took an ubuntu image and ran apt-get install mysql-server it worked fine in current instance but after commiting it into image it didnt work and i was unable to up the service as well.

so in ubuntu i made it to apt-get install mariadb-server.After commiting it in image in new container i ran service mysql start; mysql It worked fine !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.