Mysql client called with `docker-compose run` vs. `docker-compose exec`

对着背影说爱祢 提交于 2020-12-08 15:51:08

问题


Why do you need to specify a host when calling with docker-compose run?

e.g.

docker-compose run db_container mysql -uuser -ppass db_name -h db_container

seems to be the direct equivalent of

docker-compose exec db_container mysql -uuser -ppass db_name

When omitting the hostname flag from the first example, mysql fails with a "can't connect to socket" error.

What is the difference between the two examples?


回答1:


docker-compose run will start a new container on the same network with a name like folder_db_container_run_1. This is not running mysql since you passed it a command. So it is running that command. So you connect from this container to the original db container

docker-compose run db_container mysql -uuser -ppass db_name -h db_container

While when you do exec you get inside the running container. And not specifying host means local mysql

docker-compose exec db_container mysql -uuser -ppass db_name

That is why it works. No extra container is launched in this case



来源:https://stackoverflow.com/questions/46223409/mysql-client-called-with-docker-compose-run-vs-docker-compose-exec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!