Caution When Using $PWD Variable in docker-compose.yml
Recently, while deploying services with Docker Compose, I used the $PWD variable in the docker-compose.yml file to specify the host path. However, I found that the specified path did not produce the expected results.

Problem Reproduction
First, let's look at a docker-compose.yml configuration file:
version: "3.9"
services:
xui:
image: enwaiax/x-ui
container_name: xui
volumes:
- $PWD/db/:/etc/x-ui/
- $PWD/cert/:/root/cert/
restart: unless-stopped
network_mode: host
I placed the docker-compose.yml file in the /data/apps/x-ui directory. The configuration above appears to have no issues; it uses the $PWD variable to represent the current directory. According to expectations, the host absolute mount paths should be:
/data/apps/x-ui/db//data/apps/x-ui/cert/
When I executed sudo docker-compose up -d to run the container, no errors were reported, but the host mount paths did not work as expected. The actual host absolute mount paths became:
/db//cert/
It seems the $PWD variable was lost or did not take effect?
Problem Analysis and Solution
I then consulted ChatGPT about this issue, and the answer was:
It appears your problem is indeed that the $PWD variable is not taking effect. This may be because when using sudo, the environment variables are not passed to the docker-compose command.
To solve this problem, you can try the following methods:
- Replace
$PWDwith an absolute path. For example, use/data/apps/x-uias the prefix for the mount path:
version: "3.9"
services:
xui:
image: enwaiax/x-ui
container_name: xui
volumes:
- /data/apps/x-ui/db/:/etc/x-ui/
- /data/apps/x-ui/cert/:/root/cert/
restart: unless-stopped
network_mode: host
- Use the
sudo -Eoption to preserve environment variables:
sudo -E docker-compose up -d
This ensures that the $PWD variable remains effective when executing the docker-compose command. However, note that using the -E option may pose certain security risks, as it passes all environment variables of the current user to the sudo command. Please ensure you understand the potential risks and verify that no sensitive information variables are present in the current environment.
Additional Solution
In the problem above, ChatGPT provided two solutions that both resolve the $PWD variable failure issue. There is actually another solution: using a relative path, such as:
version: "3.9"
services:
xui:
image: enwaiax/x-ui
container_name: xui
volumes:
- ./db/:/etc/x-ui/
- ./cert/:/root/cert/
restart: unless-stopped
network_mode: host
Then, enter the /data/apps/x-ui directory and execute sudo docker-compose up -d.
Conclusion
When using the sudo command, some environment variables may not be correctly passed to subsequent commands. To solve this problem, we can use the sudo -E parameter to preserve environment variables, or avoid relying on environment variables and instead use absolute or relative paths. This point needs special attention, especially when ordinary users operate Docker via the sudo command.