John
John

Reputation: 85

Unable to mount volumes to docker-machine from ubuntu 18.04 host

I am trying to Dockerize the React application on Ubuntu 18.04 host to node 12 guest. I am following this guide https://mherman.org/blog/dockerizing-a-react-app/ and everything works fine until I get to mounting volumes to the docker-machine. After mounting the volume the target folder on guest is empty or even removes the existing files already mounted in the image.

Following is my Dockerfile:

# base image
FROM node:12.2.0-alpine
RUN apk add --no-cache git

# set working directory
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY ./package.json /app/package.json
RUN npm install --silent
RUN npm install [email protected] -g --silent

# start app
CMD ["npm", "start"]

create a docker-machine and configure shell to connect to it :

docker-machine create -d virtualbox lz-front
eval $(docker-machine env lz-front)

Mount the project root-folder

sanka@ThinkPad-P1:~/code/lz/lz-new$ VBoxManage sharedfolder add lz-front --name lz-new --hostpath /home/sanka/code/lz/lz-new/ --automount

Build the image

sanka@ThinkPad-P1:~/code/lz/lz-new$ docker build -t lz:dev .

Inspecting what is inside reveals all is as expected:

sanka@ThinkPad-P1:~/code/lz/lz-new$ docker run -it --rm lz:dev sh
/app # ls
node_modules       package-lock.json  package.json
/app # 

But after mounting volumes the target folder contains just node_modules

sanka@ThinkPad-P1:~/code/lz/lz-new$ docker run -v ${PWD}:/app -v /app/node_modules -p 3001:3000 -it --rm lz:dev sh
/app # ls
node_modules
/app # 

If I run the run command outside of the docker-machine the application starts up normally. So I am suspecting VirtualBox auto-mount doesn't work for me.

EDIT: this shows the content of the working directory

sanka@ThinkPad-P1:~/code/lz/lz-new$ ls
config-overrides.js  Dockerfile    package.json  README.md  test
docker-compose.yml   node_modules  public        src

EDIT 2: after logging into docker-machine with ssh I can validate that the auto-mounting shared folder works as they are present in the VM

sanka@ThinkPad-P1:~/code/lz/lz-new$ docker-machine ssh lz-front
docker@lz-front:~$ ls /
bin       home      lib       lz-new    proc      sbin      usr
dev       hosthome  lib64     mnt       root      sys       var
etc       init      linuxrc   opt       run       tmp

Upvotes: 1

Views: 734

Answers (1)

John
John

Reputation: 85

Changing the VBoxManage command to:

sanka@ThinkPad-P1:~/code/lz/lz-new$ VBoxManage sharedfolder add lz-front --name ${PWD} --hostpath ${PWD} --automount

solves the issue. My best guess is that volumes are mounted from the VM users folder rather then from host folder directly. This folder is empty at the time of executing docker run. Mounting the directory to required place into the VM prior to executing docker run enables proper mounting.

Upvotes: 1

Related Questions