如何在我的CMD脚本中设置环境变量 [英] how to set env variables in my CMD script

查看:150
本文介绍了如何在我的CMD脚本中设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下条目文件

FROM <image-of-nodejs>

COPY docker/node/entry.sh /var/entries/entry.sh
RUN apt-get update
RUN apt-get install ant -y
CMD ["/var/entries/entry.sh"]

该图像由docker-compose使用文件:

the image is used by a docker-compose file:

version: "3.3"

services:
  my_node:
    build:
      context: ./
      dockerfile: docker/node/Dockerfile-build-dev
    volumes:
      - type: bind
        source: ./
        target: /var/proj

entry.sh文件如下:

and the entry.sh file is the following:

#!/bin/bash
export QNAMAKER_SUB_KEY=b13615t

如果我随后启动映像并进入docker,我将找不到我的env变量集:

If I then start the image and I enter the docker, I won't find my env variable set:

docker-compose up --force-recreate -d
docker-compose run my_node bash

root@9c081bedde65:/# echo ${QNAMAKER_SUB_KEY}

<empty>

我希望通过脚本设置变量来代替 ENV Dockerfile命令。怎么了?

I would prefer to set my variables throug my script in place of the ENV Dockerfile command. What's wrong?

推荐答案

这里有几件事。

首先, docker-compose up 不会在您以 docker-compose up 开始的容器内运行命令。它启动一个 new 容器以运行一次性命令。您可能想要 docker-compose exec

First, docker-compose run doesn't run a command inside the container you started with docker-compose up. It starts a new container to run a one-off command. You probably want docker-compose exec.

使用<$ c时看不到变量的原因$ c> docker-compose run 是通过提供新命令( bash CMD c $ c>)在 docker-compose run 命令行上。

The reason you don't see the variable when using docker-compose run is that you are overriding your CMD by providing a new command (bash) on the docker-compose run command line.

您可以考虑:


  • Dockerfile 中使用 ENV 语句

  • docker-compose.yml <中使用环境键/ li>
  • Using ENV statements in your Dockerfile.
  • Using the environment key in your docker-compose.yml

前者会将信息嵌入到您的图片中,而后者则意味着如果您未明确设置,则该变量将不会被设置它放在您的 docker-compose.yaml 文件中(或在 docker运行中使用 -e 命令行)。

The former will embed the information into your image, while the latter would mean that the variable would be unset if you didn't explicitly set it in your docker-compose.yaml file (or using -e on the docker run command line).

您可以使用 ENTRYPOINT 实现目标脚本并在那里设置值,但这不会影响y可见的环境ou使用 docker exec (或 docker-compose exec )时。

You may be able to accomplish your goal using an ENTRYPOINT script and setting the value there, but that won't impact the environment visible to you when using docker exec (or docker-compose exec).

这篇关于如何在我的CMD脚本中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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