统一开发环境

准备

配置

.devcontainer 目录下需配置:devcontainer.jsonDockerfiledocker-compose.yml三个文件

.
└── .devcontainer
   ├── devcontainer.json
   ├── docker-compose.yml
   └── Dockerfile

devcontainer.json

详细配置:https://containers.dev/implementors/json_reference/

{
  "name": "Node.js",
  "build": {
      "dockerfile": "Dockerfile",
      // Update 'VARIANT' to pick a Node version: 18, 16, 14.
      // Append -bullseye or -buster to pin to an OS version.
      // Use -bullseye variants on local arm64/Apple Silicon.
      "args": { "VARIANT": "16-bullseye" }
  },

  // Configure tool-specific properties.
  "customizations": {
      // Configure properties specific to VS Code.
      "vscode": {
          // Add the IDs of extensions you want installed when the container is created.
          "extensions": [
            // "aaron-bond.better-comments",
            // "algorizen.javascript-snippets",
            // "antfu.icons-carbon",
            // "christian-kohler.path-intellisense",
            // "dbaeumer.vscode-eslint",
            // "eamodio.gitlens",
            // "EditorConfig.EditorConfig",
            // "Equinusocio.vsc-material-theme",
            // "equinusocio.vsc-material-theme-icons",
            // "esbenp.prettier-vscode",
            // "GitHub.copilot",
            // "mikestead.dotenv",
            // "ms-vscode.live-server",
            // "ms-vsliveshare.vsliveshare", // this is the extension id for live share
            // "naumovs.color-highlight",
            // "streetsidesoftware.code-spell-checker",
            // "usernamehw.errorlens",
            // "VisualStudioExptTeam.vscodeintellicode",
            // "Vue.volar",
            // "WakaTime.vscode-wakatime",
            // "withfig.fig"
          ]
      }
  }

  // Use 'forwardPorts' to make a list of ports inside the container available locally.
  // "forwardPorts": [],

  // Use 'postCreateCommand' to run commands after the container is created.
  // "postCreateCommand": "yarn install",

  // Comment out to connect as root instead. More info: <https://aka.ms/vscode-remote/containers/non-root>.
  // "remoteUser": "node"
}

Dockerfile

详细配置:https://docs.docker.com/engine/reference/builder/

FROM node:16-bullseye

docker-compose.yml

version: '3.8'

services:
  app:
    build:
      context: ..
      dockerfile: .devcontainer/Dockerfile
      args:
        VARIANT: "3"
        NODE_VERSION: "none"

    volumes:
      - ..:/workspace:cached

    command: sleep infinity

    network_mode: service:db

  db:
    image: postgres:latest
    restart: unless-stopped
    volumes:
      - postgres-data:/var/lib/postgresql/data
    hostname: postgres
    environment:
      POSTGRES_DB: my_media
      POSTGRES_USER: example
      POSTGRES_PASSWORD: pass
      POSTGRES_HOST_AUTH_METHOD: trust
    ports:
      - 5432:5432

volumes:
  postgres-data: null

Reference