mikeizbicki / cmc-csci143

big data course materials
40 stars 76 forks source link

Final project: forward host lookup failed: Host name lookup failure : Resource temporarily unavailable #556

Open danzhechen opened 6 months ago

danzhechen commented 6 months ago

Hey everyone,

UPDATE

I edited my .env.dev file to be

FLASK_APP=project/__init__.py
FLASK_DEBUG=1
FLASK_RUN_PORT=5000
DATABASE_URL=postgresql://postgres:pass@localhost:9998
SQL_HOST=postgres
SQL_PORT=5432
DATABASE=postgres
APP_FOLDER=/usr/src/app

This seems to fixed the first issue, but I still got the issue of

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "localhost" (127.0.0.1), port 9998 failed: Connection refused
    Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (::1), port 9998 failed: Cannot assign requested address
    Is the server running on that host and accepting TCP/IP connections?

(Background on this error at: https://sqlalche.me/e/20/e3q8)
Tables created
 * Serving Flask app 'project/__init__.py'
 * Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:5000
 * Running on http://192.168.0.3:5000
Press CTRL+C to quit
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 627-260-610

Origin I am working on the final project and I am at the first two steps to make sure my docker is running. I just docker-compose up -d --build to build the docker and when running docker ps, both dockers are running on my side, which shows,

docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED         STATUS         PORTS                                       NAMES
c68fe6c5d085   final-project_web        "/usr/src/app/entryp…"   3 seconds ago   Up 2 seconds   0.0.0.0:9998->5000/tcp, :::9998->5000/tcp   final-project_web_1
d340154b6113   final-project_postgres   "docker-entrypoint.s…"   4 seconds ago   Up 3 seconds   0.0.0.0:9999->5432/tcp, :::9999->5432/tcp   final-project_postgres_1

but when I run docker logs c68fe6c5d085, it shows that

Waiting for postgres...
db: forward host lookup failed: Host name lookup failure : Resource temporarily unavailable

The other docker has this message,

docker logs d340154b6113
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
initdb: warning: enabling "trust" authentication for local connections
initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.
syncing data to disk ... ok

Success. You can now start the database server using:

    pg_ctl -D /var/lib/postgresql/data -l logfile start

waiting for server to start....2024-05-04 04:06:07.702 UTC [48] LOG:  starting PostgreSQL 16.2 (Debian 16.2-1.pgdg110+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2024-05-04 04:06:07.703 UTC [48] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2024-05-04 04:06:07.706 UTC [51] LOG:  database system was shut down at 2024-05-04 04:06:07 UTC
2024-05-04 04:06:07.709 UTC [48] LOG:  database system is ready to accept connections
 done
server started

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/01.sql
CREATE TABLE
CREATE TABLE
CREATE TABLE

/usr/local/bin/docker-entrypoint.sh: sourcing /docker-entrypoint-initdb.d/10_postgis.sh
CREATE DATABASE
Loading PostGIS extensions into template_postgis
CREATE EXTENSION
CREATE EXTENSION
You are now connected to database "template_postgis" as user "postgres".
CREATE EXTENSION
CREATE EXTENSION
Loading PostGIS extensions into postgres
CREATE EXTENSION
CREATE EXTENSION
You are now connected to database "postgres" as user "postgres".
CREATE EXTENSION
CREATE EXTENSION

waiting for server to shut down....2024-05-04 04:06:10.091 UTC [48] LOG:  received fast shutdown request
2024-05-04 04:06:10.091 UTC [48] LOG:  aborting any active transactions
2024-05-04 04:06:10.094 UTC [48] LOG:  background worker "logical replication launcher" (PID 54) exited with exit code 1
2024-05-04 04:06:10.096 UTC [49] LOG:  shutting down
2024-05-04 04:06:10.096 UTC [49] LOG:  checkpoint starting: shutdown immediate
2024-05-04 04:06:10.149 UTC [49] LOG:  checkpoint complete: wrote 3816 buffers (23.3%); 0 WAL file(s) added, 0 removed, 2 recycled; write=0.047 s, sync=0.006 s, total=0.054 s; sync files=800, longest=0.001 s, average=0.001 s; distance=31724 kB, estimate=31724 kB; lsn=0/33E2428, redo lsn=0/33E2428
2024-05-04 04:06:10.166 UTC [48] LOG:  database system is shut down
 done
server stopped

PostgreSQL init process complete; ready for start up.

2024-05-04 04:06:10.223 UTC [1] LOG:  starting PostgreSQL 16.2 (Debian 16.2-1.pgdg110+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2024-05-04 04:06:10.224 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2024-05-04 04:06:10.224 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2024-05-04 04:06:10.224 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2024-05-04 04:06:10.228 UTC [72] LOG:  database system was shut down at 2024-05-04 04:06:10 UTC
2024-05-04 04:06:10.235 UTC [1] LOG:  database system is ready to accept connections

What might be the reason? How can I fix that?

mikeizbicki commented 6 months ago

In your docker ps output, under the ports section, I see the line 0.0.0.0:9999->5432/tcp for your postgres database. This tells me that you have routed port 9999 on the lambda server to port 5432 on the postgres database. This is not "wrong" in the sense that it will mess things up, but it's not necessary and tells me that you're having a misunderstanding about ports that is causing this problem.

The web service is contained on the same network as the database, and so you do not need to expose a port on the database server in order to access it. The connection url should be changed from

postgresql://postgres:pass@localhost:9998

to something more like

postgresql://postgres:pass@postgres:5432

That may not be exactly correct for your configuration, but fixing this url is the thing you need to do.