我正在学习docker,并且我在使用Nodejs的开发环境中遇到了一些问题。
我有以下简单的dockerfile,旨在从nodejs父图像开始我的单元测试:
FROM node:4-onbuild
VOLUME ["/usr/src/app"]
CMD [ "npm", "test" ]
我以这种方式运行我的容器:
docker run -v /C/Users/myUser/dockertest:/usr/src/app notest
但我总是遇到以下错误:
npm info it worked if it ends with ok
npm info using npm@2.15.9
npm info using node@v4.5.0
npm ERR! Linux 4.4.15-moby
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "test"
npm ERR! node v4.5.0
npm ERR! npm v2.15.9
npm ERR! path /usr/src/app/package.json
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall open
npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'
npm ERR! enoent This is most likely not a problem with npm itself
npm ERR! enoent and is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! Please include the following file with any support request:
npm ERR! /usr/src/app/npm-debug.log
我不明白为什么我无法在容器内的/ usr / src / app文件夹中找到package.json。
事实是,如果我使用以下docker文件:
FROM node:4-onbuild
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
CMD [ "npm", "test" ]
我的测试将在容器中运行,但仅在构建步骤之后(因此每次进行修改时都必须构建...)。我需要随时启动这些测试,快速启动docker命令(restart?exec?)
感谢您的帮助
答案 0 :(得分:0)
您希望复制与构建相关的所有内容,并将您的代码库链接到containerstart。
FROM node:4.4
# Enviroment variables
ENV HOMEDIR /data
RUN mkdir -p ${HOMEDIR}
WORKDIR ${HOMEDIR}
# install all dependencies
COPY package.json ./
RUN npm install
# add node content initially
COPY . .
CMD ["npm", "test"]
然后在开始时链接它:
version: '2'
services:
myApp:
build:
context: .
dockerfile: Dockerfile
image: "myApp"
volumes:
- .:/data
您现在应该可以在不重新启动/重建容器的情况下更改代码库。