I Want to invoke docker compose using bat file.I have tried to invoke using the following but its not executing the commands. this is my .bat file
echo on
cd C:\Program Files\Docker Toolbox\
start start.sh cd desktop
cd test
docker-compose up
Is there any other way to execute docker commands using bat file.or any other file.
A batch file is a script file. A script needs an interpreter. For a batch file the interpreter is cmd.exe
– the Windows command interpreter.
A *.sh file is also a script which needs an interpreter. The interpreter is on Unix/Linux systems sh
, bash
, ksh
, ... which are also executables but without file extension .exe
because on Unix/Linux executables usually do not have a file extension.
On Windows there is no interpreter installed by default for Unix/Linux shell scripts. If start start.sh
works at all than because of having installed a shell interpreter on Windows which has been registered in Windows registry as application for opening *.sh files which means interpreting the commands in the shell script file by started application.
But commands in a batch script interpreted by cmd.exe
and executed within a Windows command environment can't be executed in shell environment of the shell interpreter.
start start.sh
starts a new process running parallel to the Windows command process created for execution of the batch file. The batch file processing immediately continues after this line with executing the next command in command process while the shell interpreter process interprets parallel the commands in start.sh
.
So what you need here is a batch file which creates a shell script to call start.sh
and executes other shell commands in shell environment by shell interpreter.
The batch code below might work. It is not tested by me as I don't have Docker Toolbox nor any shell interpreter installed on my Windows machine.
@echo off
set "ShellScriptFile=%TEMP%\%~n0.sh"
( echo start.sh
echo cd desktop/test
echo docker-compose up
) >"%ShellScriptFile%"
start "Docker Compose" /D"%ProgramFiles%\Docker Toolbox" /wait "%ShellScriptFile%"
del "%ShellScriptFile%"
set "ShellScriptFile="
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
... explains %~n0
(name of batch file without file extension and path)del /?
echo /?
set /?
start /?
Read also the Microsoft article about Using Command Redirection Operators for an explanation of redirection operator >
used here to create the shell script to execute with the 3 lines:
start.sh
cd desktop/test
docker-compose up
Those 3 lines are executed in the shell environment by shell script interpreter.
To run docker-compose via a bat file,
1. create a bat file eg run-docker-compose-up.bat
cd c:\docker
docker-compose up -d
pause
c:\docker
is where I placed my docker-compose.yml
-d
run containers in the background docker compose up docs
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