简体   繁体   English

使用绑定安装和 Yarn 3 在 Docker 中使用 SWC minify 开发 Next.js:缺少 swc-linux-x64-gnu linux 二进制文件

[英]Developing Next.js with SWC minify in Docker with bind mount and Yarn 3: missing swc-linux-x64-gnu linux binary

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.我正在使用运行 Ubuntu 18 的 Docker 在我的 Mac OS 主机上使用 Next.js 进行开发,并将我的开发目录绑定安装到容器。 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.我想知道在 Next.js 12+ 中使用 SWC Minify 时是否有任何方法不必将整个主机构建上下文复制到 Docker,然后使用yarn install构建步骤和持久卷在 Docker 中运行开发环境,因为缺少的@next/swc-linux-x64-gnu二进制文件。

Relevant part of docker-compose.yaml 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)我知道因为它将在 Linux 而不是 Mac 上运行,所以 Next.js SWC Minify 将需要一个不同的二进制文件,所以我明确地将它添加到我的带有 Yarn 3 的主机上的依赖项中(设置为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.但这不会将 package 安装到我的主机node_modules文件夹中,即使它被压缩到.yarn/cache文件夹中并出现在我的 package.json 依赖项中。 I'm not sure why, and I think it's the reason for this problem.我不确定为什么,我认为这是这个问题的原因。

Dockerfile: 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:我已经找到了一个解决方法...将- /app/node_modules添加到 docker-compose 卷以允许容器保留该文件夹和 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.它获取正确的二进制文件并将其保存绑定安装的node_modules文件夹上的自己的node_modules中。 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.我敢肯定,如果 Yarn 3 实际上会在我的主机上将@next/swc-linux-x64-gnu安装到node_modules ,那么这是可能的。

Any thoughts?有什么想法吗?

In <repoDir>/.yarnrc , add:<repoDir>/.yarnrc中,添加:

--ignore-platform true

(Valid for yarn 1. You might want to double-check for later versions.) (适用于纱线 1。您可能需要仔细检查更高版本。)

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.这将在您的node_modules中安装 Linux 版本的 SWC,无论平台如何,您都可以在 docker 容器中使用它。

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更新时不会自动更新下一步,您必须手动进行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 windows 上安装 @swc/core-linux-musl 以使其在 docker 容器中工作? - How to install @swc/core-linux-musl on windows, to make it work in docker container? 安装Python 3 Docker Ubuntu错误命令&#39;x86_64-linux-gnu-gcc - Installing Python 3 Docker Ubuntu error command 'x86_64-linux-gnu-gcc 在Windows 10 linux映像中的Docker绑定挂载问题 - Docker bind mount issue in windows 10 linux image "linux docker挂载命名空间" - linux docker mount namespace 无法绑定在 Linux SQL 服务器中安装 Windows 文件夹 Z05B6053C41A2130AFDZimageBDAE1 - Unable to bind mount a Windows folder in a Linux SQL Server docker image docker无法挂载linux目标/ - docker could not mount the linux destination / Docker 构建不适用于 node:alpine 和 SWC 上的 NextJS - Docker build not working with NextJS on node:alpine with SWC Jenkins 错误构建:docker:/lib/x86_64-linux-gnu/libc.so.6:未找到版本“GLIBC_2.32”(docker 需要) - Jenkins error buildind : docker: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.32' not found (required by docker) 在 docker 容器中挂载 linux 镜像 - Mount linux image in docker container 我可以在 WSL 2 中使用 Docker 的 Linux 文件系统来绑定挂载目录而不是安装 Linux 发行版吗? - Can I use Docker's Linux filesystem in WSL 2 to bind mount a directory instead of installing a Linux distro?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM