Docker-compose - 设置不是文字的环境变量 [英] Docker-compose - setting environment variables that are not literals

查看:24
本文介绍了Docker-compose - 设置不是文字的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 Docker 容器中设置了 Jenkins,我正在尝试使用该服务器访问我的私人 Bitbucket 存储库.我需要将我的 SSH 密钥复制到该容器中,以便 Bitbucket 识别它,然后我可以让我的 Jenkins 服务器访问该存储库.

I have setup Jenkins within a Docker container and I am trying to access that my private Bitbucket repo with that server. I need to copy my SSH key into that container so that Bitbucket recognizes it and I can have my Jenkins server access the repo then.

我的 docker-compose.yml 文件中有以下内容:

I have in my docker-compose.yml file the following:

services:
  jenkins:
    build: .
    volumes:
      - jenkins-data:/var/jenkins_home
    environment:
      - SSH_PRIVATE_KEY=$(cat ~/.ssh/id_rsa)
    ports:
      - "8080:8080"
      - "50000:50000"

volumes:
  jenkins-data:

然而,echo $SSH_PRIVATE_KEY 给出了 /.ssh/id_rsa 字面意思,而不是存储在里面的值.我听说在 Dockerfile 中执行此操作的问题在于它仍然可以在将被推送的图像层之一中查看.

However, echo $SSH_PRIVATE_KEY gives /.ssh/id_rsa literally instead of the value stored inside. I have heard the problem with doing this inside the Dockerfile instead would be that it still can be viewed in one of the layers of the image that will be pushed.

我的问题是如何将 SSH_PRIVATE_KEY 的值设置为我的文件内容的值?

My question is how can I set the value of SSH_PRIVATE_KEY to the value of the contents of my file?

我相信这可能是 如何使用 docker-compose 将环境变量设置到 docker 容器中 但是该解决方案似乎对我没有任何改变.

I believe this could be a duplicate of How to set environment variable into docker container using docker-compose however that solution does not appear to change anything for me.

推荐答案

您可以在运行 compose 的 shell 中创建环境变量:

You could create an Environment variable in your shell from which you are running your compose :

export SSH_PRIVATE_KEY=$(cat ~/.ssh/id_rsa)

然后在您的撰写中使用它,例如:

and then use it in your compose like :

services:
  jenkins:
    build: .
    volumes:
      - jenkins-data:/var/jenkins_home
    environment:
      - SSH_PRIVATE_KEY
    ports:
      - "8080:8080"
      - "50000:50000"

它应该从 文档 :

容器中变量的值取自运行 Compose 的 shell 中相同变量的值.

The value of the variable in the container is taken from the value for the same variable in the shell in which Compose is run.

这篇关于Docker-compose - 设置不是文字的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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