Trying to mount a host volume on a Docker container on Windows. What is the source folder relative to?

2.7k Views Asked by At

I'm having trouble on trying to mount a volume to my docker container.

Here is my project structure

cloudRun
  distService/
    index.js
  Dockerfile
  package.json    // THIS IS THE package.json FOR THE DOCKER IMAGE

package.json      // THIS IS MY MAIN PROJECT package.json

In my main project package.json I have the following scripts:

"docker-run": "./scripts/docker/docker-run.sh",
"docker-inspect": "docker exec -ti hello-world sh"   // THIS IS USED TO INSPECT THE RUNNING CONTAINER

docker-run.sh

// STOPS ALL CONTAINERS
// REMOVES ALL CONTAINERS
// REMOVES ALL IMAGES

// BUILDS A NEW IMAGE FROM SCRATCH
docker build --tag hello-world:latest ./cloudRun

// TRYING TO RUN THE CONTAINER WITH A MOUNTED /distService VOLUME
docker run --name hello-world -p 3000:3000 -v //distService:/distService hello-world:latest

This is the Dockerfile:

FROM node:12-slim

WORKDIR /

COPY ./package.json ./package.json

RUN npm install

ENTRYPOINT npm start

It's all working. Except for the fact that the container is seeing /distService as an empty folder.

I know this because when I open a new terminal window and run:

npm run docker-inspect

I get to enter the folders and ls them. And this is what I'm getting: there is a distService folder, but the ls command comes back empty. PS: I did a ls on node_modules just to show that it works and the distService is indeed empty.

enter image description here

QUESTION

When I pass -v //folder:/folder, what is the source folder relative to? How can I be sure that I'm picking the right folder?

Environment:

Windows 10.
Docker for Windows installed
Docker Engine v19.03.13

UPDATE

I ran the container and inspected it with: docker inspect <CONTAINER_ID> to see what folder is being mounted. But it didn't help much. This is what came back:

"HostConfig": {
  "Binds": [
    "//distService/:/distService/"
],

// OTHER STUFF

"Mounts": [
  {
    "Type": "bind",
    "Source": "/distService",         // WHAT IS THIS PATH RELATIVE TO ?
    "Destination": "/distService",
    "Mode": "",
    "RW": true,
    "Propagation": "rprivate"
  }
],
2

There are 2 best solutions below

1
On

This is what I had to do to make it work.

Basicallly I had to put the full absolute path starting from c:/.

-v c:/Users/my-user/my-project/distService:/distService

Full command:

docker run --name hello-world -p 3000:3000 -v c:/Users/USER/my-project/distService:/distService hello-world:latest

I had the idea after I found this on the official docs:

enter image description here

3
On

With docker run local folder paths are always relative to the root. To get a relative path either use the PWD environment variable:

docker run -v $(PWD)/folder:/folder ...

Or, docker-compose does support relative paths if they start with a period:

version: '3.8'

services:
  nginx:
    image: nginx
    volumes:
    - ./folder:/www