I'm trying to write a Docker image for a Discord bot that can run both on my Windows PC for testing and Linux server for production use. I want to be able to Git pull my code on any OS, edit a configuration file in this repository, then simply do docker-compose up
to build and run my image (which accesses the configuration file; I don't want to copy this into the image as I may need to edit this file without editing the code/image itself). However, this seems to not be possible, because I do not have a way of guaranteeing where the directory in which this configuration file is located. I have seen workarounds in the official Docker docs that say you should use $(pwd)/path/to/file
to access your current directory, however, this only works in UNIX-based systems, and only when using this as an argument of the docker build
command. Using $(pwd)
or %cd%
is not accepted in docker-compose.yml
, throwing the error:
yaml.scanner.ScannerError: while scanning for the next token
found character '%' that cannot start any token
I have also already tried to specify paths such as path/to/file
and ./path/to/file
in both docker run -v <volume paths>
and docker-compose.yml
, to no avail. While no error is thrown when using ./path
in docker run
, no files or directories can be found in the image when running.
An example of me trying this with docker-compose.yml: Dockerfile
FROM alpine
RUN ls /usr/src/testdir/
docker-compose.yml
version: "3.8"
services:
test:
build: ./
volumes:
- ./testdir/:/usr/src/testdir
When there's a folder in the same folder as docker-compose.yml
named testdir
with one file in here, running the command docker-compose up
returns:
ls: /usr/src/testdir/: No such file or directory
ERROR: Service 'test' failed to build : The command '/bin/sh -c ls /usr/src/testdir/' returned a non-zero code: 1
Considering the "write-once, run anywhere" mentality of Docker, it dumbfounds me knowing that Docker doesn't provide any easy way to mount volumes by relative path. Is there a way to use a relative path in docker-compose.yml
and/or docker run
without the use of OS-specific syntax?