node_modules 上的 docker-compose 卷但为空 [英] docker-compose volume on node_modules but is empty
问题描述
我对 Docker 很陌生,我想在我的计算机上映射 node_modules 文件夹(用于调试目的).
I'm pretty new with Docker and i wanted to map the node_modules folder on my computer (for debugging purpose).
这是我的 docker-compose.yml
This is my docker-compose.yml
web:
build: .
ports:
- "3000:3000"
links:
- db
environment:
PORT: 3000
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
db:
image: mongo:3.3
ports:
- "27017:27017"
command: "--smallfiles --logpath=/dev/null"
我使用 Docker for Mac.当我运行 docker-compose up -d
一切正常,但它在我的电脑上创建了一个 node_modules 文件夹,但它是空的.我进入我的容器的 bash 和 ls node_modules,所有的包都在那里.
I'm with Docker for Mac. When i run docker-compose up -d
all go right, but it create a node_modules folder on my computer but it's empty. I go into the bash of my container and ls node_modules, all the packages was there.
如何在我的计算机上也获取容器上的内容?
How can i get the content on the container on my computer too?
谢谢
推荐答案
TL;DR 工作示例,克隆并尝试: https://github.com/xbx/base-server
首先(在运行容器之前)为了调试目的,您的计算机(外部图像)中需要一个 node_modules.
You need a node_modules in your computer (outside image) for debugging purposes first (before run the container).
如果你只想调试 node_modules:
If you want debug only node_modules:
volumes:
- /path/to/node_modules:/usr/src/app/node_modules
如果您想同时调试代码和 node_modules:
If you want debug both your code and the node_modules:
volumes:
- .:/usr/src/app/
请记住,您需要在容器外至少运行一次 npm install
(或复制 docker build
生成的 node_modules 目录).如果您需要更多详细信息,请现在告诉我.
Remember that you will need run npm install
at least one time outside the container (or copy the node_modules directory that the docker build
generates). Let me now if you need more details.
编辑.因此,无需在 OSX 中使用 npm,您可以:
Edit. So, without the need of npm in OSX, you can:
docker build
然后docker cp
.然后在您的 docker-compose.yml 中挂载这些文件并排除任何您想要的故障.:/path/to/node-modules ./local-node-modules/ - 或者,
docker build
和那里 (Dockerfile) 在另一个目录中执行npm install
.然后在您的命令(CMD 或 docker-compose 命令)中复制(cp
)到正确的目录,但该目录从您的计算机上挂载为空(docker-compose.yml 中的一个卷)然后解决您想要的任何问题.
docker build
and thendocker cp <container-id>:/path/to/node-modules ./local-node-modules/
. Then in your docker-compose.yml mount those files and troubleshot whatever you want.- Or,
docker build
and there (Dockerfile) do thenpm install
in another directory. Then in your command (CMD or docker-compose command) do the copy (cp
) to the right directory, but this directory is mounted empty from your computer (a volume in the docker-compose.yml) and then troubleshot whatever you want.
编辑 2.(选项 2)工作示例,克隆并尝试: https://github.com/xbx/base-server我在这个从你的分叉的仓库中自动完成了这一切.
Edit 2. (Option 2) Working example, clone and try: https://github.com/xbx/base-server I did it all automatically in this repo forked from the yours.
Dockerfile
FROM node:6.3
# Install app dependencies
RUN mkdir /build-dir
WORKDIR /build-dir
COPY package.json /build-dir
RUN npm install -g babel babel-runtime babel-register mocha nodemon
RUN npm install
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
RUN ln -s /build-dir/node_modules node_modules
# Bundle app source
COPY . /usr/src/app
EXPOSE 1234
CMD [ "npm", "start" ]
docker-compose.yml
docker-compose.yml
web:
build: .
ports:
- "1234:1234"
links:
- db # liaison avec la DB
environment:
PORT: 1234
command: /command.sh
volumes:
- ./src/:/usr/src/app/src/
- ./node_modules:/usr/src/app/node_modules
- ./command.sh:/command.sh
db:
image: mongo:3.3
ports:
- "27017:27017"
command: "--smallfiles --logpath=/dev/null"
命令.sh
#!/bin/bash
cp -r /build-dir/node_modules/ /usr/src/app/
exec npm start
请克隆我的 repo 并执行 docker-compose up
.它做你想做的.PS:可以改进以更好的方式(即最佳实践等)做同样的事情
Please, clone my repo and do docker-compose up
. It does what you want.
PS: It can be improved to do the same in a better way (ie best practices, etc)
我使用的是 OSX,它对我有用.
I'm in OSX and it works for me.
这篇关于node_modules 上的 docker-compose 卷但为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!