How to run MySQL commands in a Docker container.
#!/bin/bash
set -a; source <(cat .env | sed -e '/^#/d;/^\s*$/d' -e "s/'/'\\\''/g" -e "s/=\(.*\)/='\1'/g"); set +a
docker exec wp_db mysql -uroot -p${MYSQL_PASSWORD} -e " \
use db1; \
select * from wp_options where option_name='siteurl'; \
select * from wp_options where option_name='home';" 2>/dev/null
- The first command loads the content of .env to environment variables.
- The MySQL password can then be used using the ${MYSQL_PASSWORD} variable.
- I’m using sed to get around the special characters in the password.
- The second command runs docker exec on wp_db container.
- The last 3 commands are the actual sql commands.
- We are selecting to use the db1 database.
- Then we run select statements from the wp_options table.
- Finally, 2>/dev/null suppresses errors and warning to null.