Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 466
Guybrush Threepwood Avatar asked Sep 14 '17 15:09

Guybrush Threepwood


People also ask

What is the difference between docker run and Docker exec?

Docker Run vs Docker Exec! This is a fairly common question – but has a simple answer! In short, docker run is the command you use to create a new container from an image, whilst docker exec lets you run commands on an already running container! Easy!

What is the difference between docker run and Docker compose up?

The key difference between docker run versus docker-compose is that docker run is entirely command line based, while docker-compose reads configuration data from a YAML file. The second major difference is that docker run can only start one container at a time, while docker-compose will configure and run multiple.

What is Docker compose exec?

Description. This is the equivalent of docker exec targeting a Compose service. With this subcommand you can run arbitrary commands in your services. Commands are by default allocating a TTY, so you can use a command such as docker compose exec web sh to get an interactive prompt.


1 Answers

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

like image 153
Tarun Lalwani Avatar answered Sep 22 '22 02:09

Tarun Lalwani