4

I am relatively new to 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. When I run docker-compose up in my terminal I get the following warning:

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.

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.

When using ./ instead of $PWD I get the following error:

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
A Merii
  • 574
  • 9
  • 21
  • The filesystem spaces on your host and inside container are completely separate. For a PostgreSQL database, if you mount the data volume anywhere other than `/var/lib/postgresql/data` it just won't notice it. Do you have any reason to want a variable path inside the container? – David Maze Apr 03 '20 at 17:14
  • I am currently in the learning and experimentation phase, so I am trying to see what I can and can't do. – A Merii Apr 04 '20 at 07:22

1 Answers1

1

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. And yes, it's the shell that sets $PWD and $OLDPWD. 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. 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. 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.

hndcrftd
  • 3,180
  • 1
  • 21
  • 18
  • Thanks for the reply, the reason I wanted to use PWD was because I wanted to have the container run on my colleagues' PCs without needing them to configure the directories. What is the best way to do that? – A Merii Sep 15 '20 at 07:03
  • 1
    Folders inside containers are usually configured during the build process. However, if the data resides outside of the container, then you have a couple of options. First, you can load the data into a specific container folder from the build context, which is the folder where your Dockerfile is, and as long as you don't remove the container, that data will persist. Second, you can indicate a mount point for a container volume at the time you start the container as a command line parameter. Describe your intended usage in more detail. – hndcrftd Oct 05 '20 at 19:56