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?
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!
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With