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

Time:08-29

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?

CodePudding user response:

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)

  • Related