Can I run two mysql commands when connecting to a docket container?

Issue

I have a brief script to connect to a docker container and login:

docker exec -it mysql mysql --password=password

The first mysql is the name of the container, the second is the first command I’d like to run.

This works fine, I end up with a mysql prompt inside the container, but then I have to run something like:

use mydatabase

I’d rather do this as a one-liner, just to save time. Is this possible?

Solution

To run multiple commands in one line on a container you can write something like this:
docker exec -it <container> <bash|sh> -c "<command1> && <command2> && <command3>"

In your case, for instance:
docker exec -it mysql sh -c "mysql --password=password && use mydatabase"

Using && delimiter instead of ; will execute the next command only if the previous one has completed successfully (0 exit code)

Answered By – Pexers

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published