我正在尝试将Ubuntu 18.04主机上的React应用程序Docker化为节点12来宾。我正在按照本指南https://mherman.org/blog/dockerizing-a-react-app/进行操作,直到将卷安装到docker-machine上为止一切正常。装入卷后,guest虚拟机上的目标文件夹为空,甚至删除映像中已装入的现有文件。
以下是我的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 react-scripts@3.0.1 -g --silent
# start app
CMD ["npm", "start"]
创建一个docker-machine并配置shell以连接到它:
docker-machine create -d virtualbox lz-front
eval $(docker-machine env lz-front)
安装项目根文件夹
sanka@ThinkPad-P1:~/code/lz/lz-new$ VBoxManage sharedfolder add lz-front --name lz-new --hostpath /home/sanka/code/lz/lz-new/ --automount
构建图像
sanka@ThinkPad-P1:~/code/lz/lz-new$ docker build -t lz:dev .
检查里面的东西可以看到所有的东西:
sanka@ThinkPad-P1:~/code/lz/lz-new$ docker run -it --rm lz:dev sh
/app # ls
node_modules package-lock.json package.json
/app #
但是在装入卷之后,目标文件夹仅包含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 #
如果我在docker-machine外部运行run命令,则应用程序将正常启动。因此,我怀疑VirtualBox自动挂载对我不起作用。
编辑: 这显示了工作目录的内容
sanka@ThinkPad-P1:~/code/lz/lz-new$ ls
config-overrides.js Dockerfile package.json README.md test
docker-compose.yml node_modules public src
编辑2: 使用ssh登录docker-machine后,我可以验证自动挂载的共享文件夹是否可以在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
答案 0 :(得分:1)
将VBoxManage命令更改为:
sanka@ThinkPad-P1:~/code/lz/lz-new$ VBoxManage sharedfolder add lz-front --name ${PWD} --hostpath ${PWD} --automount
解决了问题。我最好的猜测是,卷是从VM用户文件夹而不是直接从主机文件夹装入的。在执行docker run时,该文件夹为空。在执行docker run之前将目录挂载到VM的所需位置即可正确挂载。