Access Postgres数据库远程使用WebAPI在Docker容器中托管的Azure托管

发布于 2025-02-10 14:24:48 字数 1566 浏览 1 评论 0 原文

我是Azure Cloud Services的新手,所以请原谅我是否是一个愚蠢的问题。

我有一个带有.NET Core WebAPI和Postgres数据库的Docker-Compose文件。我将其运行在Azure作为Web应用程序及其工作(我可以在查询数据库中有数据时查询时)。但是,我想远程访问数据库,以便我可以通过PGADMIN或类似的内容进行检查并查看数据库中的数据。

我确实将端口绑定到我的Docker-Compose中的Pgadmin网站上,但似乎并没有打开端口。我在某个地方阅读了只有端口80和443可以在使用多图像容器时从Azure Web应用中暴露的地方。 (此Docker-Compose在本地工作100%,我可以访问PGADMIN网站,并查看及其所有表格的数据库)。

因此,我的问题是,如何在Azure上使用Postgres数据库运行我的Web-API并可以对数据库具有可见性?

Docker-Compose文件:

version: '3.8'

services:

  web:
    container_name: 'bootcampapi'
    image: 'myimage'
    build:
      context: .
      dockerfile: backend.dockerfile
    restart: always
    ports:
     - 8080:80
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - bootcampbackend-network

  postgres:
    container_name: 'postgres'
    restart: always
    image: 'postgres:latest'
    healthcheck:
      test: ["CMD-SHELL", "pg_isready"]
      interval: 10s
      timeout: 5s
      retries: 5
    environment:
      - POSTGRES_USER=myusername
      - POSTGRES_PASSWORD=mypassword
      - POSTGRES_DB=database-name
      - PGDATA=database-data
    networks:
      - bootcampbackend-network
    ports:
      - 5432:5432
    volumes:
      - database-data:/var/lib/postgresql/data/

  pgadmin:
    image: dpage/pgadmin4
    ports:
      - 15433:80
    env_file:
      - .env
    depends_on:
      - postgres
    networks:
      - bootcampbackend-network
    volumes:
      - database-other:/var/lib/pgadmin/

networks:
  bootcampbackend-network:
    driver: bridge

I am new to Azure cloud services so excuse me if this is a dumb question.

I have a docker-compose file with a .Net core webapi and postgres database. I have it running on Azure as a web-app and its working (I can see when I query the API that there's data in the database). However I would like to get access to the database remotely so that I can inspect and see the data in the database via pgAdmin or something similar.

I did bind a port to my pgAdmin site in my docker-compose but it does not seem like that port is open. I've read somewhere that only port 80 and 443 can be exposed from Azure web-apps when using multi-image containers. (This docker-compose works locally 100% and I can access the pgAdmin site and see the database with all its tables).

So my question is, how do I run my web-api with my postgres database on azure and have visibility to my database?

Docker-compose file:

version: '3.8'

services:

  web:
    container_name: 'bootcampapi'
    image: 'myimage'
    build:
      context: .
      dockerfile: backend.dockerfile
    restart: always
    ports:
     - 8080:80
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - bootcampbackend-network

  postgres:
    container_name: 'postgres'
    restart: always
    image: 'postgres:latest'
    healthcheck:
      test: ["CMD-SHELL", "pg_isready"]
      interval: 10s
      timeout: 5s
      retries: 5
    environment:
      - POSTGRES_USER=myusername
      - POSTGRES_PASSWORD=mypassword
      - POSTGRES_DB=database-name
      - PGDATA=database-data
    networks:
      - bootcampbackend-network
    ports:
      - 5432:5432
    volumes:
      - database-data:/var/lib/postgresql/data/

  pgadmin:
    image: dpage/pgadmin4
    ports:
      - 15433:80
    env_file:
      - .env
    depends_on:
      - postgres
    networks:
      - bootcampbackend-network
    volumes:
      - database-other:/var/lib/pgadmin/

networks:
  bootcampbackend-network:
    driver: bridge

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赤濁 2025-02-17 14:24:48

正如您发现的那样,App Service仅在一个端口上听。一个解决方案是使用像Nginx这样的反向代理将流量路由两个容器路由。

顺便说一句,构建,依赖性和网络不支持。

As you have found, App Service only listens on one port. One solution around that is to use a reverse proxy like Nginx to route the traffic to both your containers.

BTW, build, depends_on and networks are unsupported. See doc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文