N
N
Nikolay Baranenko2020-08-20 10:11:04
Computer networks
Nikolay Baranenko, 2020-08-20 10:11:04

How to solve a network problem, an application from a Docker container does not see the database in another container?

Hello.

Deployed 2 docker containers
- flask_app - web application on flask
- postgresql - Postresql DBMS

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
d5c368b82067        flask:0.1        "flask run -p 5000 -…"   12 minutes ago      Up 12 minutes       0.0.0.0:5000->5000/tcp              flask_app
06fbaf3ffe5d        postgres            "docker-entrypoint.s…"   8 days ago          Up 8 days           0.0.0.0:54320->5432/tcp             postgresql


flask_app returns network error when running

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 54320?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 54320?


when assembling the container, I added telnet and ping to it;
in general, attempts to connect from the flask container to the DBMS Connection refused

flask inspect

"NetworkSettings": {
            "Bridge": "",
            "SandboxID": "8d4d3e7707902e1a97e204d405a3815fda2b4fb19676f619defde5d4c801dd0f",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "5000/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "5000"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/8d4d3e770790",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "167b7f463934cd4c427672df0fefef55772147932b485eed273f70f7b30f703b",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.4",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:04",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "8c8fa606ac1730dd3a2d06e93802ba09f6ceea311f45e02b531c928f9cb4e8e8",
                    "EndpointID": "167b7f463934cd4c427672df0fefef55772147932b485eed273f70f7b30f703b",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.4",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:04",
                    "DriverOpts": null
                }
            }
        }
    }


postgres

"NetworkSettings": {
            "Bridge": "",
            "SandboxID": "340e70753ef7fd6053d3d6f1512e15eff78532d554002dc0664256c1fecb7028",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "5432/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "54320"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/340e70753ef7",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "77c6bd92da4724341c57d4ab8dbf9d84f43df9a7838c93371fc1ac91d2a9c7fa",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.3",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:03",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "8c8fa606ac1730dd3a2d06e93802ba09f6ceea311f45e02b531c928f9cb4e8e8",
                    "EndpointID": "77c6bd92da4724341c57d4ab8dbf9d84f43df9a7838c93371fc1ac91d2a9c7fa",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:03",
                    "DriverOpts": null
                }
            }


How to solve a network problem, an application from a Docker container does not see the database in another container?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2020-08-20
@drno-reg

So it is necessary to address not to the localhost but to the container.
Find out the internal address of the container and knock on it, or even better - organize your containers using docker-compose and then you can access it directly by its name.

N
Nikolay Baranenko, 2020-08-20
@drno-reg

inside flask_app

inclouds.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://user:[email protected]:5432/inclouds"

where postgresqlis the name of the docker container if they are located on the same host

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question