2
3

More than 3 years have passed since last update.

node_modulesをvolume マウントすることで、docker-composeでyarn add が遅い問題を解決する

Last updated at Posted at 2021-02-04

はじめに

Docker環境下でNuxt開発をしている際に、docker-composeでyarnを用いてパッケージをインストールすると、とてもノロノロになってしまっていたので、それを解決しました。

原因

node_modulesをバインドマウントしていたから。ホストとdockerコンテナ内で相互に連携しないといけないので、時間がかかっていた。

解決策

node_modulesだけバインドマウントから外して、volumeマウントにする。通常のバインドマウントの後にnode_modulesだけvolumeマウントにすることで、node_modulesをバインドマウントから外せる。

例↓

docker-compose.yml
# (略)
  front:
    build:
      context: ./front

    environment:
      NODE_ENV: "development"
    command: yarn run dev
    volumes:
      - ./front:/app # ホストとdocker内部でプロジェクトのソースコードを共有するためにバインドマウント
      - node_modules_volume:/app/node_modules # node_modulesはホストと共有しないでvolumeマウント
    ports:
      - 8080:8080

volumes:
  node_modules_volume:

最後に

node_modulesに限らず、railsのgemの管理にも応用できました(docker環境下でのbundle installの高速化)

参考

2
3
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
3