![](/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.