Docker包含子目录并实时重新加载 [英] Docker compose with subdirectory and live reload

查看:23
本文介绍了Docker包含子目录并实时重新加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 create-react-app 创建了一个应用,并设置了docker compose设置容器并启动应用程序.当应用程序位于根目录中时,该应用程序将启动并且实时重新加载有效.但是,当我将应用程序移至子目录时,可以启动该应用程序,但是实时重新加载无法正常工作.

I created an app using create-react-app and set up docker compose to set up the container and start the app. When the app is in the root directory, the app starts and the live reload works. But when I move the app to a subdirectory, I can get the app to start, but the live reload does not work.

这是工作设置:

Dockerfile

Dockerfile

FROM node:7.7.2

ADD . /code

WORKDIR /code

RUN npm install

EXPOSE 3000

CMD npm start

docker-compose.yml

docker-compose.yml

version: "2"

services:
  client:
    build: .
    ports:
      - "3000:3000"
    volumes:
      - .:/code

目录结构

app
- node_modules
- docker-compose
- Dockerfile
- package.json
- src
- public

这是我想要的结构:

app
- server
- client
  / node_modules
  / Dockerfile
  / package.json
  / src
  / public
- docker-compose.yml

我尝试了所有可以想到的变体,但是实时重新加载无法正常工作.

I've tried every variation that I can think of, but the live reload will not work.

我要做的第一件事是更改构建位置:

The first thing I had to do was change the build location:

version: "2"

services:
  client:
    build: ./client
    ports:
      - "3000:3000"
    volumes:
      - .:/code

然后尝试运行 docker-compose up 时出现错误:

Then I got an error when trying to run docker-compose up:

npm ERR! enoent ENOENT: no such file or directory, open '/code/package.json'

因此,我将卷更改为-.:/client/code 并重新构建并运行了该命令,应用程序启动了,但没有实时重新加载.

So I changed the volume to - .:/client/code and rebuilt and ran the command and the app started, but no live reload.

当应用程序位于子目录中时,是否可以执行此操作?

Anyway to do this when the app is in a subdirectory?

推荐答案

移动本地目录时,容器内 的路径没有区别.因此,您只需要更改本地引用.

There's no difference to the paths inside the container when you move your local directory. So you only need to change the local references.

卷装载应来自 ./client

version: "2"

services:
  client:
    build: ./client
    ports:
      - "3000:3000"
    volumes:
      - ./client:/code

这篇关于Docker包含子目录并实时重新加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆