ruby-on-rails - MacOs Errno::ENOENT上的Docker:没有这样的文件或目录-getcwd

标签 ruby-on-rails macos docker

我试图在docker上使用webpacker设置Rails 6,并且一旦docker up完成,我会收到一个奇怪的错误消息:

Errno::ENOENT: No such file or directory - getcwd

一旦我在容器中SSH,我得到这个:
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory

这是我的DockerFile:
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
COPY package.json /app/package.json
COPY yarn.lock /app/yarn.lock
COPY . /app

# 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 组成:
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: treasure-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: ["/bin/bash","-c", "script/start-rails"]
    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:
    <<: *backend
    command: ["script/start-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:

这是我的启动脚本
#!/bin/bash
echo "Preparing container. This may take a while..."
wait_service ${DATABASE_URL:-db}  5432
wait_service webpacker 3035
bundle check || bundle install
yarn install --frozen-lockfile
bundle exec rake db:create
bundle exec rake db:migrate
bundle exec rake db:seed
echo "Done."
bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3054 -b '0.0.0.0'"

这些是我的docker-compose和docker版本:
treasure rails-6-new-postgresql % docker -v
Docker version 19.03.4, build 9013bf5
treasure rails-6-new-postgresql % docker-compose -v
docker-compose version 1.24.1, build 4667896b
treasure rails-6-new-postgresql % 

我已经在Google上阅读了一下,这是一个Docker错误,但是我没有找到如何传递它的方法,因此我的Rails服务器将无法启动。有任何想法吗?

更新:
这将是有关github的问题:
https://github.com/docker/for-mac/issues/1509

一个人说他们这样做:
@ryfow might be on to something. In my case there was no subdirectory mount, but two different containers based on the same image were mounting a volume from the same host directory at the same path inside the container. We have two different services in the same repository with virtually identical dependencies, so docker-compose.yml just launches the same image twice with different commands and configurations. We now have a workaround in place which just retries in a loop until it works, so we haven't noticed the problem in a while, but I suspect it's still happening.

但是我需要翻译他的工作或一段代码。

David的Update2:

Dockerfile保持原样

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: treasure-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

  rails:
    <<: *backend
    command: ["/bin/bash","-c", "script/start-rails"]
    ports:
      - '3054:3054'

  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"

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

最佳答案

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

ENV APP_PATH=/app
WORKDIR $APP_PATH
ONBUILD COPY Gemfile* /app/
ONBUILD COPY package.json /app/
ONBUILD COPY yarn.lock /app/
ONBUILD COPY . /app

# 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

好的,似乎答案是使用ONBUILD命令,这似乎已经将其与APP_PATH变量一起修复了.....我已将容器重制了5次,没有更多错误。

我无法解释为什么,也许某个知识渊博的人可以,但这就是答案,至少对我来说

关于ruby-on-rails - MacOs Errno::ENOENT上的Docker:没有这样的文件或目录-getcwd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58518349/

相关文章:

ruby-on-rails - 自动重新启动我的heroku应用程序

javascript - rails : Trying to render a partial on button click

amazon-web-services - 使用 Elastic Beanstalk 将多 docker 本地设置部署到 AWS

docker - Mysql docker容器未挂载

ruby-on-rails - Ruby 1.9.2 + heroku gem + rails 3 + windows

css - 我将如何设置此 rails return 语句的内联样式?

macos - xcode项目中使用系统调用有什么优点和缺点

php - MongoDB 和 MAMP

python - macOS 上的 virtualenv 使用不存在的 python 解释器

node.js - 在Docker中使用负载测试应用