使用webpacker(webpack-dev-server)在docker容器内编译而不重建

时间:2018-03-21 15:16:28

标签: ruby-on-rails docker webpack docker-compose webpacker

编辑:Docker版本17.12.0-ce,构建c97c6d6在OSX High Sierra 10.12.6上

我正在将webpacker包含到现有的rails 4.2项目中(某些部分仍然使用sprockets)并且已经遇到热重新加载的一些问题。我的应用程序是dockerized,我希望有一个设置,我可以编辑我的React代码,并在我的rails docker容器中编译和刷新它,而不需要为每个小的更改重建(例如docker-compose build)。

目前我正在运行webpack-dev-server并正确编译代码。 webpack dev服务器(http://dockerhost:3035/webpack-dev-server/)将从我的本地计算机编译卷装入的代码,但是当我刷新rails应用程序时,新编译的资产将不会出现。我的设置有什么明显的错误吗?

# webpacker.yml
development:
  <<: *default

compile: true
  # Reference: https://webpack.js.org/configuration/dev-server/
  dev_server:
    https: false
    host: 0.0.0.0 #webpacker
    port: 3035
    public: 0.0.0.0:3035
    hmr: true
    # Inline should be set to true if using HMR
    inline: true
    overlay: true
    compress: true
    disable_host_check: true
    use_local_ip: false
    quiet: false
    headers:
      'Access-Control-Allow-Origin': '*'
    watch_options:
      ignored: /node_modules/
      poll: 1000

# docker-compose.yml
version: '3'
networks:
  pnet:
    driver: bridge

services:  
  webpacker:
    build: .
    command: bundle exec bin/webpack-dev-server #./bin/webpack-dev-server
    networks:
      - pnet
    volumes:                                                                                                               
      - .:/webpacker-app                                                                                                   
    working_dir: /webpacker-app
    ports:
      - '3035:3035'
      - '8080:8080'
    environment:
      - NODE_ENV=development
      - RAILS_ENV=development
      - WEBPACKER_DEV_SERVER_HOST=0.0.0.0
  web: 
    build: .
    command: bundle exec passenger start
    volumes:
      - ~/tmp/pids:/usr/src/app/tmp/pids
      - .:/webpacker-app
    networks:
      - pnet
    ports:
      - "3000:3000"
      - "3443:3443"
    depends_on:
      - webpacker
    environment:
      - NODE_ENV=development
      - RAILS_ENV=development
      - WEBPACKER_DEV_SERVER_HOST=0.0.0.0

1 个答案:

答案 0 :(得分:2)

我不太熟悉w / webpacker但是看着你的配置,我的猜测是你使用0.0.0.0:3035地址从web容器中访问dev-server资产应该是webpacker:3035

尝试更新WEBPACKER_DEV_SERVER_HOST服务配置中的web

web:
  ...
  environment:
    ...
    - WEBPACKER_DEV_SERVER_HOST=webpacker