2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

devcontainerのディスクのパフォーマンス改善

Last updated at Posted at 2022-08-13

devcontainerでnode_modulesのようなパッケージがインストールされるディレクトリは容量が大きくホストにマウントしているとDockerの動作が遅くなる。

そのため、node_modulesをマウントから外す必要がある。以下のように記載することでnode_modulestry-node_moduelesというVolumeに保存できる。

"mounts": [
    "source=try-node-node_modules,target=${containerWorkspaceFolder}/node_modules,type=volume"
],

匿名Volumeに保存したい場合の設定は以下のように記載する。

"mounts": [
    "target=${containerWorkspaceFolder}/node_modules,type=volume"
],

devcontainer.jsonの全体は以下のようになる。

devcontainer.json
// 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/javascript-node
{
	"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": [
				"dbaeumer.vscode-eslint"
			]
		}
	},
	"mounts": [
		"source=try-node-node_modules,target=${containerWorkspaceFolder}/node_modules,type=volume"
	],
	"postCreateCommand": "sudo chown node node_modules",
	// 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"
}

コンテナ内のnode_modulesにファイルがあるが、ローカルホスト上にはファイルがないことを確認する。
image.png

image.png

docker-composeの場合はこのようにする。

services:
  database:
    image: postgres
    container_name: database
    restart: unless-stopped
    environment:
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: laravel_docker
    volumes:
      - ./postgres-data:/var/lib/postgresql/data
    ports:
      - '5433:5432'
  php-apache:
    container_name: php-apache
    build:
      context: .
      dockerfile: ./php/Dockerfile
    ports:
        - '8080:80'
    volumes:
      - ./src:/var/www/laravel_docker
      - /var/www/laravel_docker/node_modules # Library should not Mount Local Volume for Performance
      - /var/www/laravel_docker/vendor # Library should not Mount Local Volume for Perfomance
      - ./apache/default.conf:/etc/apache2/sites-enabled/000-default.conf
    depends_on:
      - database

参考文献

https://code.visualstudio.com/remote/advancedcontainers/improve-performance
https://github.com/microsoft/vscode-remote-try-node/issues/11

2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?