簡體   English   中英

在另一個Dockerized容器中運行docker-compose

[英]Running docker-compose in another Dockerized Container

我正在設置開發環境,這次是使用docker容器運行所有內容,例如tmux,vim等。

當我運行運行編輯器的映像時,我使用-v /var/run/docker.sock:/var/run/docker.sock ,這樣,當我在編輯器容器的外殼中使用docker命令時,它會鏈接到docker在主機上,我可以運行其他Docker容器而不會出現問題。 這樣,我可以在編輯器容器中進行編碼,並以dev envs的形式啟動其他容器。

但是,如果我嘗試使用工作的編輯容器運行相同Dockerfile docker builddocker run使用碼頭工人,compose.yml和docker-compose up我得到以下的輸出:

OUTPUT

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

Docker Compose volumes:指令始終引用主機上的路徑。 (與docker run -v相同。)如果要從容器中以某種形式啟動Docker,則無法將一個容器的本地文件系統注入另一個容器中。

對於您正在描述的應用程序,我建議兩條路徑:

  1. 如果您的目標是通過調試器和實時重載在開發模式下運行應用程序,請完全放棄Docker,而僅在本地使用npm / yarn。 (我敢打賭,無論如何,主機上都已經安裝了vim和tmux來進行基本管理,這就是您使用的工具,並且安裝Node絕對比安裝Docker容易。)

  2. 如果您的目標是在Docker中運行應用程序以進行生產或生產前測試,請從Dockerfile中刪除volume volumes:指令。 在從步驟1構建並測試了您的應用程序之后, docker build映像並運行映像本身,而無需嘗試替換其代碼。

還要記住,任何可以運行Docker命令的人或任何人都可以對主機進行不受限制的root訪問。 我沒有看到按照您所描述的方式在容器中運行Docker Compose的明顯好處。

我知道發生了什么。 啟動編輯器容器時,我會掛載一個存儲所有項目的卷(即-v path / to / host / projects:/ path / to / container / projects)。

當docker-compose編譯.yml文件時(特別是將諸如。的縮寫路徑轉換為完整路徑)時,它會在編輯器容器上這樣做,但是當它向docker發送命令時,由於以下原因,它會將其發送給主機上的docker: -v /var/run/docker.sock:/var/run/docker.sock 因此docker嘗試使用容器在主機上的工作路徑來裝載卷,而docker顯然找不到。 我當前的解決方法是將卷安裝到相同的路徑名(即-v / path / to / projects:/ path / to / projects)。 它不干凈,但是可以用:)

歡迎任何想法,以尋求更好的解決方案!

我也在使用docker-compose時得到了這個。 我通過將docker-compose.yml內卷下的。:/ app更改為。:/ src / app來解決它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM