Docker compose won't find $PWD environment variable

ShellDockerEnvironment Variables

Shell Problem Overview


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/

Shell Solutions


Solution 1 - Shell

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.

Solution 2 - Shell

My advice: change all $PWD to .

Solution 3 - Shell

$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=/

Solution 4 - Shell

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

set PWD=%CD%

Solution 5 - Shell

I had the same issue with one of my env vars. On looking at my bashrc file more closely, I found out that I hadn't exported that variable.
Before:
VAR=<value>
After:
export VAR=<value>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser6791424View Question on Stackoverflow
Solution 1 - ShellBMitchView Answer on Stackoverflow
Solution 2 - ShellkoualskyView Answer on Stackoverflow
Solution 3 - ShellMarcelo MilhomemView Answer on Stackoverflow
Solution 4 - ShellJthorpeView Answer on Stackoverflow
Solution 5 - ShellAshwin A.VardhanView Answer on Stackoverflow