Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run pass docker terminal commands using .bat file

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.

like image 330
raj19 Avatar asked Oct 21 '25 18:10

raj19


2 Answers

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.

like image 62
Mofi Avatar answered Oct 23 '25 08:10

Mofi


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

  1. docker-compose must run as Administrator.
    While a bat file itself cannot be marked as run as Administrator, you can either
    • right click on the bat file and choose 'Run as administrator'
    • or create a shortcut to the bat file and mark it as run as Administrator
      (right click on shortcut, choose Properties, choose Advance, choose 'Run as administrator')
like image 31
browniebytes Avatar answered Oct 23 '25 07:10

browniebytes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!