Docker Community Forums

Share and learn in the Docker community.

run docker exec command from batch file in windows 10

docker

(Ikoutz) #1

I’m using Docker in Windows 10 Pro. I start my mysql container using docker-compose and when the container is up, I want to run these commands through a .bat file, here’s the content of the .bat file:

@echo on

docker exec mysql sh -c "echo \"DROP USER 'mysql_user'@'%' ;DROP DATABASE mysql_db;\" | mysql -u root -p${MYSQL_ROOT_PASSWORD}"
docker exec mysql sh -c "echo \"CREATE DATABASE mysql_db;CREATE USER 'mysql_user'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}';GRANT ALL PRIVILEGES ON *.* TO 'mysql_user'@'%';FLUSH PRIVILEGES;\" | mysql -u root -p${MYSQL_ROOT_PASSWORD}"
docker exec mysql sh -c "mysql -u root -p${MYSQL_ROOT_PASSWORD} mysql_db</root/backups/mysql_db.sql"

pause

When I run these commands one by one in command line, they are executed correctly. But when I try to execute the .bat file, it seems like the ocker exec commands are executed (a command line window opens and the executed commands are shown one by one, as well as the result of execution) but finally they are not executed (I realize it when I proceed to the application that uses the database) and I have to run them in command line one by one.

The .bat file exists in C:\Users… path (which is shared to Docker) and I run it either as administrator or not, either from command line or from windows, but the result is the same.

Any suggestions?