我正在设置开发环境,这次是使用docker容器来运行所有内容,例如tmux,vim等。
当我运行运行编辑器的映像时,我使用-v /var/run/docker.sock:/var/run/docker.sock
,这样,当我在编辑器容器的外壳中使用docker
命令时,它会链接到主机上的docker,我可以运行其他docker容器没有问题。这样,我可以在编辑器容器中进行编码,并以dev envs的形式启动其他容器。
但是,如果尝试使用docker-compose.yml和docker build
从使用docker run
和docker-compose up
的编辑器容器中运行相同的Dockerfile,则会得到以下输出:
输出
Recreating frontend_tests_1 ... done
Recreating frontend_web_1 ... done
Attaching to frontend_tests_1, frontend_web_1
tests_1 | npm ERR! path /app/package.json
tests_1 | npm ERR! code ENOENT
tests_1 | npm ERR! errno -2
tests_1 | npm ERR! syscall open
web_1 | npm ERR! path /app/package.json
web_1 | npm ERR! code ENOENT
web_1 | npm ERR! errno -2
web_1 | npm ERR! syscall open
tests_1 | npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
tests_1 | npm ERR! enoent This is related to npm not being able to find a file.
tests_1 | npm ERR! enoent
web_1 | npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
web_1 | npm ERR! enoent This is related to npm not being able to find a file.
web_1 | npm ERR! enoent
tests_1 |
tests_1 | npm ERR! A complete log of this run can be found in:
tests_1 | npm ERR! /root/.npm/_logs/2019-03-24T22_08_49_446Z-debug.log
web_1 |
web_1 | npm ERR! A complete log of this run can be found in:
web_1 | npm ERR! /root/.npm/_logs/2019-03-24T22_08_49_447Z-debug.log
frontend_web_1 exited with code 254
frontend_tests_1 exited with code 254
如果我使用相同的目录在主机上执行相同的步骤,则会得到以下正常输出:
期望
Recreating frontend_web_1 ... done
Recreating frontend_tests_1 ... done
Attaching to frontend_web_1, frontend_tests_1
web_1 |
web_1 | > frontend@0.1.0 start /app
web_1 | > react-scripts start
web_1 |
tests_1 |
tests_1 | > frontend@0.1.0 test /app
tests_1 | > react-scripts test --env=jsdom
tests_1 |
web_1 | Starting the development server...
web_1 |
tests_1 | PASS src/App.test.js
tests_1 | ✓ renders without crashing (22ms)
tests_1 |
tests_1 | Test Suites: 1 passed, 1 total
tests_1 | Tests: 1 passed, 1 total
tests_1 | Snapshots: 0 total
tests_1 | Time: 1.352s
tests_1 | Ran all test suites related to changed files.
tests_1 |
tests_1 | Watch Usage
tests_1 | › Press p to filter by a filename regex pattern.
tests_1 | › Press t to filter by a test name regex pattern.
tests_1 | › Press q to quit watch mode.
tests_1 | › Press Enter to trigger a test run.
web_1 | Compiled successfully!
web_1 |
web_1 | You can now view frontend in the browser.
web_1 |
web_1 | Local: http://localhost:3000/
web_1 | On Your Network: http://0.0.0.0:3000/
web_1 |
web_1 | Note that the development build is not optimized.
web_1 | To create a production build, use yarn build.
web_1 |
^CGracefully stopping... (press Ctrl+C again to force)
Stopping frontend_tests_1 ... done
Stopping frontend_web_1 ... done
Dockerfile.dev
FROM node:alpine
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY ./ ./
CMD ["npm", "run", "start"]
docker-compose.yml
version: '3'
services:
web:
build:
context: .
dockerfile: Dockerfile.dev
ports:
- "8080:8080"
volumes:
- /app/node_modules
- .:/app
tests:
build:
context: .
dockerfile: Dockerfile.dev
volumes:
- /app/node_modules
- .:/app
command: ["npm", "run", "test"]
答案 0 :(得分:0)
Docker Compose volumes:
指令始终引用主机上的路径。 (与docker run -v
相同。)如果从容器中以某种形式启动Docker,则无法将一个容器的本地文件系统注入另一个容器中。
对于您要描述的应用程序,我建议两条路径:
如果您的目标是通过调试器和实时重载在开发模式下运行应用程序,请完全放弃Docker,而仅在本地使用npm / yarn。 (我敢打赌,无论如何,主机上都已经安装了vim和tmux来进行基本管理,这是您使用的工具,并且安装Node绝对比安装Docker容易。)
如果您的目标是在Docker中运行应用程序以进行生产或生产前测试,请从Dockerfile中删除volumes:
指令。从第1步构建并测试了应用程序之后,docker build
映像并运行映像本身,而无需尝试替换其代码。
还请记住,任何可以运行Docker命令的人或任何人都可以对主机进行不受限制的root访问。我看不到按照您所描述的方式在容器中运行Docker Compose的明显好处。
答案 1 :(得分:0)
我弄清楚是怎么回事。启动编辑器容器时,我会挂载一个存储所有项目的卷(即-v path / to / host / projects:/ path / to / container / projects)。
当docker-compose编译.yml文件时(特别是当它将。等缩写路径转换为完整路径时),它在编辑器容器上执行此操作,但是当它向docker发送命令时,它将在docker上发送给docker由于-v /var/run/docker.sock:/var/run/docker.sock
而成为主机。因此docker尝试使用容器在主机上的工作路径来装载卷,而docker显然找不到。我目前的解决方法是将卷安装到相同的路径名(即-v / path / to / projects:/ path / to / projects)。它不干净,但是可以用:)
欢迎任何想法,以寻求更好的解决方案!
答案 2 :(得分:0)
我也在使用docker-compose时得到了这个。我通过将docker-compose.yml内卷下的。:/ app更改为。:/ src / app来解决了该问题