![](/img/trans.png)
[英]How to install @swc/core-linux-musl on windows, to make it work in docker container?
[英]Developing Next.js with SWC minify in Docker with bind mount and Yarn 3: missing swc-linux-x64-gnu linux binary
我正在使用运行 Ubuntu 18 的 Docker 在我的 Mac OS 主机上使用 Next.js 进行开发,并将我的开发目录绑定安装到容器。 我想知道在 Next.js 12+ 中使用 SWC Minify 时是否有任何方法不必将整个主机构建上下文复制到 Docker,然后使用yarn install
构建步骤和持久卷在 Docker 中运行开发环境,因为缺少的@next/swc-linux-x64-gnu
二进制文件。
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 上运行,所以 Next.js SWC Minify 将需要一个不同的二进制文件,所以我明确地将它添加到我的带有 Yarn 3 的主机上的依赖项中(设置为nodeLinker: node-modules
in.yarnrc)
但这不会将 package 安装到我的主机node_modules
文件夹中,即使它被压缩到.yarn/cache
文件夹中并出现在我的 package.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-compose 卷以允许容器保留该文件夹和 Dockerfile:
COPY . /app/
RUN yarn install
它获取正确的二进制文件并将其保存在绑定安装的node_modules
文件夹上的自己的node_modules
中。 但我只是想看看是否有一种方法可以做到这一点,而无需从构建上下文中复制整个内容。 我敢肯定,如果 Yarn 3 实际上会在我的主机上将@next/swc-linux-x64-gnu
安装到node_modules
,那么这是可能的。
有什么想法吗?
在<repoDir>/.yarnrc
中,添加:
--ignore-platform true
(适用于纱线 1。您可能需要仔细检查更高版本。)
然后运行:
yarn add --dev @next/swc-linux-x64-gnu
这将在您的node_modules
中安装 Linux 版本的 SWC,无论平台如何,您都可以在 docker 容器中使用它。
权衡:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.