docker postgres pgadmin local connection

前端 未结 16 2158
清歌不尽
清歌不尽 2020-12-02 06:44

I have created an ubuntu image with nginx, php and postgres.

I want to connect the postgres database in my current image with pgadmin located on my loca

相关标签:
16条回答
  • 2020-12-02 06:51

    If pgAdmin is intended to be run wihtin same Ubuntu host/guest, then you need to link postgres container, so it could be resolved by a name.

    1. Run a postgres container:

    docker run --name some-postgres -e POSTGRES_PASSWORD=postgres -d postgres
    

    2. Run pgAdmin container:

    docker run -p 80:80 --link some-postgres -e "PGADMIN_DEFAULT_EMAIL=email@domain.com" -e "PGADMIN_DEFAULT_PASSWORD=postgres" -d dpage/pgadmin4
    

    3. Now when adding new server in phAdmin web app, use some-postgres as server name

    Note the --link some-postgres when we were bringing up the pgAdmin. This command makes postgres container visible to pgAdmin container.

    0 讨论(0)
  • 2020-12-02 06:53

    You have to expose the postgres port in the container to you local system. You do this by running your container like this:

    docker run -p 5432:5432 <name/id of container>

    when connecting with your GUI client or CLI make sure to use the ip-address not localhost even if your ip-address is the localhost ip-address.

    docker ps would give you the ip address your postgres container is on.

    0 讨论(0)
  • 2020-12-02 06:53

    If you verified that PostgreSQL is running and you can connect there with local copy of PgAdmin...

    The answer is simple: use host.docker.internal istead of localhost for the PgAdmin running inside the Docker

    0 讨论(0)
  • 2020-12-02 06:54

    In my case, I had a PostgreSQL container, so I didn't change my container or create a docker-compose approach, I needed pgadming after few months to had installed PostgreSQL, so this is my approach:

    1. docker inspect my_container_id_postgreSQL
    2. The network assigned to PostgreSQL was:

      "Networks": { "docker_default": { "IPAddress": "172.18.0.2", ... } }

    3. Ran my PGADMIN with --network command.

      docker run -p 85:80 --network docker_default -e 'PGADMIN_DEFAULT_EMAIL=user@domain.com' -e 'PGADMIN_DEFAULT_PASSWORD=SuperSecret' -d dpage/pgadmin4

    4. Insert the Ip address into pgAdmin and the database credentials used in docker.

    I hope this can be useful for someone. Regards.

    0 讨论(0)
  • 2020-12-02 06:54

    After facing this issue for two days i was able to resolve that issue.

    solution of this problem is already answered by peoples like do inspect

    docker inspect CONTAINER_ID

    but while running this command i got a-lot of logs like Host Config Config Network Settings etc. so i got confused which IPAddress to add in the pgAdmin connection because i tried 0.0.0.0 and config, host, networkSettings different -2 IPAddress in the logs but finally it works after trying a-lot.

    it works with which IP, we have to add that network ip address (which we created to connect the postgres and pgAdmin.)

    like in my case when i run :-

    docker inspect postgres_container

     "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "sdffsffsfsfsfsf123232e2r3pru3ouoyoyuosyvo8769696796",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "5432/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "5432"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/231231Ad132",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "postgres": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "postgres",
                        "0e2j3bn2m42"
                    ],
                    "NetworkID": "35rhlhl3l53l5hlh3523233k3k4k22l3hk2k4",
                    "EndpointID":"b3424n43k52o2i4n235k1k4l2l4hj234f14n2",
                    "Gateway": "192.168.16.1",
                    "IPAddress": "192.168.16.2",
                    "IPPrefixLen": 20,
                    "IPv6Gateway": "",
    

    so we have to add the NetworkSettings -> Network -> Postgres(mine created network) -> IPAddress i.e. "IPAddress": "192.168.16.2".

    After adding this ip it will work.

    I hope it will help.

    0 讨论(0)
  • 2020-12-02 06:57

    I included this in the docker yaml file to get the database and pgAdmin:

    database:
        image: postgres:10.4-alpine
        container_name: kafka-nodejs-example-database
        environment:
          POSTGRES_USER: ${POSTGRES_USER}
          POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
        expose:
          - "5432"
        ports:
          - 8000:5432
        volumes:
          - ./services/database/schema.sql:/docker-entrypoint-initdb.d/1-schema.sql
          - ./services/database/seed.sql:/docker-entrypoint-initdb.d/2-seed.sql
      pgadmin:
        image: dpage/pgadmin4
        ports:
          - 5454:5454/tcp
        environment:
          - PGADMIN_DEFAULT_EMAIL=admin@mydomain.com
          - PGADMIN_DEFAULT_PASSWORD=postgres
          - PGADMIN_LISTEN_PORT=5454
    

    The postgres username is alphaone and the password is xxxxxxxxxxx.

    Do a docker ps to get the container id and then docker inspect <dockerContainerId> | grep IPAddress

    eg: docker inspect 2f50fabe8a87 | grep IPAddress

    Insert the Ip address into pgAdmin and the database credentials used in docker:

    0 讨论(0)
提交回复
热议问题