Hello I am making this post because I have only been able to find people with the opposite issue of the image working in docker but not with docker-compose.
你好,我做这个帖子是因为我只能找到在码头工作的形象相反问题的人,而不是码头-作曲。
I am working on a full-stack application utilizing React, Node, and Mongo.
Here is my set up
我正在开发一个利用React、Node和Mongo的全栈应用程序。这是我的设置
Running docker-compose up
works and has the expected results. Everything connects and runs fine.
运行docker-compose up工作,并有预期的结果。一切都连接得很好,运行得也很好。
However I wanted to be able to run each image in isolation to perform testing.
但是,我希望能够单独运行每个映像以执行测试。
Building the React Dockerfile with docker build -t <name>
followed by docker run <name>
starts the image, dependecies get loaded but then the application hangs. It is unable to be canceled via terminal using ctrl+c, and can only be closed by deleting it from the docker desktop app (or maybe running in detached and killing somewhere else). Either way it does not work.
使用docker build -t构建React Dockerfile,
然后使用docker run
启动镜像,dependecies被加载,但随后应用程序挂起。它无法通过终端使用codeword +c取消,只能通过从docker桌面应用程序中删除它来关闭(或者可能在其他地方运行分离和杀死)。无论哪种方式都不起作用。
docker-compose.yml
Docker-compose.yml
version: "3.8"
services:
react:
build: ./re_react
ports:
- 3000:3000
node: #node service
build: ./node #check for the node folder containing our docker file
ports:
- 3001:3001
environment: #env vars
MONGO_URL: mongodb://db-mongo:27017
depends_on:
db-mongo:
condition: service_started
db-mongo: #MongoDB Service
image: mongo:4.0-xenial
ports:
- 27017:27017
volumes: #volume
- mongo_store:/data/db
healthcheck: #healthcheck also very unsure if its working
test: ["CMD", "/etc/init.d/mongodb", "status"]
timeout: 30s
retries: 5
volumes:
mongo_store:
Node: Dockerfile
节点:Dockerfile
FROM node:18
WORKDIR /server/src
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3001
CMD ["node", "server.js"]
React: Dockerfile
反应:文档文件
FROM node:18-alpine
WORKDIR /react/
COPY package*.json ./
RUN npm install
COPY public/ ./public
COPY src/ ./src
EXPOSE 3000
CMD ["npm", "start"]
I have tried moving some of the commands around in the React:Dockerfile to no avail, and I am considering it could be a port issue?
我已经尝试在Reaction:Dockerfile中移动一些命令,但无济于事,我认为这可能是端口问题?
I'm totally unclear and lost on what the problem could be.
我完全不清楚问题是什么,也不知道是什么问题。
Also the node image runs fine (albiet it depends on an environment variable from the compose so it does generate an error after running, but it does not hang).
此外,节点映像运行良好(虽然它依赖于compose中的一个环境变量,因此它在运行后确实会生成错误,但它不会挂起)。
Output from docker build -t <name>
Docker内部版本的输出-t
The build seems to go without issue
这个版本似乎没有任何问题
Running with docker run <name
Program is dead in the water at this stage and nolonger recieving any inputs
使用Docker Run运行
更多回答
优秀答案推荐
I don't see the actual command, but you have to run the container along with publishing port to the host machine. Same is applicable for the Node.js project as well (backend).
我看不到实际的命令,但您必须运行容器以及将端口发布到主机。这同样适用于Node.js项目(后端)。
docker run --publish 3000:3000
However, this Dockerfile is NOT for production. For that, you have to build the app and serve it through a web server.
但是,此Dockerfile不用于生产。要做到这一点,你必须构建应用程序,并通过网络服务器提供服务。
FROM node:18-alpine AS build
WORKDIR /react
COPY package*.json ./
RUN npm install
COPY ./src .
RUN npm run build
FROM nginx:stable-alpine
COPY --from=build /react/build /usr/share/nginx/html
Try this both for frontend and backend
在前端和后端都尝试此功能
build:
context: #path of backend or frontend project
dockerfile: Dockerfile
BUILD:CONTEXT:#后台或前端项目dockerfile路径:dockerfile
更多回答
Thank you this fixed it. I figured it would probably be some port issue because my compose file was handling the port binding and I wasn't doing that anywhere when I was just running docker.
谢谢你把它修好了。我认为这可能是一些端口问题,因为我的合成文件正在处理端口绑定,而我在运行docker时并没有在任何地方做这件事。
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
您的答案可以通过其他支持信息来改进。请编辑以添加更多详细信息,如引用或文档,以便其他人可以确认您的答案是正确的。你可以在帮助中心找到更多关于如何写出好答案的信息。
我是一名优秀的程序员,十分优秀!