There is a VSCode extension by Microsoft called “Remote - Containers” which allows development inside of Docker containers, with configuration managed as code (within a devcontainer.json
file).
This has some nice features, namely:
Dockerfile
in the repo, developers don’t need to manually manage it on their host machine.node_modules
is stored within a Docker volume, not on the host operating system. This can improve the speed of installs/builds particularly if the host operating system is Windows (cause of AV scanning, slow file system, etc.)// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.241.1/containers/docker-existing-dockerfile
{
"name": "My Next.js App",
"context": "..",
"customizations": {
"vscode": {
"settings": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"extensions": ["mutantdino.resourcemonitor", "esbenp.prettier-vscode"]
}
},
"dockerFile": "../Dockerfile",
"mounts": [
"source=my-next-app-node_modules,target=${containerWorkspaceFolder}/node_modules,type=volume"
],
// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [3000],
"postCreateCommand": "pnpm install"
}
.devcontainer/devcontainer.json
FROM node:latest
RUN npm i -g pnpm
Dockerfile
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
webpack: (config) => {
// Workaround for Next not detecting changes within a container
config.watchOptions = {
aggregateTimeout: 200,
poll: 1000,
};
return config;
},
};
module.exports = nextConfig;
next.config.js
devcontainer.json
referencenode_modules
folder to stored inside a docker volume