简体   繁体   English

docker-compose 在 Ubuntu WSL 2 上找不到环境变量 $PWD

[英]docker-compose not finding environment variable $PWD on Ubuntu WSL 2

I am relatively new to docker.我对 docker 比较陌生。 I have been trying to compose the file below:我一直在尝试编写以下文件:

version: "3"

services: 

  postgres:
    restart: always
    image: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_DB=test_db
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD_FILE=/run/secrets/postgres-passwd
    volumes:
      - test_db:${PWD}

  pgweb:
    restart: always
    image: sosedoff/pgweb
    ports: 
      - "8081:8081" 
    environment:
      - DATABASE_URL=postgres://postgres:POSTGRES_USER@POSTGRES_PASSWORD_FILE:5432/POSTGRES_DB?sslmode=disable
    depends_on:
      - postgres

volumes: 
  test_db:

What I am trying to do is mount the volume test_db to my current working directory by using the environment variable $PWD.我要做的是使用环境变量 $PWD 将卷 test_db 挂载到我当前的工作目录。 When I run docker-compose up in my terminal I get the following warning:当我在终端中运行docker-compose up ,我收到以下警告:

The PWD variable is not set. Defaulting to a blank string.

Now it is important to note that I am currently using Ubuntu running on WSL2 on windows 10. Another thing to note is that I am running ZSH and not BASH.现在需要注意的是,我目前正在使用在 windows 10 上的 WSL2 上运行的 Ubuntu。另外需要注意的是,我正在运行 ZSH 而不是 ZE027C5FF7F2AD257E3AFZA0FF10B305B。

I followed the exact steps mentioned in the documentation .我遵循了文档中提到的确切步骤。

I also checked another question which seemed to be similar to mine but not quite the same, as it was possible to replace ${PWD} with ./ which simply does not work in my case.我还检查了另一个似乎与我相似但不完全相同的问题,因为可以用./替换${PWD} ,这在我的情况下根本不起作用。

When using ./ instead of $PWD I get the following error:当使用./而不是$PWD我收到以下错误:

for pg_test_postgres_1  Cannot create container for service postgres:\ 
invalid volume specification: 'pg_test_test_db:.:rw': invalid mount config\ 
for type "volume": invalid mount path: '.' mount path must be absolute

If you are trying to see what you can an cannot do, this is something you cannot do.如果你想看看你能做什么和不能做什么,这是你不能做的事情。 Docker does not load an environment variable that would normally be set by the shell, zsh or bash, doesn't matter. Docker 不会加载通常由 shell、zsh 或 bash 设置的环境变量。 And yes, it's the shell that sets $PWD and $OLDPWD.是的,设置 $PWD 和 $OLDPWD 的是 shell。 Docker CAN define a variable that will be passed to the distro as an environment variable and also be used by Docker at the time of the container build. Docker 可以定义一个变量,该变量将作为环境变量传递给发行版,并在容器构建时由 Docker 使用。 Also volumes need to be defined using absolute paths.还需要使用绝对路径定义卷。

Also, like David Maze mentions, your PostgreSQL data folder needs to be specifically in /var/lib/postgresql/data or that folder needs to be symlinked to a different arbitrary folder where PostgreSQL has read-write access.此外,就像 David Maze 提到的那样,您的 PostgreSQL 数据文件夹需要专门位于 /var/lib/postgresql/data 中,或者该文件夹需要符号链接到 PostgreSQL 具有读写访问权限的不同任意文件夹。 The point of a container is to build it for your needs so under normal circumstances you should know where everything goes and set volumes' paths explicitly.容器的重点是根据您的需要构建它,因此在正常情况下,您应该知道所有内容的去向并明确设置卷的路径。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM