【问题标题】:Docker on MacOs Errno::ENOENT: No such file or directory - getcwdMacOs上的Docker Errno::ENOENT:没有这样的文件或目录-getcwd
【发布时间】:2020-02-19 10:26:28
【问题描述】:

我正在尝试在 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-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

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

这是我的 start-rails 脚本

#!/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.

但我需要翻译一下他在做什么,或者一段代码。

大卫的更新 2:

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:

【问题讨论】:

  • 你能把这个减少到minimal reproducible example吗?例如,这六个容器中似乎只有一个或两个实际上与这个问题相关。将 Dockerfile 的 Node 和 Ruby 部分分离到单独的容器中会有所帮助,这也有助于减少卷的数量。
  • @DavidMaze 你是对的,对于这个问题,唯一需要启动的容器是 postgresql 和 rails one

标签: ruby-on-rails macos docker


【解决方案1】:
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 次,没有更多错误。

我无法解释为什么,也许有更多知识的人可以,但这是答案,至少对我而言

【讨论】:

    猜你喜欢
    • 2012-02-24
    • 2015-11-30
    • 2020-02-22
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    • 2013-02-01
    • 2018-05-01
    相关资源
    最近更新 更多