Docker mount files and volumes to relative path without using UNIX/Windows-specific syntax
Answer a question
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?
Answers
My Solution
My issue wasn't necessarily related to not being able to access relative paths in Docker. While relative paths are not easily usable through docker run (you must use %cd%, $(pwd), etc. depending on your OS), relative paths are supported in docker-compose.yml as pointed out by David. I was attempting to access this volume during build-time, which isn't possible per my configuration during. I instead will copy the configuration file over during one build-time layer, but use the volume for when the image is running.
Other Resources
- Enable Docker BuildKit
- Mount volumes during build using Docker BuildKit
更多推荐
所有评论(0)