Docker Community Forums

Share and learn in the Docker community.

Docker-compose volume problem

docker

(Xandervolt) #1

Hello,
I built an Express/Node application, created an image for it, and run a container which works fine on its own. Heres the Dockerfile for it…

FROM node:10

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm install --only=production

# Bundle app source
COPY . .

EXPOSE 8080
CMD [ "npm", "start" ]

And I need to connect this application to a MongoDB container, which I do in my docker-compose file …

version: "3"
services:
  app:
    container_name: database
    restart: always
    build: .
    ports:
      - "8080:8080"
    links:
      - mongo
  mongo:
   container_name: mongo
    image: mongo
    volumes:
      - /data:/data/db
    ports:
      - "27017:27017"

My question is - do I have my volume syntax right? At first I was using ‘- ./data:/data/db’, with the dot in the beginning since the data folder needs to be in my app directory where the docker-compose file is. But every time I tried to run docker-compose up with the dot in the beginning of the volume path mongo would give an error and stop running. If I delete the volume from the docker-compose file altogether or if I remove the dot and run docker-compose up it seems to build fine without any errors.
I’m developing on a Windows machine so I’m concerned leaving the dot out of the volume path might work on Windows but probably wont work in an Ubuntu environment which is what I plan to use in production.
Thanks for any help you can give.


(Xandervolt) #2

apparently MongoDB and mapped volumes in Docker on Windows are not compatible which is why
‘- ./data:/data/db’ does not work without errors, and ‘- /data:/data/db’ is the wrong syntax so it didnt work at all.

This really sucks. Does anyone know of a workaround? Or has anyone used something like a ‘mongo-express’ container for a development database?


(Tekki) #3

/data is not a wrong syntax, just this folders doesn’t exists (Windows paths start with a drive letter).
Windows volumes are connected as a CIFS mount, where a many of the features of the file system are missing. This may be the reason why it still doesn’t work with the correct path.
The question is, do you really need access to the db files from Windows or do you just need persistent data? For the second create a named volume (https://docs.docker.com/compose/compose-file/#volume-configuration-reference).


(Xandervolt) #4

thanks for the reply. You’re probably right, I probably dont really need the db mapped in a development environment. It would just be nice to see how MongoDB works behind the scenes a little more since I’m new to MongoDB and NoSQL databases.

Probably the best solution is to not map the db volume in development and map it in production for backups etc. Thanks again


#5

Actually I don’t think this is correct. I’m using docker-compose (1.22.0 on Fedora 29) and get the same problem.

Docker-compose doesn’t seem to allow creating volumes with a dotfile in the mount name on either side of the stanza - works fine without one.

This works:

volumes:
  - data:/dir/subdir

This doesn’t work using docker-compose. Volume is created just fine by docker when brought up without docker-compose:

volumes:
  - data:/dir/.subdir

Anyone know of a way around this?


(Xandervolt) #6

Are you trying to map the volume to a hidden folder? I haven’t tried that. I was trying to use the dot to tell docker to use the current directory, not map it from a hidden directory


#7

Probably the same problem - it seems docker-compose doesn’t understand dots in file expressions.


(Xandervolt) #8

docker-compose understands dots. you probably just cant write to a hidden folder