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