76

Here's my docker-compose:

version: '2'
services:
  couchpotato:
    build:
        context: ./couchpotato
        dockerfile: Dockerfile
    ports:
     - 5050:5050
    volumes:
     - "${PWD}/couchpotato/data:/home/CouchPotato/data/"
     - "${PWD}/couchpotato/config:/home/CouchPotato/config/"

When I run it inside the shell, in the directory of the docker-compose.yml, I get:

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

and the compose starts with PWD being empty.

I don't see any error in the file, as seen here: https://docs.docker.com/compose/environment-variables/

7
  • What happens if you run env | grep PWD from the same shell?
    – BMitch
    Jan 31, 2017 at 1:19
  • 2
    @BMitch I get my normal PWD: /home/user/... and OLDPWD=...
    – user6791424
    Jan 31, 2017 at 1:22
  • And that's the exact same shell you used to run the docker-compose command? You didn't launch it with some kind of exec, through any kind of scheduler, etc? Because when I run something similar locally I can't reproduce this error under bash.
    – BMitch
    Jan 31, 2017 at 1:27
  • @BMitch nope, I've just run docker-compose up. That's what I did: echo $PWD and enter, got the PWD printed. Then docker-compose up in the next line, got the warning
    – user6791424
    Jan 31, 2017 at 1:28
  • 4
    In my case, I happened to be running docker-compose using sudo. Using the -E flag for sudo to preserve the existing environment variables solved this for me. sudo -E bash -c 'docker-compose up'
    – cjsimon
    Jan 21, 2020 at 0:25

4 Answers 4

92

You don't need ${PWD} for this, you can just make the path relative and compose will expand it (one major difference between compose paths and those processed by docker run).

version: '2'
services:
  couchpotato:
    build:
        context: ./couchpotato
        dockerfile: Dockerfile
    ports:
     - 5050:5050
    volumes:
     - "./couchpotato/data:/home/CouchPotato/data/"
     - "./couchpotato/config:/home/CouchPotato/config/"

As for why compose doesn't see this variable, that depends on your shell. Compose looks for an exported environment variable, contents of the .env file, and command line flags to the docker-compose command. If each of those comes up empty for the variable, you'll get that warning.

8
  • 14
    For whatever reason, Docker for Windows gets confused with relative directories. The ${PWD} approach works better. Jun 26, 2017 at 17:42
  • 7
    I;m using Docker compose with Windows Linux Subsystem. I can relate to the fact that relative paths (like ./config/nginx) are not working and gives the error not a directory. For me at least. Using ${PWD} instead of .solves the problem
    – Ionel Lupu
    Mar 7, 2018 at 12:47
  • 5
    Something to bare in mind, if you are using -f /path/to/compose.yml, then the relative volumes will be relative to the template. Sometimes it might be useful to have it relative to PWD, hence why you might want to include it. It's to do with the shell. Might have to manually set PWD if you are running automation without a shell.
    – dnk8n
    Aug 13, 2019 at 21:55
  • @boyd I am currently using ubuntu wsl2, and have docker and docker-compose installed on it there and not on windows. When trying to use ${PWD} , I get the following warning: The PWD variable is not set. Defaulting to a blank string.
    – A Merii
    Apr 3, 2020 at 15:33
  • 6
    The dot (.) and PWD variable can expand to different values, when the docker-compose.yml is not in the current dir: "." expand to the dir, where the YAML file is and PWD is your current dir. So they can serve different purposes.
    – pwes
    Oct 17, 2020 at 10:29
23

My advice: change all $PWD to .

5
  • 10
    This is exactly the same as BMitch's answer
    – MBorg
    Feb 17, 2020 at 12:04
  • 2
    In docker-compose.yml $PWD has different meaning than "."
    – pwes
    Oct 17, 2020 at 10:30
  • 2
    @MBorg no it isn't- this answer is a lot shorter.
    – markemus
    Jan 4, 2021 at 20:09
  • 7
    . is a path relative to the docker-compose file, while $PWD is an environment variable that usually contains the current working directory, from which the docker-compose command is ran. They are not the same, unless you ran dc from the same dir as the yml. Nov 22, 2021 at 18:13
  • I used to use this, the extremely frustrating problem with this is that now Docker errors with "volume name is too short, names should be at least two alphanumeric characters" and ${PWD} doesn't seem to work for me on Windows, even when executing it in Cygwin. Annoying because the whole point to using docker was for simplicity and cross compatibility. May 12, 2022 at 20:50
20

$PWD will not work if you are running using sudo. Try the recommended settings from Docker for Linux https://docs.docker.com/engine/install/linux-postinstall/.

Sudo will run as a different user, with a different env.

$ sudo env | grep -i pwd
$ env | grep -i pwd
PWD=/home/user
OLDPWD=/
1
  • 1
    This worked for me sudo PWD=${PWD} docker-compose build
    – Xoundboy
    Nov 15, 2022 at 15:16
3

If you really need absolute paths, then call this before calling docker-compose up:

set PWD=%CD%