Docker + mysql + Composer Gotchya!

Have you ever tried to use docker-compose to create a MAMP container?

I have!

And I ran into one of the most confusing problems ever.

Now, I ran docker-compose up -d several times, as I figured out how I wanted my docker-compose.yml configured to run my wordpress site.

Eventually I added environment variables for the MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD and MYSQL_ROOT_PASSWORD.

To my surprise, I could NOT get my wordpress site to connect to the database, and I could not log into the database from the command line as root or the MYSQL_USER.

It turns out, when I run docker-compose down it does not remove the project_mysql volume! As a result, when I run docker-compose up -d again, the project_mysql volume already exists, so it doesn’t re-create the database.  That is why my environment variables were being ignored!  (Replace project with the name of your project of course.)

To resolve this issue, I had to run docker-compose down followed by docker volume rm project_mysql.  The next time I ran docker-compose up -d, the environment variables were read by the docker-entrypoint.sh, and the database was re-created the way I wanted it to be.

Alternatively, I learned that I could run docker-compose down -v to delete volumes along with the containers specified in the docker-compose.yml.  This can be helpful, although it’s not always ideal, specifically when you want to keep changes you have made to your database between restarts.

This doesn’t appear to be documented anywhere.

I hope it saves you time in the future!

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.