如何使用.bat文件运行传递docker terminal命令 [英] how to run pass docker terminal commands using .bat file

查看:454
本文介绍了如何使用.bat文件运行传递docker terminal命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用bat文件调用docker compose.我试图使用以下命令来调用,但是它不执行命令.这是我的.bat文件

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

还有其他方法可以使用bat文件或任何其他文件执行docker命令.

Is there any other way to execute docker commands using bat file.or any other file.

推荐答案

批处理文件是脚本文件.脚本需要一个解释器.对于批处理文件,解释器是Windows命令解释器 cmd.exe .

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.

*.sh文件也是需要解释器的脚本.解释器在Unix/Linux系统 sh bash ksh ,...上,它们也是可执行文件,但没有文件扩展名.exe ,因为在Unix/Linux上,可执行文件通常没有文件扩展名.

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.

在Windows上,默认情况下未安装Unix/Linux Shell脚本的解释器.如果 start start.sh 全部起作用,那是因为在Windows上安装了Shell解释器,该解释器已在Windows注册表中注册为用于打开* .sh文件的应用程序,这意味着解释了Shell脚本文件中的命令通过启动的应用程序.

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.

但是由 cmd.exe 解释并在Windows命令环境中执行的批处理脚本中的命令不能在shell解释器的shell环境中执行.

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 启动一个新进程,该进程与为执行批处理文件而创建的Windows命令进程并行运行.在此行之后,批处理文件处理将立即继续,并在命令解释器中执行下一个命令,而Shell解释器进程在 start.sh 中并行解释命令.

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.

因此,这里需要一个批处理文件,该文件创建一个Shell脚本来调用 start.sh 并通过Shell解释器在Shell环境中执行其他Shell命令.

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.

下面的批处理代码可能有效.我没有对其进行测试,因为我的Windows计算机上没有安装 Docker Toolbox 或任何外壳解释器.

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.

  • 调用/? ...说明%〜n0 (不带文件扩展名和路径的批处理文件的名称)
  • del/?
  • echo/?
  • 设置/?
  • 开始/?
  • call /? ... explains %~n0 (name of batch file without file extension and path)
  • del /?
  • echo /?
  • set /?
  • start /?

另请阅读有关使用命令重定向操作符的Microsoft文章.重定向操作符> 的说明,此处用于创建要使用3行代码执行的shell脚本:

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

这3行是由Shell脚本解释器在Shell环境中执行的.

Those 3 lines are executed in the shell environment by shell script interpreter.

这篇关于如何使用.bat文件运行传递docker terminal命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆