docker-compose 在启动使用 create-react-app 创建的 react 应用程序后立即停止

MAc*_*bot 7 docker reactjs docker-compose create-react-app

我正在尝试使用此处create-react-app描述的工具创建 React 应用程序。

我想用来docker-compose在 docker 容器内运行 react 应用程序。我采取了以下步骤:

在我的机器上,我创建了一个目录调用app并运行了一个 nodejs docker 容器:

mkdir app
docker run -it --rm -v "$(pwd)/app:/app" -w /app -p 3000:3000 node:13.10.1 bash
Run Code Online (Sandbox Code Playgroud)

在容器内,我初始化我的反应应用程序并启动我的应用程序:

npx create-react-app .
yarn start
Run Code Online (Sandbox Code Playgroud)

当我在浏览器中打开http://localhost:3000/#/ 时,我可以看到默认的反应页面。

接下来,我停止yarn start并退出容器。

在我的机器上,我可以通过运行以下命令来启动 react 应用程序:

docker run -it --rm -v "$(pwd)/app:/app" -w /app -p 3000:3000 node:13.10.1 yarn start
Run Code Online (Sandbox Code Playgroud)

当我在浏览器中打开http://localhost:3000/#/ 时,我再次可以看到默认的反应页面。

接下来,我通过停止 docker 容器来停止 React 应用程序。

最后,我创建了以下docker-compose.yml文件:

version: '3.7'
services:
    test-create-react-app:
        image: node:13.10.1
        volumes:
            - ./app:/app
        working_dir: /app
        ports:
            - 3000:3000
        command: ["yarn", "start"]
Run Code Online (Sandbox Code Playgroud)

当我使用docker-compose容器启动然后立即停止的 docker 容器时:

?  test-create-react-app docker-compose up
Creating network "test-create-react-app_default" with the default driver
Creating test-create-react-app_test-create-react-app_1 ... done
Attaching to test-create-react-app_test-create-react-app_1
test-create-react-app_1  | yarn run v1.22.0
test-create-react-app_1  | $ react-scripts start
test-create-react-app_1  | ? ?wds?: Project is running at http://172.21.0.2/
test-create-react-app_1  | ? ?wds?: webpack output is served from 
test-create-react-app_1  | ? ?wds?: Content not from webpack is served from /app/public
test-create-react-app_1  | ? ?wds?: 404s will fallback to /
test-create-react-app_1  | Starting the development server...
test-create-react-app_1  | 
test-create-react-app_1  | Done in 1.31s.
test-create-react-app_test-create-react-app_1 exited with code 0
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么应用程序在使用 启动 Docker 容器时停止docker-compose吗?

下面的一些版本可能会帮助您找到问题:

码头工人版本:

?  test-create-react-app docker version
Client:
 Version:           19.03.6
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        369ce74a3c
 Built:             Fri Feb 28 23:45:43 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server:
 Engine:
  Version:          19.03.6
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       369ce74a3c
  Built:            Wed Feb 19 01:06:16 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.3.3-0ubuntu1~18.04.1
  GitCommit:        
 runc:
  Version:          spec: 1.0.1-dev
  GitCommit:        
 docker-init:
  Version:          0.18.0
  GitCommit:
Run Code Online (Sandbox Code Playgroud)

Docker 撰写版本:

?  test-create-react-app docker-compose version
docker-compose version 1.25.0, build 0a186604
docker-py version: 4.1.0
CPython version: 3.7.4
OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019
Run Code Online (Sandbox Code Playgroud)

反应应用程序包:

?  test-create-react-app cat app/package.json
Run Code Online (Sandbox Code Playgroud)
{
  "name": "app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-scripts": "3.4.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

MAc*_*bot 13

添加stdin_open: true到 docker-compose 文件为我解决了这个问题。这是在相应的github 问题中提出的


Ale*_*hin 9

根据docker run文档

在前台模式下...docker run可以在容器中启动进程并将控制台附加到进程的标准输入、输出和标准错误。它甚至可以伪装成 TTY(这是大多数命令行可执行文件所期望的)并传递信号。

对于 docker-compose.yaml,类似的docker run对应物是tty: true. 所以你应该添加tty: true到你的docker-compose.yaml,它会做的伎俩。