I'm trying to start a service with docker-compose up
where the volume is referencing ${PWD}/data
. The data/
directory does not exist before docker-compose up
is run.
This causes an exception. I thought the docker-compose up
call would create {PWD}/data
rather than giving this exception.
test-docker-compose_tor_1 is up-to-date
Creating test-docker-compose_walletserver_1 ... error
ERROR: for test-docker-compose_walletserver_1 Cannot create container for service walletserver: failed to mount local volume: mount /tmp/test-docker-compose/data:/var/lib/docker/volumes/bitcoin-s/_data, flags: 0x1000: no such file or directory
ERROR: for walletserver Cannot create container for service walletserver: failed to mount local volume: mount /tmp/test-docker-compose/data:/var/lib/docker/volumes/bitcoin-s/_data, flags: 0x1000: no such file or directory
ERROR: Encountered errors while bringing up the project.
This is how i have the docker-compose.yml
defined. How do i get my docker-compose.yml
to create ${PWD}/data
if the directory does not exist?
version: "3.8"
services:
walletserver:
image: bitcoinscala/bitcoin-s-server:latest
entrypoint: ["/opt/docker/bin/bitcoin-s-server", "--datadir", "/bitcoin-s", "--conf", "/opt/docker/docker-application.conf"]
restart: on-failure
volumes:
- datadir:/bitcoin-s
volumes:
datadir:
name: "bitcoin-s"
driver: local
external: false
driver_opts:
o: bind
type: none
device: ${PWD}/data # note if i change this to ${PWD}, this works
Here are my versions
docker --version
Docker version 20.10.17, build 100c701
docker-compose version 1.29.2, build 5becea4c
CodePudding user response:
If you specify the volume as a bind mount in the service configuration, like this, it will work:
version: "3.8"
services:
walletserver:
image: bitcoinscala/bitcoin-s-server:latest
entrypoint: ["/opt/docker/bin/bitcoin-s-server", "--datadir", "/bitcoin-s", "--conf", "/opt/docker/docker-application.conf"]
restart: on-failure
volumes:
- ./data:/bitcoin-s
It looks like using the extended volume syntax has different behavior (arguably, the extended volume syntax has the correct behavior: automatically creating the directory may seem convenient, but it hides typos and can lead to unexpected behavior when you misspell a file and transform your config file into a directory).
CodePudding user response:
There are two different alternatives for mounting your directories from host into the container. One is using -v (or --volume) and the other one is --mount. The difference between these two is exactly what you asked for. By using -v docker would create the given directory in the location if it does not already exist and the other one returns an error.
Now for using this in docker-compose you can easily use the type
tag for selecting the -v instead of --mount.
You can also specify the user id of the container using user: "${UID}:${GID}"
or user: root
on your docker-compose.yml
You would probably need something like this:
version: "3.8"
services:
walletserver:
image: bitcoinscala/bitcoin-s-server:latest
user: "${UID}:${GID}"
entrypoint: ["/opt/docker/bin/bitcoin-s-server", "--datadir", "/bitcoin-s", "--conf", "/opt/docker/docker-application.conf"]
restart: on-failure
volumes:
- type: bind
source: "datadir"
target: "/bitcoin-s"