I'm trying to create a .my.cnf file for my MySQL docker-container that would allow me to prefill the user and password for the MySQL client. To achieve this I created the following dockerfile:

FROM mysql:5.7
RUN touch ~/.my.cnf && echo -e "[client]\nuser=root\npassword=$(echo $MYSQL_ROOT_PASSWORD)" >> ~/.my.cnf

But when starting the container I always get the errors:

mysql5.7-database    | 2021-04-01 18:38:51+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
mysql5.7-database    | 2021-04-01 18:38:51+00:00 [ERROR] [Entrypoint]: mysqld failed while attempting to check config
mysql5.7-database    |  command was: mysqld --verbose --help --log-bin-index=/tmp/tmp.5imGsNRXXC
mysql5.7-database    |  mysqld: [ERROR] Found option without preceding group in config file /root/.my.cnf at line 1!
mysql5.7-database    | mysqld: [ERROR] Fatal error in defaults handling. Program aborted!
  • The environment variable $MYSQL_ROOT_PASSWORD is successfully set with the correct password.
  • I can login with the password in the $MYSQL_ROOT_PASSWORD

The result of echo -e "[client]\nuser=root\npassword=$(echo $MYSQL_ROOT_PASSWORD)" is:


When I create the .my.cnf file manually in the running docker container, it works like a charm. Any advise?


The default Dockerfile shell is sh. I don't think echo there has the -e modifier for backslash. Newline & other special symbols will work by default. In your command, resulting .my.cnf would contain a litteral -e.

Remove -e or use the original command in Dockerfile in a different shell, for instance:

RUN /bin/bash -c '<your-command>'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.