简体   繁体   English

在Docker容器中浏览postgres

[英]browse postgres in a docker container

I am using docker-compose to work across multiple docker containers, all these containers are mostly individual django rest framework built applications. 我正在使用docker-compose跨多个docker容器工作,所有这些容器大多是单独的Django rest框架构建的应用程序。 I have downloaded all the containers and am able to build the whole application using all these containers. 我已经下载了所有容器,并能够使用所有这些容器来构建整个应用程序。

Each container has postgres db running, I want to browse the db now using any ui tool. 每个容器都有正在运行的postgres db,我现在想使用任何ui工具浏览该db。 I know pgadmin can do the work here, but how I can configure my pgadmin to showcase any postgres database from these containers? 我知道pgadmin可以在这里完成工作,但是如何配置pgadmin来展示这些容器中的任何postgres数据库?

It should be possible to expose your database port also to your local network. 还应该将您的数据库端口也公开给本地网络。

Normally you connect your application containers internally to the database container. 通常,您将应用程序容器内部连接到数据库容器。 In that case it's not needed declare the ports section in your compose file for the database, but if you have that entry you bind your database in addition to your local host. 在这种情况下,无需在数据库的撰写文件中声明端口部分,但是如果您有该条目,则除了本地主机之外,还可以绑定数据库。

After you have also expose the postgres port to your host port it should be no problem to connect with the gui tool of your choice. 将postgres端口公开到主机端口之后,使用您选择的gui工具连接应该没有问题。

version: '3.2'
services:
    httpd:
        image: "oth/d_apache2.4:0.2"
        ports:
                # container port 80 of the webserver to localhost 80   
                - "80:80"
    keycloak:
        # keycloak uses keycloak_db
        image: "jboss/keycloak-postgres:3.2.1.Final"
        environment:
              # internal network reference to db container
              - POSTGRES_PORT_5432_TCP_ADDR=keycloak_db
              - POSTGRES_PORT_5432_TCP_PORT=5432
    keycloak_db:
        environment:
        image: "postgres:alpine"
        ports:
                # container port 5432 to localhost 5432 
                # stack intern is the port still available 
                - "5432:5432"

Make sure that the port of the postgres container is mapped to the host system. 确保postgres容器的端口已映射到主机系统。 The default postgres port is 5432 . 默认的postgres端口是5432 You can do that with the port directive in your docker-compose.yml . 您可以使用docker-compose.ymlport指令执行此docker-compose.yml You are only able to map the port once. 您只能映射一次端口。 So your config file would look like: 因此,您的配置文件如下所示:

services:
  postgres_1:
    ports:
      - "49000:54321"
    [...]
  postgres_2:
    ports:
      - "49001:54321"
    [...]

After that you should be able to access the desired database with the IP of your docker host and the above specified port. 之后,您应该能够使用docker主机的IP和上面指定的端口访问所需的数据库。


If you still encounter problems connecting with a client like pgadmin check the following configuration files inside your container. 如果您仍然遇到与pgadmin客户端连接的问题,请检查容器中的以下配置文件。

  1. Is there anything blocking your connection attempt? 有什么阻止您的连接尝试吗? Is your docker host behind a firewall? 您的docker主机是否在防火墙后面?
  2. postgresql.conf under the section connections and authentication : 连接和认证部分下的postgresql.conf

    • listen_addresses
    • port
  3. Check your pg_hba.conf , which controls client authentication . 检查您的pg_hba.conf ,它控制客户端身份验证

    • For debug purposes you can set it to the following: 出于调试目的,您可以将其设置为以下内容:

      Don't do the following in production : 在生产中请勿执行以下操作

       host all all all trust 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM