在dock_modules上的docker-compose卷,但为空 [英] docker-compose volume on node_modules but is empty
问题描述
这是我的docker-compose.yml
web:
build:。
ports:
- 3000:3000
links:
- db
环境:
PORT:3000
卷:
- 。:/ usr / src / app
- / usr / src / app / node_modules
db:
image:mongo:3.3
ports:
- 27017:27017
command:--smallfiles --logpath = / dev / null
我和Mac的Docker当我运行 docker-compos up -d
全部右键,但它在我的电脑上创建一个node_modules文件夹,但它是空的。我进入我的容器的bash和ls node_modules,所有的包都在那里。
我如何在计算机上的容器上获取内容? >
谢谢
TL; DR工作示例,克隆和尝试: https://github.com/xbx/base-server
首先需要在计算机中使用一个node_modules(外部图像)进行调试(运行容器之前) >
如果您只想调试node_modules:
卷:
- / path / to / node_modules:/ usr / src / app / node_modules
代码和node_modules:
卷:
- 。:/ usr / src / app /
请记住,您至少需要一次运行 npm install
容器(或副本 docker build
生成的npm_modules)。如果您需要更多详细信息,请现在就可以。
编辑。因此,在OSX中不需要npm,您可以:
-
docker build
然后docker cp< container-id>:/ path / to / node-modules ./local-node-modules /
。然后在您的docker-compose.yml中挂载这些文件,并且无需任何想要的东西。 - 或者,
docker build
和那里(Dockerfile)在另一个目录中执行npm install
。然后在您的命令(CMD或docker-compose命令)中,将副本(cp
)添加到正确的目录,但此目录从您的计算机(在docker-compose.yml),然后麻烦你想要的。
编辑2。 (选项2)工作示例,克隆并尝试: https:// github。 com / xbx / base-server
我从这个repo中自动完成了所有这些。
Dockerfile
FROM node:6.3
#安装应用依赖项
RUN mkdir / build-dir
WORKDIR / build-dir
COPY package.json / build-dir
运行npm install -g babel babel-runtime babel-register mocha nodemon
运行npm安装
#创建应用程序目录
RUN mkdir -p / usr / src / app
WORKDIR / usr / src / app
RUN ln -s / build-dir / node_modules node_modules
#捆绑应用源
COPY 。 / usr / src / app
EXPOSE 1234
CMD [npm,start]
docker-compose.yml
web:
build:。
ports:
- 1234:1234
links:
- db#liaison avec la DB
环境:
PORT:1234
命令:/command.sh
卷:
- ./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
command.sh
#!/ bin / bash
cp -r / build-dir / node_modules / / usr / src / app /
exec npm start
请克隆我的回购,并执行 docker-compose up
。它做你想要的
PS:可以通过更好的方式来改善(即最佳做法等)
我在OSX中,适用于我。
I'm pretty new with Docker and i wanted to map the node_modules folder on my computer (for debugging purpose).
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"
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?
Thank you
TL;DR Working example, clone and try: https://github.com/xbx/base-server
You need a node_modules in your computer (outside image) for debugging purposes first (before run the container).
If you want debug only node_modules:
volumes:
- /path/to/node_modules:/usr/src/app/node_modules
If you want debug both your code and the node_modules:
volumes:
- .:/usr/src/app/
Remember that you will need run npm install
at least one time outside the container (or copy the npm_modules that the docker build
generates). Let me now if you need more details.
Edit. So, without the need of npm in OSX, you can:
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.
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
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"
command.sh
#!/bin/bash
cp -r /build-dir/node_modules/ /usr/src/app/
exec npm start
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)
I'm in OSX and it works for me.
这篇关于在dock_modules上的docker-compose卷,但为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!