簡體   English   中英

連接postgresql docker配置docker-compose的容器

[英]Connect to postgresql docker container configured with docker-compose

我有一台配置了 docker-compose 的 postgresql 服務器:

version: "3"
services:

  heritagebuxfr-postgres:
    image: postgres:12
    environment:
      - POSTGRES_DB=heritagebuxfr
      - POSTGRES_USER=heritagebuxfr
      - POSTGRES_PASSWORD=xxxxxxxx
    restart: always
    volumes:
      - /data/heritagebuxfr-posgtres:/var/lib/postgresql/data
    networks:
      heritagebuxfr:
        ipv4_address: 10.8.0.5

networks:
  heritagebuxfr:
    driver: bridge
    ipam:
      config:
        - subnet: 10.8.0.0/16

當我嘗試連接它時,它失敗了。 例子:

docker run -it --rm --network root_heritagebuxfr postgres psql -h heritagebuxfr-postgres -U heritagebuxfr

psql: error: connection to server at "heritagebuxfr-postgres" (10.8.0.5), port 5432 failed: Connection timed out
    Is the server running on that host and accepting TCP/IP connections?

如何配置我的 postgresql 容器或其他容器才能加入?

下面是一些附加信息:

docker ps

CONTAINER ID   IMAGE                       COMMAND                  CREATED         STATUS              PORTS                                                                                                                             NAMES
5543d6f43a00   algoo/tracim:4.4.2          "/bin/bash /tracim/t…"   12 hours ago    Up About a minute   0.0.0.0:10088->80/tcp, :::10088->80/tcp                                                                                           root_heritagebuxfr_1
28739c1d135a   postgres:12                 "docker-entrypoint.s…"   12 hours ago    Up 12 hours         5432/tcp                                                                                                                          root_heritagebuxfr-postgres_1
[...]
docker network ls

root@s2:~# docker network ls
NETWORK ID     NAME                 DRIVER    SCOPE
924e62e9349c   bridge               bridge    local
d82517c5bf4a   host                 host      local
0a94208c6a9c   none                 null      local
794be35c7046   prestashop-net       bridge    local
3c718488b040   root_default         bridge    local
f3dafc20a876   root_dentiste74fr    bridge    local
3be74184dda3   root_heritagebuxfr   bridge    local
41db94bb460a   root_muzich          bridge    local
4c1fc0c30383   root_rocketchatmah   bridge    local
0537f6c1667f   root_tracimbuxfr     bridge    local
iptables -L

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
DOCKER-USER  all  --  anywhere             anywhere            
DOCKER-ISOLATION-STAGE-1  all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain DOCKER (8 references)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             172.18.0.2           tcp dpt:9980
ACCEPT     tcp  --  anywhere             10.5.0.6             tcp dpt:http
ACCEPT     tcp  --  anywhere             10.7.0.4             tcp dpt:http
ACCEPT     tcp  --  anywhere             10.7.0.4             tcp dpt:3030
ACCEPT     tcp  --  anywhere             172.17.0.2           tcp dpt:http
ACCEPT     tcp  --  anywhere             172.18.0.3           tcp dpt:3000
ACCEPT     tcp  --  anywhere             172.18.0.3           tcp dpt:ssh
ACCEPT     tcp  --  anywhere             10.6.0.6             tcp dpt:http
ACCEPT     tcp  --  anywhere             172.18.0.4           tcp dpt:http-alt
ACCEPT     tcp  --  anywhere             172.18.0.5           tcp dpt:8082
ACCEPT     tcp  --  anywhere             172.18.0.5           tcp dpt:https
ACCEPT     tcp  --  anywhere             172.18.0.5           tcp dpt:http
ACCEPT     tcp  --  anywhere             10.8.0.4             tcp dpt:http

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target     prot opt source               destination         
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere            
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere            
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere            
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere            
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere            
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere            
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere            
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere            
RETURN     all  --  anywhere             anywhere            

Chain DOCKER-ISOLATION-STAGE-2 (8 references)
target     prot opt source               destination         
DROP       all  --  anywhere             anywhere            
DROP       all  --  anywhere             anywhere            
DROP       all  --  anywhere             anywhere            
DROP       all  --  anywhere             anywhere            
DROP       all  --  anywhere             anywhere            
DROP       all  --  anywhere             anywhere            
DROP       all  --  anywhere             anywhere            
DROP       all  --  anywhere             anywhere            
RETURN     all  --  anywhere             anywhere            

Chain DOCKER-USER (1 references)
target     prot opt source               destination         
RETURN     all  --  anywhere             anywhere
docker network inspect root_heritagebuxfr
[
    {
        "Name": "root_heritagebuxfr",
        "Id": "3b9d353561a02dd9bea8bbc66258aa120fbd464cf8fb519c4b47ed9f82110d8e",
        "Created": "2022-11-15T10:27:39.751614908Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "10.8.0.0/16"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "673decc2a8a4493fbdb01000684ed0e8f42905bea200e6efc412bd55f27fcca1": {
                "Name": "root-heritagebuxfr-postgres-1",
                "EndpointID": "35d0cd973c79758c242b9510e475e827b5c7aa6633aedb3daa655147ae89668f",
                "MacAddress": "02:42:0a:08:00:05",
                "IPv4Address": "10.8.0.5/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "heritagebuxfr",
            "com.docker.compose.project": "root",
            "com.docker.compose.version": "2.12.2"
        }
    }
]
root@s2:~# docker --version
Docker version 20.10.21, build baeda1f
root@s2:~# docker-compose --version
docker-compose version 1.21.2, build a133471
$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

注意:我閱讀了所有有關主題的堆棧溢出答案,但沒有人適用於此配置。

使用docker compose子命令的 Compose V2 對我來說工作正常。 您的問題是您仍在使用舊的 docker-compose cli。 嘗試docker compose up在 docker 和 compose 之間不使用破折號。

如果您還沒有 Compose V2,請升級到它,此站點將引導您完成它: https://www.howtogeek.com/devops/how-to-upgrade-to-docker-compose-v2/

或者您可以查看 docker 文檔: https://docs.docker.com/compose/install/

$ docker ps

CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS         PORTS      NAMES
f86a00472ece   postgres:12   "docker-entrypoint.s…"   4 minutes ago   Up 3 minutes   5432/tcp   test-heritagebuxfr-postgres-1

$ docker network ls

NETWORK ID     NAME                                                        DRIVER    SCOPE
4f6e933d9297   test_heritagebuxfr                                          bridge    local

$ docker run -it --rm --network test_heritagebuxfr postgres psql -h heritagebuxfr-postgres -U heritagebuxfr

Password for user heritagebuxfr:
psql (15.1 (Debian 15.1-1.pgdg110+1), server 12.13 (Debian 12.13-1.pgdg110+1))
Type "help" for help.

heritagebuxfr=# \l
                                                          List of databases
     Name      |     Owner     | Encoding |  Collate   |   Ctype    | ICU Locale | Locale Provider |        Access privileges
---------------+---------------+----------+------------+------------+------------+-----------------+---------------------------------
 heritagebuxfr | heritagebuxfr | UTF8     | en_US.utf8 | en_US.utf8 |            | libc            |
 postgres      | heritagebuxfr | UTF8     | en_US.utf8 | en_US.utf8 |            | libc            |
 template0     | heritagebuxfr | UTF8     | en_US.utf8 | en_US.utf8 |            | libc            | =c/heritagebuxfr               +
               |               |          |            |            |            |                 | heritagebuxfr=CTc/heritagebuxfr
 template1     | heritagebuxfr | UTF8     | en_US.utf8 | en_US.utf8 |            | libc            | =c/heritagebuxfr               +
               |               |          |            |            |            |                 | heritagebuxfr=CTc/heritagebuxfr
(4 rows)

暫無
暫無

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

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