Mysql客户端使用docker-compose run与docker-compose exec进行调用 [英] Mysql client called with `docker-compose run` vs. `docker-compose exec`
问题描述
为什么使用 docker-compose run
调用时需要指定主机?
Why do you need to specify a host when calling with docker-compose run
?
例如
docker-compose运行db_container mysql -uuser -ppass db_name -h db_container
似乎直接等同于
docker-compose exec db_container mysql -uuser -ppass db_name
在第一个示例中省略主机名标志时,mysql失败,并显示无法连接到套接字"错误.
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?
推荐答案
docker-compose run将在同一网络上启动一个名称为 folder_db_container_run_1
的新容器.因为您向它传递了命令,所以它没有运行mysql.因此它正在运行该命令.因此,您可以从此容器连接到原始数据库容器
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
当执行exec时,您会进入正在运行的容器.并且不指定host意味着本地mysql
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
这篇关于Mysql客户端使用docker-compose run与docker-compose exec进行调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!