Docker 文件系统未加载到本地主机
目前,我正在开发一个带有 react 前端、mysql DB 和 apache php 实例的全栈应用程序。我从 docker 容器到 localhost 的更改似乎出现了问题。我可以从我的本地机器写入 -> docker,但似乎localhost没有从我的docker容器中读取react。
我知道我的安装工作正常 本地计算机 -> docker 文件系统
因为每当我在 IDE 中进行更改并保存,然后在我的 docker 容器中运行 cat App.js
时,这些更改就在那里。
任何见解都会有所帮助,我认为正在发生的事情是 docker 在创建容器时获取文件的副本,因为每当我重新创建容器时,我都会更改为本地主机。
ps 我是 docker 新手,所以如果您需要更多信息,请告诉我。谢谢!
docker-compose
version: "3.7"
services:
frontend:
container_name: frontend
build:
context: "./hartley_react"
dockerfile: Dockerfile
volumes:
- "./hartley_react:/app"
- "/app/node_modules"
ports:
- 3000:3000
stdin_open: true
environment:
- CHOKIDAR_USEPOLLING=true
command: npm start
php:
container_name: php
build:
context: "./dockerfiles/php-img/"
ports:
- "80:80"
volumes:
- ./src:/var/www/html/
db:
container_name: db
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: userdb
MYSQL_USER: my_user
MYSQL_PASSWORD: my_password
volumes:
- ./mysqldata:/var/lib/mysql
adminer:
container_name: adminer
depends_on:
- db
image: adminer
restart: always
ports:
- 8080:8080
volumes:
my-mysqldata:
frontend:
React DockerFile
FROM node:17.4.0-alpine
WORKDIR /app
COPY package.json .
RUN npm install
COPY . ./
EXPOSE 3000
CMD [ "npm", "start" ]
Current I am working on a full stack application with a react frontend, mysql DB, and apache php instance. Something seems to be up with my changes going from my docker container to localhost. I can write from my local machine -> docker, but it seems like localhost is not reading react from my docker container.
I know that my mount is working correctly local machine -> docker file system
because whenever I make changes in my IDE and save, then go and cat App.js
within my docker container, that changes are there.
Any insight would be helpful, I think what is happening is that docker is taking a copy of the file upon creating the container, because whenever I remake the container, my changes to through to localhost.
p.s. I'm newish to docker, so let me know if you need more information. Thanks!
docker-compose
version: "3.7"
services:
frontend:
container_name: frontend
build:
context: "./hartley_react"
dockerfile: Dockerfile
volumes:
- "./hartley_react:/app"
- "/app/node_modules"
ports:
- 3000:3000
stdin_open: true
environment:
- CHOKIDAR_USEPOLLING=true
command: npm start
php:
container_name: php
build:
context: "./dockerfiles/php-img/"
ports:
- "80:80"
volumes:
- ./src:/var/www/html/
db:
container_name: db
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: userdb
MYSQL_USER: my_user
MYSQL_PASSWORD: my_password
volumes:
- ./mysqldata:/var/lib/mysql
adminer:
container_name: adminer
depends_on:
- db
image: adminer
restart: always
ports:
- 8080:8080
volumes:
my-mysqldata:
frontend:
React DockerFile
FROM node:17.4.0-alpine
WORKDIR /app
COPY package.json .
RUN npm install
COPY . ./
EXPOSE 3000
CMD [ "npm", "start" ]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜你的问题是,如果你编辑文件,
npm start
不会自动重新加载。为此,您可以使用nodemon
或supervisor
,它们可以在每次更新文件时重新加载项目。否则,您应该手动重新启动(可能通过重新启动 docker 容器)I guess your problem is that
npm start
do not auto reload if you edit your files. For that you could usenodemon
orsupervisor
which can reload the project each time a file is updated. Otherwise you should restart manually (probably by restarting the docker container)您可以尝试以下一些操作:
检查您的 package.json 文件,特别是启动脚本是否为 npm start 提供热重载选项。
为此,您可以在本地(不使用 docker)进行完整测试,并检查您在 html(前端)中所做的更改是否确实反映在本地应用程序中,而无需重建。
其次,在 package.json 文件(自定义脚本)中创建另一个脚本,以使 npm run/ npm dev (在 React 中可用,但不确定是否适合您的情况)进行热重载或使用 nodemon 。
一旦你有了这个,在你的 docker-compose 文件中使用它来代替 CMD [ "npm", "start" ]
对我来说,看起来你的 dockerfile 和 docker-compose 文件以及指定的卷定义看起来没问题。
但只有一件事 - 不确定为什么你在 docker-compose 文件中提到“命令:npm start”,而你在创建图像时已经在 dockerfile 中覆盖了该部分。
There are a few things you can try:
check your package.json file and specifically scripts whether start gives npm start with hot reload option or not.
to do so, you may do the full test in your local (without docker) and check whether the changes you are making in html (frontend) is indeed reflecting to your application locally without rebuilding or not.
secondly, create another script inside package.json file (custom script) to have npm run/ npm dev (available in react but not sure for your case) with hot-reload or use nodemon for that.
Once you have that, use that in your docker-compose file in place of CMD [ "npm", "start" ]
For me, It looks like your dockerfile and docker-compose file along with the named volume definition looks ok.
Only one thing though - Not sure why did you mention the "command: npm start" inside docker-compose file while you already have covered that part in your dockerfile while creating an image.