[Ruby] What I did when the DB couldn’t come up with docker-compose up

1 minute read

When you build the environment with docker and do docker-compose up to start the server I get the error Mysql2::Error::ConnectionError: Unknown MySQL server host'db' (-2).

If you check the error log in the terminal, image.png It seems that the DB has shut down due to something.

Since it is privilege tables, are the user tables created with sudo? Why do I get this error in docker? I wondered, but I decided to delete all the volumes for the time being.


① Check the volume ID

$ docker volume ls
local b1d0c8467782387a61c446e1ea2aedaa745d6f120c4c6423bdc37e9005a7bd34

② Delete volume by rm command using VOLUME NAME checked above

$ docker volume rm [VOLUME NAME]
# Example ↓
$ docker volume rm b1d0c8467782387a61c446e1ea2aedaa745d6f120c4c6423bdc37e9005a7bd34

③ Restart container & server based on docker-compose.yml

$ docker-compose up --build
$ docker-compose run web rails db:create
$ docker-compose run web rails db:migrate

④ Access localhost:3000 → Start without error


Since the location where the table information of mysql is stored is volume, the volume container was not completely deleted when the container was deleted, and the previous mysql information remained halfway, it seems that the privileges were insufficient. is.

Before this, I made and deleted the container many times, and repeated the process of trial and error, so it would be the remains of that time…

Referenced article

Delete volume when db does not start up with docker-compose up