Dockerを使ったReactの環境構築でerror Couldn't find a package.json file in ”xxx”となってしまう
Q&A
Closed
実現したいこと
現在Docker-machineを使って、仮想環境のvirtualbox上に
React+Rails+MySqlの環境構築を行いたいと思ってます。
解決したいこと
こちらの記事を参考に環境構築を行なっているのですが、
Docker-compose up 時にyarnのエラーになってしまいます
$ docker-compose up
省略
yarn run v1.22.15
error Couldn't find a package.json file in "/myapp"
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
front側のコンテナだけうまくいかず、railsやMySQLのコンテナはきちんと動作しております。
frontのコンテナが動かないので、create-react-appもできない状態(行なっても、同じ様にpackege.jsonが見つからないと怒られる)
以下Dockerfileなどです
構成
myapp
|-api
|-front
docker-compose.yml
docker@try1:~/myapp$ ls api/
Dockerfile Rakefile config.ru log vendor
Gemfile app db public
Gemfile.lock bin entrypoint.sh storage
README.md config lib tmp
#rails.newをした後なので、Docker-file以外のディレクトリができています
docker@try1:~/myapp$ ls front/
Dockerfile
front側のDockerfile
FROM node:17-alpine
#ここは何度か別verを試したりしています。
RUN mkdir /myapp
WORKDIR /myapp
docker@try1:~/myapp$ cat docker-compose.yml
version: '3'
services:
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: password
ports:
- '3306:3306'
command: --default-authentication-plugin=mysql_native_password
volumes:
- mysql-data:/var/lib/mysql
api:
build: ./api
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- ./api:/myapp
- gem_data:/usr/local/bundle
ports:
- "3000:3000"
depends_on:
- db
stdin_open: true
tty: true
front:
build: ./front
command: yarn start
ports:
- '8000:3000'
volumes:
- ./front:/myapp
depends_on:
- api
tty: true
volumes:
mysql-data:
gem_data:
driver: local
試したこと
1 yarnの公式を見て、
DockerfileにRUN yarn init -2
やRUN yarn install
を記述したりしましたが、変わりませんでした。
(Dockerfileを記述後に、再度docker-compose build
でイメージの作り直しは当然行なっております。)
2 Dockerfileのnodeのverを変えることも実施
3 docker-composeを書き換え
コマンドを実行するディレクトリが悪いのかと思い、docker-composeのfrontサービスのコマンドを以下の様に書き換えても
command: sh -c "cd front && yarn start"
docker@try1:~/myapp$ docker-compose up
省略
front_1 | sh: cd: line 1: can't cd to front: No such file or directory
db_1 | 2021-12-19T00:43:22.204506Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead.
db_1 | 2021-12-19T00:43:22.204528Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.27) starting as process 1
db_1 | 2021-12-19T00:43:22.244707Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
myapp_front_1 exited with code 2
cdのそんなディレクトリ見つからないよと怒られる
わかる方いらっしゃれば、お知恵をいただけるとありがたいです。
よろしくお願いします。
環境
docker@try1:~/myapp$ docker --version
Docker version 19.03.12, build 48a66213fe
docker@try1:~/myapp$ docker-compose --version
docker-compose version 1.16.1, build 6d1ac21