gpt4 book ai didi

Docker 撰写运行 yarn 安装

转载 作者:行者123 更新时间:2023-12-04 23:37:38 30 4
gpt4 key购买 nike

运行步骤时 RUN yarn 安装 在 docker-compose build 命令期间的 Dockerfile 中,我得到:

[1/4] Resolving packages... [2/4] Fetching packages... info fsevents@1.1.3: The platform "linux" is incompatible with this module. info "fsevents@1.1.3" is an optional dependency and failed compatibility check. Excluding it from installation. [3/4] Linking dependencies... warning "@rails/webpacker > postcss-cssnext@3.1.0" has unmet peer dependency "caniuse-lite@^1.0.30000697". warning " > webpack-dev-server@2.11.1" has unmet peer dependency "webpack@^2.2.0 || ^3.0.0". warning "webpack-dev-server > webpack-dev-middleware@1.12.2" has unmet peer dependency "webpack@^1.0.0 || ^2.0.0 || ^3.0.0". [4/4] Building fresh packages...



但未创建 node_modules 文件夹。另一方面,当我运行时
docker-compose run SERVICE_NAME yarn install

我得到:

[1/4] Resolving packages... [2/4] Fetching packages... info fsevents@1.1.3: The platform "linux" is incompatible with this module. info "fsevents@1.1.3" is an optional dependency and failed compatibility check. Excluding it from installation. [3/4] Linking dependencies... warning "@rails/webpacker > postcss-cssnext@3.1.0" has unmet peer dependency "caniuse-lite@^1.0.30000697". warning " > webpack-dev-server@2.11.1" has unmet peer dependency "webpack@^2.2.0 || ^3.0.0". warning "webpack-dev-server > webpack-dev-middleware@1.12.2" has unmet peer dependency "webpack@^1.0.0 || ^2.0.0 || ^3.0.0". [4/4] Building fresh packages...



然后是文件夹 node_modules 它是在项目文件夹中创建的。

我只是不明白为什么......我期待相同的功能,但我错过了一些东西。

这是我的 docker-compose 服务
services:
ruby:
build:
context: .
dockerfile: docker/ruby/Dockerfile
networks:
- some-network
volumes:
- .:/app
ports:
- "3000:3000"
depends_on:
- mysql
command: bundle exec rails s -p 3000 -b '0.0.0.0'

文件
FROM ruby:2.5

# Install dependencies:
# - build-essential: To ensure certain gems can be compiled
# - nodejs: Compile assets
# - npm: Install node modules
# - yarn: Install & manage node modules [should make npm obsolete]
# - libpq-dev
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - && \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update && \
apt-get install -qq -y build-essential nodejs yarn \
libpq-dev \
mysql-client

RUN mkdir /app
COPY . /app

WORKDIR /app

# install node dependencies
RUN yarn install

RUN bundle install

最佳答案

首先根据 Dockerfile 创建一个镜像:

  • 获取图片 ruby:2.5从中创建新图像
  • 安装所有依赖项
  • 创建文件夹 /app
  • 将系统(项目目录)中的所有文件复制到图像的 /app路径
  • 安装 yarnbundle (调用图像内的方法)

  • 然后将您的项目文件夹“挂载”到图像的 /app文件夹,因此它与您的系统根文件夹相同。之后“生成”容器并执行 CMD来自 Docker 文件的命令(如果存在)。

    记下 这不是图像/容器生成流程的详细解释 ,但希望它可以帮助您解决您的问题。这样你就可以
  • 直接从容器运行你的代码(就像你在 Dockerfile 中所做的那样)
  • 或者不要将源代码复制到容器并将代码作为卷挂载(就像您在 docker-compose.yml 中所做的那样),然后执行 yarn install在您的系统中,或使用安装命令创建入口点脚本:entrypoint: ./entrypoint.sh

  • 文件示例

    如果要“动态”更新源文件(无需重建容器即可查看更新),则必须使用第二种方式。

    在您的项目目录中创建文件 run.sh:

    run.sh
    #!/bin/sh

    echo '--- run yarn install'
    yarn install

    echo '--- run bundle install'
    bundle install

    # HERE YOU CAN RUN ANY OTHER SCRIPT BEFORE CONTAINER BUILDING

    echo '--- create docker image and up it'
    sudo docker-compose up -d --build

    docker-compose.yml

    无需更改(保持在您的问题中)。

    Dockerfile
    FROM ruby:2.5

    # Install dependencies:
    # - build-essential: To ensure certain gems can be compiled
    # - nodejs: Compile assets
    # - npm: Install node modules
    # - yarn: Install & manage node modules [should make npm obsolete]
    # - libpq-dev
    RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - && \
    curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
    apt-get update && \
    apt-get install -qq -y build-essential nodejs yarn \
    libpq-dev \
    mysql-client

    WORKDIR /app

    建立和建立容器 只需运行命令 : sh run.sh
    它将在您的系统中安装节点依赖项,之后将创建容器,其中 /app文件夹将是您的项目文件夹(在您的系统中),因此您的代码更改将立即生效。

    关于Docker 撰写运行 yarn 安装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48806626/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com