首页 > 解决方案 > 在 rails 6 和 docker 中找不到命令“webpack-dev-server”

问题描述

我有一个 Rails 6 应用程序。我只是做了 rails new 来生成它,没有别的。

这是我的码头文件:

ARG RUBY_VERSION
# See explanation below
FROM ruby:$RUBY_VERSION

ARG PG_MAJOR
ARG NODE_MAJOR
ARG BUNDLER_VERSION
ARG YARN_VERSION

# Add PostgreSQL to sources list
RUN curl -sSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
  && echo 'deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main' $PG_MAJOR > /etc/apt/sources.list.d/pgdg.list

# Add NodeJS to sources list
RUN curl -sL https://deb.nodesource.com/setup_$NODE_MAJOR.x | bash -

# Add Yarn to the sources list
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
  && echo 'deb http://dl.yarnpkg.com/debian/ stable main' > /etc/apt/sources.list.d/yarn.list

# Install dependencies
# We use an external Aptfile for that, stay tuned
COPY ./dockerDev/Aptfile /tmp/Aptfile
RUN apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
  DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
    build-essential \
    postgresql-client-$PG_MAJOR \
    nodejs \
    yarn=$YARN_VERSION-1 \
    $(cat /tmp/Aptfile | xargs) && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
    truncate -s 0 /var/log/*log

# Configure bundler and PATH
ENV LANG=C.UTF-8 \
  GEM_HOME=/bundle \
  BUNDLE_JOBS=4 \
  BUNDLE_RETRY=3
ENV BUNDLE_PATH $GEM_HOME
ENV BUNDLE_APP_CONFIG=$BUNDLE_PATH \
  BUNDLE_BIN=$BUNDLE_PATH/bin
ENV PATH /app/bin:$BUNDLE_BIN:$PATH

# Upgrade RubyGems and install required Bundler version
RUN gem update --system && \
    gem install bundler:$BUNDLER_VERSION

# Create a directory for the app code
RUN mkdir -p /app

WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN bundle install
COPY . /app
# RUN rails webpacker:install

# Add a script to be executed every time the container starts. Fixes a Rails-specific issue that prevents the server from restarting when a certain server.pid file pre-exists
# COPY entrypoint.sh /usr/bin/
# RUN chmod +x /usr/bin/entrypoint.sh
# ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

这是我的 docker-compose 文件:

version: '3.5'

services:
  app: &app
    build:
      context: .
      dockerfile: ./Dockerfile
      args:
        RUBY_VERSION: '2.6.3'
        PG_MAJOR: '10'
        NODE_MAJOR: '11'
        YARN_VERSION: '1.13.0'
        BUNDLER_VERSION: '2.0.2'
    image: my-dev:1.0.0
    tmpfs:
      - /tmp

  backend: &backend
    <<: *app
    stdin_open: true
    tty: true
    volumes:
      - .:/app
      # !!!! WARNING !!! For MacOs add this line. It does have a cost though
      # https://docs.docker.com/docker-for-mac/osxfs-caching/#cached
      # - .:/app:cached
      - rails_cache:/app/tmp/cache
      - bundle:/bundle
      - node_modules:/app/node_modules
      - packs:/app/public/packs
      - ./dockerDev/.psqlrc:/root/.psqlrc:ro
    environment:
      - NODE_ENV=development
      - RAILS_ENV=${RAILS_ENV:-development}
      # - REDIS_URL=redis://redis:6379/
      - DATABASE_URL=postgres://postgres@postgres
      - BOOTSNAP_CACHE_DIR=/bundle/bootsnap
      - WEBPACKER_DEV_SERVER_HOST=webpacker
      - WEB_CONCURRENCY=1
      - HISTFILE=/app/log/.bash_history
      - PSQL_HISTFILE=/app/log/.psql_history
      - EDITOR=vi
    depends_on:
      - postgres
      # - redis

  # runner:
  #   <<: *backend
  #   command: /bin/bash
  #   ports:
  #     - '3000:3000'
  #     - '3002:3002'

  rails:
    <<: *backend
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3054 -b '0.0.0.0'"
    ports:
      - '3054:3054'

  # sidekiq:
  #   <<: *backend
  #   command: bundle exec sidekiq -C config/sidekiq.yml

  postgres:
    image: postgres:10.10
    volumes:
      - .psqlrc:/root/.psqlrc:ro
      - postgres-data:/var/lib/postgresql/data
      - ./log:/root/log:cached
    environment:
      - PSQL_HISTFILE=/root/log/.psql_history
    ports:
      - "5432:5432"

  # redis:
  #   image: redis:3.2-alpine
  #   volumes:
  #     - redis:/data
  #   ports:
  #     - 6379

  webpacker:
    <<: *app
    command: ["/bin/bash","-c", "./bin/webpack-dev-server"]
    ports:
      - '3035:3035'
    volumes:
      - .:/app:cached
      - bundle:/bundle
      - node_modules:/app/node_modules
      - packs:/app/public/packs
    environment:
      - NODE_ENV=${NODE_ENV:-development}
      - RAILS_ENV=${RAILS_ENV:-development}
      - WEBPACKER_DEV_SERVER_HOST=0.0.0.0

volumes:
  postgres-data:
  # redis:
  bundle:
  node_modules:
  rails_cache:
  packs:

在另一台计算机上,我运行了 rails webpacker:install,所以我在bin文件夹中有以下文件:

#!/usr/bin/env ruby

ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development"
ENV["NODE_ENV"]  ||= "development"

require "pathname"
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
  Pathname.new(__FILE__).realpath)

require "rubygems"
require "bundler/setup"

require "webpacker"
require "webpacker/dev_server_runner"

APP_ROOT = File.expand_path("..", __dir__)
Dir.chdir(APP_ROOT) do
  Webpacker::DevServerRunner.run(ARGV)
end

此设置适用于执行 webpacker:install 的笔记本电脑,但在任何新笔记本电脑上都会生成以下错误:

webpacker_1  | yarn run v1.13.0
webpacker_1  | error Command "webpack-dev-server" not found.
webpacker_1  | info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

知道为什么吗?由于文件显然在那个文件夹中?

root@11b6f5f161d1:/app# ruby ./bin/webpack-dev-server
Traceback (most recent call last):
    7: from ./bin/webpack-dev-server:17:in `<main>'
    6: from ./bin/webpack-dev-server:17:in `chdir'
    5: from ./bin/webpack-dev-server:18:in `block in <main>'
    4: from /bundle/gems/webpacker-4.0.7/lib/webpacker/runner.rb:6:in `run'
    3: from /bundle/gems/webpacker-4.0.7/lib/webpacker/dev_server_runner.rb:11:in `run'
    2: from /bundle/gems/webpacker-4.0.7/lib/webpacker/dev_server_runner.rb:38:in `detect_port!'
    1: from /bundle/gems/webpacker-4.0.7/lib/webpacker/dev_server_runner.rb:38:in `new'
/bundle/gems/webpacker-4.0.7/lib/webpacker/dev_server_runner.rb:38:in `initialize': getaddrinfo: Name or service not known (SocketError)

大卫更新:

root@b6f1fb4ad240:/app/bin# ls
rails  rake  setup  webpack  webpack-dev-server  yarn

标签: ruby-on-railsdockerwebpack

解决方案


如果您使用纱线并重新开始,则验证node_modules中已安装的文件没有通过运行被删除yarn install --check-filesbin/webpack-dev-server


推荐阅读