简体   繁体   English

vscode 开发容器环境变量不暴露给 docker-compose

[英]vscode dev container environment variables not exposing to docker-compose

My docker-compose.yml looks like this我的docker-compose.yml看起来像这样

version: "3.8"
services: 
  vscode:
    volumes:
      - ..:/workspace:cached
      - $SSH_AUTH_SOCK:/ssh-agent
      - /var/run/docker.sock:/var/run/docker.sock
    environment: 
      - SSH_AUTH_SOCK=/ssh-agent

Problem is that vscode does not want to give any kind of possibility to do an equivalent of docker-compose run --env... thus I'm left with问题是vscode不想给出任何可能性来做相当于docker-compose run --env...所以我只剩下

WARNING: The SSH_AUTH_SOCK variable is not set. Defaulting to a blank string.

Is there any way for me to expose my variables from my host to the dev container without using an .env file or anything like that?有什么方法可以让我在不使用.env文件或类似文件的情况下将我的变量从我的主机公开到 dev 容器?

To expose environment variables from the host through a container, as per the documentation根据文档,通过容器从主机公开环境变量

You can pass environment variables from your shell straight through to a service's containers with the 'environment' key by not giving them a value, just like with docker run -e VARIABLE... :您可以将环境变量从 shell 直接传递到具有“环境”键的服务容器,方法是不给它们一个值,就像docker run -e VARIABLE...一样:

 web: environment: - DEBUG

The value of the DEBUG variable in the container is taken from the value for the same variable in the shell in which Compose is run.容器中DEBUG变量的值取自运行 Compose 的 shell 中相同变量的值。

Regarding specifically SSH_AUTH_SOCK you should have a look at SSH Agent forwarding inside docker compose container .关于SSH_AUTH_SOCK你应该看看SSH Agent forwarding inside docker compose container

Example例子

Right, problem is that environment variables are not being exposed properly even with doing that.对,问题是即使这样做,环境变量也没有被正确暴露。

A simple service using a variable DEBUG .使用变量DEBUG的简单服务。

services:
  test:
    image: busybox
    command: ["echo", "${DEBUG}"]
    environment:
      - DEBUG

Using it without exporting the variable, it does not work a warning is displayed in the output.在不导出变量的情况下使用它,它不起作用,output 中会显示警告。

# running the service
docker-compose up

# WARNING: The DEBUG variable is not set. Defaulting to a blank string.
# Starting d-compose-env_test_1 ... done
# Attaching to d-compose-env_test_1
# test_1  | 
# d-compose-env_test_1 exited with code 0

Now exporting the variable on the host before running it, and it works, the value of the variable is printed.现在在运行它之前在主机上导出变量,它可以工作,打印变量的值。

# exporting the variable
export DEBUG=test
# running the service
docker-compose up

# Recreating d-compose-env_test_1 ... done
# Attaching to d-compose-env_test_1
# test_1  | test
# d-compose-env_test_1 exited with code 0

I have opened an issue on github yesterday.我昨天在 github 上打开了一个问题。 The outcome is that if you use WSL , then you need to export this variable through your ~/.profile or ~/.bash_profile.结果是,如果您使用 WSL ,则需要通过 ~/.profile 或 ~/.bash_profile 导出此变量。

We are using a non-interactive login shell to probe the environment variables in WSL and use these as the "local" variables.我们正在使用非交互式登录 shell 来探测 WSL 中的环境变量并将它们用作“本地”变量。

https://github.com/microsoft/vscode-remote-release/issues/5806 https://github.com/microsoft/vscode-remote-release/issues/5806

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker-compose 未将环境变量传递给 docker 容器 - Docker-compose not passing environment variables to docker container 通过docker-compose将环境变量传递给Docker容器中的应用程序 - Passing environment variables to application in a Docker container through docker-compose 基于 docker-compose 环境变量将文件添加到 docker 容器 - Adding files to docker container based on a docker-compose Environment variables docker-compose 暴露远程容器上的端口 - docker-compose exposing ports on remote container Docker-compose 环境变量 - Docker-compose environment variables docker-compose 可以将环境变量传递到容器中吗? - docker-compose passing environment variables into container possible? 如何在docker-compose容器中重新加载环境变量,停机时间最短? - How to reload environment variables in docker-compose container with minimum downtime? 在构建阶段将docker-compose中的环境变量传递给容器 - Pass environment variables from docker-compose to container at build stage docker-compose不会初始化容器环境变量 - docker-compose does not initalize container environment variables VSCode 远程容器 - 未使用 docker-compose 在开发容器上安装扩展 - VSCode Remote Container - extensions not installing on dev container using docker-compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM