前提
docker-compose を使ってvue.jsの環境構築を行っている。処理高速化のためnode_modulesはバインドマウントではなくvolumeマウントをおこない、vue createコマンドによってvueプロジェクトは作成済とします。
実現したいこと
docker-compose up --build コマンドでコンテナを起動した際に
DockerfileのRUNに記載したnpm install ●● を反映させたい
発生している問題
DockerfileのRUNの箇所にnpm install ●●を書いても反映されない。
(今回はaxiosをインストールしたかったが反映されなかった)
該当のソースコード
dockercompose.yml
1# docker-composeのバージョン 2version: '3' 3 4services: 5 6 frontend: 7 build: 8 context: . 9 dockerfile: Dockerfile 10 command: npm run serve 11 container_name: frontend 12 volumes: 13 - ./src/frontend:/code 14 - node_modules_volume:/code/node_modules 15 ports: 16 - "8080:8080" 17 expose: 18 - "8080" 19 tty: true 20 stdin_open: true 21 22volumes: 23 node_modules_volume:
Dockerfile
1FROM node:18.1-alpine 2 3WORKDIR /code 4 5RUN apk update && \ 6 npm install -g npm && \ 7 npm install -g @vue/cli 8 9RUN npm install axios 10 11ADD . /code
試したこと
下記コードのようにdocker-compose.ymlのcommand欄にnpm install ●●を書けば反映される。(具体的にはdocker-compose exec frontend sh でコンテナの中に入り、nodemodulesの中にaxiosフォルダが作成されていることを確認した)
インストール関連のコマンドはDockerfileのRUNに書けば良いと思っていたのだが、使い分けがよく分からなくなってしまった。RUN命令はDockerfileからImageを作成するときに実行されると書いてあったが、それならdocker-compose up --build コマンドを実行した際に反映されそうな気がしてしまいます。
dockercompose.yml
1# docker-composeのバージョン 2version: '3' 3 4services: 5 6 frontend: 7 build: 8 context: . 9 dockerfile: Dockerfile 10 command: > 11 sh -c "npm install axios && 12 npm run serve" 13 container_name: frontend 14 volumes: 15 - ./src/frontend:/code 16 - node_modules_volume:/code/node_modules 17 ports: 18 - "8080:8080" 19 expose: 20 - "8080" 21 tty: true 22 stdin_open: true 23 24volumes: 25 node_modules_volume:
Dockerfile
1FROM node:18.1-alpine 2 3WORKDIR /code 4 5RUN apk update && \ 6 npm install -g npm && \ 7 npm install -g @vue/cli 8 9ADD . /code
あなたの回答
tips
プレビュー