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

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

问题描述

我已经在Docker容器中设置了Jenkins,并且尝试通过该服务器访问我的私有Bitbucket存储库。我需要将SSH密钥复制到该容器中,以便Bitbucket能够识别它,然后我的Jenkins服务器可以访问该存储库。



我在docker-compose中拥有。 yml文件如下:

 服务:
jenkins:
生成:。
卷:
-詹金斯数据:/ var / jenkins_home
环境:
-SSH_PRIVATE_KEY = $(cat〜/ .ssh / id_rsa)
端口:
- 8080:8080
- 50000:50000

量:
jenkins-data:

但是, echo $ SSH_PRIVATE_KEY 给出 /。ssh / id_rsa 从字面上代替存储在其中的值。我听说在Dockerfile中执行此操作的问题是它仍然可以在将要推送的图像的一层中查看。



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



我相信可能是如何设置环境变量的副本

您可以使用docker-compose 将其放入docker容器,但是该解决方案似乎对我没有任何改变。

解决方案

在运行shell的shell中创建一个环境变量:

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

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

 服务:
jenkins:
构建:。
卷:
-jenkins-data:/ var / jenkins_home
环境:
-SSH_PRIVATE_KEY
端口:
- 8080:8080
- 50000:50000

它应该从shell中为容器获取环境变量的值 docs 中指定的环境:


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



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.

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:

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.

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

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.

解决方案

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"

It should pick up the value for your environment variable for container from shell environment as specified in the docs :

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天全站免登陆