title: MySQL server in a docker container doesn't work after commiting the container
body: <p>I created docker-compose.yml file, which creates MySQL image and sets the password of MySQL root user to "hello".</p>
  
  <pre><code># 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
  </code></pre>
  
  <p>Then I run:</p>
  
  <pre><code>sudo docker-compose up # (1)
  </code></pre>
  
  <p>... from the directory with this file.</p>
  
  <p>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. </p>
  
  <p>But when I open a new terminal window and run:</p>
  
  <pre><code>sudo docker exec -it bdebee1b8090 /bin/bash # (2)
  </code></pre>
  
  <p>..., where bdebee1b8090 is the id of the running container, I enter bash, where I can enter MySQL shell as root user, entering password "hello":</p>
  
  <pre><code>mysql -u root -p # (3)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/dADBq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dADBq.png" alt="enter image description here"></a></p>
  
  <p>Then I exit MySQL shell and bash shell in the container without stopping the container.</p>
  
  <p>And then I commit changes to the container:</p>
  
  <pre><code>sudo docker commit bdebee1b8090 hello_mysql # (4)
  </code></pre>
  
  <p>..., creating a image. And then, when I run the image:</p>
  
  <pre><code>sudo docker run -it --rm hello_mysql /bin/bash # (5)
  </code></pre>
  
  <p>... and try to start MySQL shell again as root user, entering password "hello", I get an error like </p>
  
  <pre><code>ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/EBngX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EBngX.png" alt="enter image description here"></a></p>
  
  <p>And even after I restart the MySQL server:</p>
  
  <pre><code>/etc/init.d/mysql restart # (6)
  </code></pre>
  
  <p>..., I get the same error.</p>
  
  <p>All of the above commands were run on ubuntu.</p>
  
  <p>Why is this happening?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>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:</p>
  
  <pre><code>ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/D3vxb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D3vxb.png" alt="b"></a></p>
  
  <p>And when I try to restart MySQL server in the container</p>
  
  <pre><code>/etc/init.d/mysql restart
  </code></pre>
  
  <p>..., the container restarts in the background</p>
  
  <p><a href="https://i.stack.imgur.com/7fONz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7fONz.png" alt="enter image description here"></a></p>
  
  <p>..., 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...</p>
  
  <p><strong>Edit2:</strong></p>
  
  <p>After I removed lines: </p>
  
  <pre><code>  - MYSQL_ALLOW_EMPTY_PASSWORD=hello
    - MYSQL_RANDOM_ROOT_PASSWORD=hello      - 
  </code></pre>
  
  <p>...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:</p>
  
  <pre><code>ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
  </code></pre>
  
  <p>.... again.</p>