将docker run命令输出存储在BASH变量中 [英] Store `docker run` command output in BASH variable

查看:208
本文介绍了将docker run命令输出存储在BASH变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将BASH变量中存储docker run -it -d -p 43211:3000 --name appname -h hostname -v $PWD/local_dir:/root/remote_dir repo/imagename的输出时遇到问题.我尝试了`backticks`,也尝试像官方文档所说的BASH_VAR=$(docker run ...)一样运行它,我什至尝试使用docker run --...>$FILE_DESCRIPTOR将输出存储在文件中,但是没有运气来存储错误情况,即已经使用名称的情况通过另一个容器,例如:

I'm having an issue storing the output of docker run -it -d -p 43211:3000 --name appname -h hostname -v $PWD/local_dir:/root/remote_dir repo/imagename in a BASH varibale. I tried `backticks`, I also tried running it like the official docs say BASH_VAR=$(docker run ...), I even tried storing the output in a file with docker run --...>$FILE_DESCRIPTOR, but no luck storing the error situation, the situation when the name is already used by another container, like so:

$ FATA[0000] Error response from daemon: Conflict. The name "appname" is already in use by container 7c84d8d703c8. You have to delete (or rename) that container to be able to reuse that name.

$ FATA[0000] Error response from daemon: Conflict. The name "appname" is already in use by container 7c84d8d703c8. You have to delete (or rename) that container to be able to reuse that name.

我想说的是,它适用于成功情况,因此,成功运行应用程序后,我可以将完整的容器ID存储在BASH_VAR中,但是不幸的是,这只能解决我面临的一半问题.

I want to say that it works for the success situation, so I'm able to store in BASH_VAR the full container ID, upon running the application successfully, but unfortunately this solves only half the problem I'm facing.

任何帮助将不胜感激.

Any help would be appreciated.

谢谢!

推荐答案

您需要捕获标准错误并将其存储在变量中.

What you need is to capture standard error and store it in a variable.

使用

BASH_VAR=$(command)

BASH_VAR=`command`

将捕获标准输出,而不是标准错误.

will capture standard output and not standard error.

这是将标准错误消息存储在变量中的正确语法:

This is the right syntax to store standard error messages in a variable:

BASH_VAR=$( { command; } 2>&1 )

这篇关于将docker run命令输出存储在BASH变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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