Docker-compose两个服务无法相互访问。连接被拒绝。

huangapple go评论115阅读模式
英文:

Docker-compose two service cannot access each other. Connection refused

问题

以下是您要翻译的内容:

I am new to docker, now I am trying to containerize my frontend and backend, React and Flask application.

I created my docker-compose file as following, I try to expose them on the same network.

  1. version: '3'
  2. services:
  3. frontend:
  4. build: ./client
  5. ports:
  6. - "3000:3000"
  7. environment:
  8. - BACKEND_URL=http://backend:5000
  9. networks:
  10. - my-network
  11. depends_on:
  12. - backend
  13. backend:
  14. build: ./backend
  15. environment:
  16. - FLASK_ENV=production
  17. ports:
  18. - "5005:5000"
  19. networks:
  20. - my-network
  21. networks:
  22. my-network:
  23. driver: bridge

Here's my frontend Dockerfile

  1. # Use an official Node.js runtime as a parent image
  2. FROM node:16.13.2-alpine
  3. # Set the working directory to /app
  4. WORKDIR /app
  5. # Copy the package.json and package-lock.json files to the container
  6. COPY package*.json ./
  7. # Install the dependencies
  8. RUN npm install
  9. # Copy the rest of the application code to the container
  10. COPY . .
  11. # Build the application
  12. RUN npm run build
  13. # Expose the port on which the application will run
  14. EXPOSE 3000
  15. # Define the command to start the application
  16. CMD ["npm", "start"]

and here is my backend dockerfile

  1. FROM python:3.9
  2. COPY requirements.txt /app/
  3. RUN pip install --no-cache-dir -r /app/requirements.txt
  4. COPY . /app/
  5. WORKDIR /app/
  6. EXPOSE 5000
  7. CMD ["python3", "-m", "flask", "run"]

In my front end code, I tried to access the backend host port 5000 with this code

  1. axios.post(`http://backend:5000/deletecode`, {
  2. 'uuid': uuid,
  3. }, {
  4. headers: {
  5. 'Content-Type': 'application/json'
  6. }
  7. })

But I got a URL not found, and there's nothing return back to me.

Here's what I tried so far to resolve this issue.

  1. I shell into the front end container and PING the backend service, and got the response.
  1. /app # ping backend:5000
  2. PING backend:5000 (172.21.0.2): 56 data bytes
  3. 64 bytes from 172.21.0.2: seq=0 ttl=64 time=0.257 ms
  4. 64 bytes from 172.21.0.2: seq=1 ttl=64 time=0.292 ms
  1. I tried to send a URL request within the backend container, and got success
  1. # curl http://localhost:5000/test
  2. {
  3. "message": "healthy response"
  4. }
  1. I tried to curl the backend/test from my frontend shell, and it's being denied.
  1. /app # curl backend:5000/test
  2. curl: (7) Failed to connect to backend port 5000 after 1 ms: Connection refused
  1. I tried to curl the backend/test on port 5005, which is the port exposed to the host, from the host terminal, and got an empty response.
  1. shaopengliu@Shaopengs-MBP Evase % curl http://localhost:5005/test
  2. curl: (52) Empty reply from server

Thanks for looking into this question thus far, if there's any suggestion of what I might be doing wrong, or anything I could try out, would be much appreciated.

英文:

I am new to docker, now I am trying to containerize my frontend and backend, React and Flask application.

I created my docker-compose file as following, I try to expose them on the same network.

  1. version: '3'
  2. services:
  3. frontend:
  4. build: ./client
  5. ports:
  6. - "3000:3000"
  7. environment:
  8. - BACKEND_URL=http://backend:5000
  9. networks:
  10. - my-network
  11. depends_on:
  12. - backend
  13. backend:
  14. build: ./backend
  15. environment:
  16. - FLASK_ENV=production
  17. ports:
  18. - "5005:5000"
  19. networks:
  20. - my-network
  21. networks:
  22. my-network:
  23. driver: bridge

Here's my frontend Dockerfile

  1. # Use an official Node.js runtime as a parent image
  2. FROM node:16.13.2-alpine
  3. # Set the working directory to /app
  4. WORKDIR /app
  5. # Copy the package.json and package-lock.json files to the container
  6. COPY package*.json ./
  7. # Install the dependencies
  8. RUN npm install
  9. # Copy the rest of the application code to the container
  10. COPY . .
  11. # Build the application
  12. RUN npm run build
  13. # Expose the port on which the application will run
  14. EXPOSE 3000
  15. # Define the command to start the application
  16. CMD ["npm", "start"]

and here is my backend dockerfile

  1. FROM python:3.9
  2. COPY requirements.txt /app/
  3. RUN pip install --no-cache-dir -r /app/requirements.txt
  4. COPY . /app/
  5. WORKDIR /app/
  6. EXPOSE 5000
  7. CMD ["python3", "-m", "flask", "run"]

In my front end code, I tried to access the backend host port 5000 with this code

  1. axios.post(`http://backend:5000/deletecode`, {
  2. 'uuid': uuid,
  3. }, {
  4. headers: {
  5. 'Content-Type': 'application/json'
  6. }
  7. })

But I got a URL not found, and there's nothing return back to me.

Here's what I tried so far to resolve this issue.

  1. I shell in to the front end container and PING the backend service, and got the response.

> /app # ping backend:5000
>
> PING backend:5000 (172.21.0.2): 56 data bytes
> 64 bytes from 172.21.0.2: seq=0 ttl=64 time=0.257 ms
> 64 bytes from 172.21.0.2: seq=1 ttl=64 time=0.292 ms

  1. I tried to send a URL request within the backend container, and got success

> # curl http://localhost:5000/test
>
> {
> "message": "healthy response"
> }

  1. I tried to curl the backend/test from my frontend shell, and it's be denied.

> /app # curl backend:5000/test
> curl: (7) Failed to connect to backend port 5000 after 1 ms: Connection refused

  1. I tried to curl the backend/test on port 5005, which is the port exposed to the host, from the host terminal, and got empty response.

> shaopengliu@Shaopengs-MBP Evase % curl http://localhost:5005/test
> curl: (52) Empty reply from server

Thanks for looking into this question thus far, if there's any suggestion of what I might doing wrong, or anything I could tried out, would be much appreciated.

答案1

得分: 1

以下是要翻译的内容:

在您的compose.yml文件中存在一个错误:
每当您指定要公开的端口时,语法是:<host_port>:<container_port>,所以在您的情况下,对于后端,您正在将容器端口5000暴露在本地端口5005,而您的前端正在尝试连接到端口5000。要解决这个问题,您可以简单地更改:

backend:
build: ./backend
environment:
- FLASK_ENV=production
ports:
- "5000:5000"
networks:
- my-network

---- 编辑 -----

另一个选项是保留当前的docker-compose配置:

backend:
build: ./backend
environment:
- FLASK_ENV=production
ports:
- "5005:5000"
networks:
- my-network

然后修改前端逻辑,将前端连接到端口5005而不是端口5000:

axios.post(http://backend:5005/deletecode, {
'uuid': uuid,
}, {
headers: {
'Content-Type': 'application/json'
}
})

这应该解决您的问题,并允许您为后端使用不同的端口(因为端口5000已经在使用中)。

英文:

There is an error in your compose.yml file:
whenever you specify a port to expose the syntax is: <host_port>:<container_port>, so in your case for the backed you are exposing the container port 5000 on the local port 5005 while your frontend is trying to connect to port 5000. To solve this problem you can simply change:

  1. backend:
  2. build: ./backend
  3. environment:
  4. - FLASK_ENV=production
  5. ports:
  6. - &quot;5000:5000&quot;
  7. networks:
  8. - my-network

---- EDIT -----

Another option is leaving the current docker-compose configuration:

  1. backend:
  2. build: ./backend
  3. environment:
  4. - FLASK_ENV=production
  5. ports:
  6. - &quot;5005:5000&quot;
  7. networks:
  8. - my-network

And then modify the frontend logic to connect the frontend to the port 5005 instead of port 5000:

  1. axios.post(`http://backend:5005/deletecode`, {
  2. &#39;uuid&#39;: uuid,
  3. }, {
  4. headers: {
  5. &#39;Content-Type&#39;: &#39;application/json&#39;
  6. }
  7. })

This should solve your problem and allow use a different port for the backend (since the 5000 is already in use).

huangapple
  • 本文由 发表于 2023年4月7日 00:18:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定