问题
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