在运行脚本的docker容器之外访问bash脚本变量 [英] Access a bash script variable outside the docker container in which the script is running

查看:40
本文介绍了在运行脚本的docker容器之外访问bash脚本变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在docker容器中运行的bash脚本.在此脚本中,我设置了一些变量的值.

I have a bash script running inside a docker container. In this script, I set the value of some variable.

我可以以某种方式在容器外部访问此变量的值吗?

我试图将变量设置为"global",但无法弄清楚该怎么做.将所需变量设置为容器内的环境变量是一个好主意吗?

I tried to make the variable "global" but could not figure out how to do it. Is it a good idea to make the required variable an environment variable inside the container?

如何复制

创建一个名为 temp.sh 的bash脚本,其内容如下:

Create a bash script called temp.sh with the following contents:

a=$RANDOM

现在,按如下所示在docker容器中运行此文件:

Now, run this file in a docker container as follows:

 docker run -it --rm -v $(pwd):/opt alpine sh -c "sh /opt/temp.sh"

所需行为:要能够在Docker容器外部访问变量 a

Desired behaviour: To be able to access the variable a outside the docker container

推荐答案

信用:

  • 我使用
  • 在docker文件系统上安装了目录
    1. I mounted a directory on the docker filesystem using

    docker run -v< host-file-system-directory>:< docker-file-system-directory>

    1. 在bash脚本中,我添加了 echo"$ variable"> docker-file-system-directory/variable.txt

    当我在docker文件系统上挂载了主机文件系统目录后,我仍然可以使用 cat< host-file-system-directory>/来访问 variable.txt .variable.txt

    As I had mounted a host filesystem directory on the docker filesystem, I can still access variable.txt simply using cat <host-file-system-directory>/variable.txt

    请注意, docker-file-system-directory 必须是绝对路径,而不是相对路径.

    这篇关于在运行脚本的docker容器之外访问bash脚本变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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