首页 > 解决方案 > 在另一个 Dockerized 容器中运行 docker-compose

问题描述

我正在设置我的开发环境,这一次我使用 docker 容器来运行所有东西......比如 tmux、vim 等。

当我运行运行我使用的编辑器的图像时,-v /var/run/docker.sock:/var/run/docker.sock当我在编辑器容器的 shell 中使用docker命令时,它会链接到主机上的 docker,我可以毫无问题地运行其他 docker 容器。这样我就可以在我的编辑器容器中编码,并将其他容器作为开发环境启动。

但是,如果我尝试从使用docker builddocker run使用 docker-compose.yml 的编辑器容器运行相同的 Dockerfile,docker-compose up我会得到以下输出:

输出

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"]

码头工人-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"]

标签: dockernpmdocker-composedockerfile

解决方案


Docker Composevolumes:指令总是引用主机上的路径。(同样适用docker run -v。)如果您从容器中以某种形式启动 Docker,则无法将一个容器的本地文件系统注入到另一个容器中。

对于您描述的应用程序,我建议两条路径:

  1. 如果您的目标是在开发模式下运行应用程序,使用调试器和实时重新加载,请完全放弃 Docker,只在本地使用 npm/yarn。(我敢打赌,你的主机上已经安装了 vim 和 tmux,用于基本管理你使用的工具,而且安装 Node 比安装 Docker 更容易。)

  2. 如果您的目标是在 Docker 中运行应用程序以进行生产或预生产测试,请volumes:从 Dockerfile 中删除指令。在从第 1 步构建并测试您的应用程序后,docker build生成一个映像并运行映像本身,而不尝试替换其代码。

还要记住,可以运行 Docker 命令的任何人或任何事物都对主机具有不受限制的 root 访问权限。我认为按照您描述的方式在容器中运行 Docker Compose 没有明显的好处。


推荐阅读