VS Code Dev Container在使用Vuejs开发时通常会很慢吗

p5cysglq  于 2023-06-24  发布在  Vue.js
关注(0)|答案(1)|浏览(153)

我有一个用Vuejs框架构建的静态网站。这个网站本身是相当轻量级的,我把它移到了一个VS Code开发容器中,这样它就可以更方便地被一些同事共享/重用。
但是我注意到运行npm installnpm run serve需要更长的时间;我怀疑它与主机和容器之间同步的目录node_modules有关。
这是预期的吗?或者有优化性能的方法吗?

devcontainer.json

{
    "name": "Personal Website Dev Container",
    "dockerFile": "../Dockerfile",
    "forwardPorts": [8080],
    "settings": {
        "terminal.integrated.shell.linux": "/bin/bash"
    },
}

Dockerfile

FROM node:latest

WORKDIR /app

COPY . .

EXPOSE 8080
z31licg0

z31licg01#

是的,这是可以预料的;)你可以 checkout 下面的链接,以加快一些事情。
基本上,如果你没有使用docker-compose,这是你必须做的(从微软网站复制)

Follow these steps:

Use the workspaceMount property in devcontainer.json to tell VS Code where to bind your source code. Then use the mounts property (VS Code 1.41+) to mount the node_modules sub-folder into a named local volume instead.

"mounts": [
    "source=${localWorkspaceFolderBasename}-node_modules,target=${containerWorkspaceFolder}/node_modules,type=volume"
]
Note: You may use ${localWorkspaceFolderBasename}, ${devcontainerId}, or a hardcoded name in the source.

Since this repository runs VS Code as the non-root "node" user, we need to add a postCreateCommand to be sure the user can access the folder.

"remoteUser": "node",
"mounts": [
    "source=${localWorkspaceFolderBasename}-node_modules,target=${containerWorkspaceFolder}/node_modules,type=volume"
],
"postCreateCommand": "sudo chown node node_modules"
This second step is not required if you will be running in the container as root.

If you've already built the container and connected to it, run Dev Containers: Rebuild Container from the Command Palette (F1) to pick up the change. Otherwise run Dev Containers: Open Folder in Container... to connect to the container.

Two notes on this approach:

If you delete the node_modules folder in the container, it may lose the connection to the volume. Delete the contents of the node_modules folder instead when needed (rm -rf node_modules/* node_modules/.*).

You'll find that an empty node_modules folder gets created locally with this method. This is because the volume mount point in the container is inside the local filesystem bind mount. This is expected and harmless.

相关问题