使用bind Mount和纱线在Docker中开发Next.js 3:缺少SWC-Linux-X64-GNU Linux Linux二进制

发布于 2025-01-25 19:28:07 字数 3034 浏览 3 评论 0原文

我使用运行Ubuntu 18的Docker在Mac OS主机上使用Next.js开发。将我的开发目录绑在容器中。我想知道在Next.js 12+中使用SWC时是否有任何方法由于缺少@next/swc-linux-x64-gnu二进制文件,因此在Docker中运行开发环境的卷。

docker-compose.yaml的相关部分,

  # Next.js Client and Server
  my-nextjs:
    container_name: my-nextjs
    build:
      context: ./www
      dockerfile: Dockerfile
    volumes:
      # bind mounting my development directory to /app in Docker
      - ./www:/app/
    ports:
      - "3911:3000"

我知道,因为它将在linux上而不是Mac上运行,因此。带有纱3的机器(设置为nodelinker:node-modules .yarnrc),

但这确实 not 将软件包安装到我的主机node_modules文件夹即使在.yarn/cache文件夹中z缩进行了缩进,并显示在我的poffage.json依赖项中。我不确定为什么,我认为这是造成这个问题的原因。

dockerfile:

FROM ubuntu:18.04

# Install Node.js
RUN apt-get update
RUN apt-get -y install curl
RUN apt-get -y install sudo
RUN useradd -m docker && echo "docker:docker" | chpasswd && adduser docker sudo
RUN curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
RUN apt-get install -y nodejs
RUN apt-get install -y build-essential

# Install Yarn
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
RUN sudo apt-get update
RUN sudo apt-get install yarn

WORKDIR /app

EXPOSE 3000
ENV PORT 3000

ENV NEXT_TELEMETRY_DISABLED 1

CMD ["yarn", "dev"]

当我尝试运行此容器时,我会得到:

 ❯  docker compose up my-nextjs                                                      
[+] Running 1/1
 ⠿ Container my-nextjs  Recreated                                                                                                                                                        0.1s
Attaching to my-nextjs
my-nextjs  | ready - started server on 0.0.0.0:3000, url: http://localhost:3000
my-nextjs  | info  - Loaded env from /app/.env
my-nextjs  | warn  - SWC minify release candidate enabled. https://nextjs.org/docs/messages/swc-minify-enabled
my-nextjs  | info  - Attempted to load @next/swc-linux-x64-gnu, but it was not installed
my-nextjs  | info  - Attempted to load @next/swc-linux-x64-gnux32, but it was not installed
my-nextjs  | info  - Attempted to load @next/swc-linux-x64-musl, but it was not installed
my-nextjs  | error - Failed to load SWC binary for linux/x64, see more info here: https://nextjs.org/docs/messages/failed-loading-swc
my-nextjs exited with code 1

我已经找到了解决方法...添加- /app/node_modules到docker-compospose卷中,允许容器可以持久持续该文件夹和文件夹和在Dockerfile中:

COPY . /app/
RUN yarn install

它具有正确的二进制文件,并在其自己的 node_modules 上持续 bind bint opted node_modules folder 。但是我只是想看看是否有一种方法可以在不从构建上下文中复制整个事物。我敢肯定,如果YARN 3实际上将安装@next/swc-linux-x64-gnu node_modules在我的主机上,那么这是可能的。

有什么想法吗?

I am developing with Next.js on my Mac OS host using Docker running Ubuntu 18 and bind mounting my development directory to the container. I want to know if there is any way when using SWC Minify in Next.js 12+ to not have to copy over the whole host build context to Docker and then use a yarn install build step and a persistent volume to run a development environment in Docker because of the missing @next/swc-linux-x64-gnu binary.

Relevant part of docker-compose.yaml

  # Next.js Client and Server
  my-nextjs:
    container_name: my-nextjs
    build:
      context: ./www
      dockerfile: Dockerfile
    volumes:
      # bind mounting my development directory to /app in Docker
      - ./www:/app/
    ports:
      - "3911:3000"

I know that because it will be running on Linux and not Mac, Next.js SWC Minify will need a different binary so I explicitly add that to my dependencies on my Host machine with Yarn 3 (set to nodeLinker: node-modules in .yarnrc)

But that does not install the package to my host node_modules folder even though it is zipped in the .yarn/cache folder and appears in my package.json dependencies. I'm not sure why, and I think it's the reason for this problem.

Dockerfile:

FROM ubuntu:18.04

# Install Node.js
RUN apt-get update
RUN apt-get -y install curl
RUN apt-get -y install sudo
RUN useradd -m docker && echo "docker:docker" | chpasswd && adduser docker sudo
RUN curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
RUN apt-get install -y nodejs
RUN apt-get install -y build-essential

# Install Yarn
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
RUN sudo apt-get update
RUN sudo apt-get install yarn

WORKDIR /app

EXPOSE 3000
ENV PORT 3000

ENV NEXT_TELEMETRY_DISABLED 1

CMD ["yarn", "dev"]

When I try run this container I get:

 ❯  docker compose up my-nextjs                                                      
[+] Running 1/1
 ⠿ Container my-nextjs  Recreated                                                                                                                                                        0.1s
Attaching to my-nextjs
my-nextjs  | ready - started server on 0.0.0.0:3000, url: http://localhost:3000
my-nextjs  | info  - Loaded env from /app/.env
my-nextjs  | warn  - SWC minify release candidate enabled. https://nextjs.org/docs/messages/swc-minify-enabled
my-nextjs  | info  - Attempted to load @next/swc-linux-x64-gnu, but it was not installed
my-nextjs  | info  - Attempted to load @next/swc-linux-x64-gnux32, but it was not installed
my-nextjs  | info  - Attempted to load @next/swc-linux-x64-musl, but it was not installed
my-nextjs  | error - Failed to load SWC binary for linux/x64, see more info here: https://nextjs.org/docs/messages/failed-loading-swc
my-nextjs exited with code 1

I have figured out a workaround for this... adding - /app/node_modules to the docker-compose volumes to allow the container to persist that folder and in the Dockerfile:

COPY . /app/
RUN yarn install

Which gets the correct binary and persists it in its own node_modules over the bind mounted node_modules folder. But I'm just trying to see if there's a way to do it without copying over the whole thing from the build context. I'm sure that if Yarn 3 would actually install @next/swc-linux-x64-gnu to node_modules on my host then this could be possible.

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謸气贵蔟 2025-02-01 19:28:07

< repodir>/。yarnrc中,add :(

--ignore-platform true

1有效。

yarn

yarn add --dev @next/swc-linux-x64-gnu

对 代码> node_modules ,无论平台如何,您都可以在Docker容器中使用它。

折衷:

  • 您将丢失平台检查以后安装的软件包,
  • 下次更新时不会自动更新,您必须手动进行操作

In <repoDir>/.yarnrc, add:

--ignore-platform true

(Valid for yarn 1. You might want to double-check for later versions.)

Then run:

yarn add --dev @next/swc-linux-x64-gnu

This will install the Linux version of SWC in your node_modules, regardless of the platform, and you'll be able to use that in docker containers.

Trade-offs:

  • you lose the platform check for packages you install in the future
  • it won't be updated automatically when you update Next, you'll have to do it manually
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文