簡體   English   中英

無法從外部訪問 web 容器(Windows Docker-Desktop)

[英]Can't access web container from outside (Windows Docker-Desktop)

我在 Windows 上使用 Docker-Desktop,我試圖在 docker-desktop 中運行 3 個容器。 經過一些研究和測試,我讓 3 個容器運行 [WEB - API - DB],日志中的一切似乎都可以正常編譯/運行,但我無法從外部訪問我的 web 容器。

這是我的 dockerfile 和 docker-compose,我錯過了什么或弄錯了什么?

[網絡] dockerfile

FROM node:16.17.0-bullseye-slim
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .

#EXPOSE 4200 (the issue is the same with or without this line)

CMD ["npm", "run", "start"]

[原料葯] dockerfile

FROM openjdk:17.0.1-jdk-slim
WORKDIR /app
COPY ./target/test-0.0.1-SNAPSHOT.jar /app

#EXPOSE 2022 (the issue is the same with or without this line)

CMD ["java", "-jar", "test-0.0.1-SNAPSHOT.jar"]

Docker-compose檔案

version: "3.8"

services:
  ### FRONTEND ###
  web:
    container_name: wallet-web
    restart: always
    build: ./frontend
    ports:
      - "80:4200"
    depends_on:
      - "api"
    networks:
      customnetwork:
        ipv4_address: 172.20.0.12
    #networks:
    #  - "api"
    #  - "web"

  ### BACKEND ###
  api:
    container_name: wallet-api
    restart: always
    build: ./backend
    ports:
      - "2022:2022"
    depends_on:
      - "db"
    networks:
      customnetwork:
        ipv4_address: 172.20.0.11
    #networks:
    #  - "api"
    #  - "web"

  ### DATABASE ###
  db:
    container_name: wallet-db
    restart: always
    image: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_DB=postgres
    networks:
      customnetwork:
        ipv4_address: 172.20.0.10
    #networks:
    #  - "api"
    #  - "web"
networks:
  customnetwork:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16
          gateway: 172.20.0.1
#  api:
#  web:

正在收聽:在此處輸入圖片描述

我發現了幾個與我的問題類似的問題,但該解決方案對我不起作用。

您是否查看過瀏覽器開發控制台,是否出現任何錯誤。 您的 docker-compose 似乎沒有任何問題。

讓我們嘗試調試它:

docker ps

CONTAINER ID   IMAGE             COMMAND                  CREATED             STATUS             PORTS                    NAMES
6245eaffd67e   nginx             "/docker-entrypoint.…"   About an hour ago   Up About an hour   0.0.0.0:4200->80/tcp     test-api-1

復制容器 ID 然后執行:

 docker exec -it 6245eaffd67e  bin/bash  

現在您在容器內。 除了 id,您還可以使用容器名稱。

curl http://localhost:80

注意:在我的例子中,我只是從 nginx 圖像創建一個容器。 在您的情況下,請使用您的應用程序運行的端口。 如果您不確定,請在您的代碼中控制它。 許多 Javascript 框架默認在 3000 上啟動。

如果出現錯誤:找不到 curl 命令,請將其安裝到您的映像中:


FROM node:16.17.0-bullseye-slim
USER root # to install dependencies you need sudo permissions su we tell the image that it is root
RUN apt update -y && apt install curl -y
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .

#EXPOSE 4200 (the issue is the same with or without this line)
USER node # we dont want to execute the image as root so we put user node (this user is defined in the  node:16.17.0-bullseye-slim image)
CMD ["npm", "run", "start"]

現在 curl 應該可以工作了(如果它還沒有的話)。

您的主機也應該這樣做。 這是一件重要的事情:

localhost,總是指的是財政計算機,或者你所指的容器本身。 每個容器和您的 PC 都有本地主機,但它們並不相同。 在 docker-compose 中,您只需 map 端口主機/容器,因此運行 docker 的 PC(主機)可以從您在容器端口內定義的主機端口上的主機訪問 docker.network。

如果您仍然無法從主機訪問,請嘗試更改主機端口 2022、4200 ecc。 可能是您的 Windows 機器上發生了某些沖突。

有時 docker.networks 會產生一些沖突。 往下執行一個docker-compose,所以應該是delete再創建。

還是行不通?

將 docker-desktop 重置為出廠設置,控制你是否有最新版本(這總是更好)。

如果所有這些都沒有幫助,請告訴我,以便我們進一步調試。

為了清楚起見,我在這里向您發布了我曾經檢查過的 docker-compose。 我只是使用 nginx 來測試端口,因為我沒有你的圖像。

version: "3.8"

services:
  ### FRONTEND ###
  web:
    restart: always
    image: nginx
    ports:
      - "4200:80"
    depends_on:
      - "api"
    networks:
      - "web"

  ### BACKEND ###
  api:
    restart: always
    image: nginx
    ports:
      - "2022:80"
    depends_on:
      - "db"
    networks:
      - "api"
      - "web"

  ### DATABASE ###
  db:
    restart: always
    image: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_DB=postgres
    networks:
      - "api"
networks:
  api:
  web:
```

Update:

You can log what happens in the conatiner like so:

```
docker logs containerid/name
```

If you are using Visualcode there is excellent extension for docker build also by Microsoft:
Just search docker in the extensions. Has something like 20.000.000 downloads and can help you a lot debugging containers ecc. After installing it you see the dockericon on the left toolbar.

If you can see directly the errors that occurs in the logs, maybe you can post them partially. So it would be possible to understand. Please tell also something about your Frontendapp architecture, (react-app, angular). There are some frameworks that need to be startet on 0.0.0.0 instead of 127.0.0.1 or they dont work.


如果我知道你正在嘗試訪問端口 80。為此,你必須將 yaml 文件中的 map 容器端口 4200 更改為 80,而不是 4200:4200。

https://docs.docker.com/config/containers/container.networking/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM