The reason is very clear, Postgres service is not running at this stage I mean during the build time.
RUN postgresql://username:password@10.1.0.173:5432/db // error
So this will not work as you are expecting.
This is a common problem with such docker image or custom Docker, So I will suggest using offical image so you will not bother with custom entrypoint script and all you just need to copy your SQL script to /docker-entrypoint-initdb.d
during build time or can mount to this path at runtime.
FROM postgres
COPY my_initdb.sql /docker-entrypoint-initdb.d/
That's it, the offical image will take care of this script and will run the script when the Postgres service is up and running.
If you want To make this working, you must place such command at entrypoint where the Postgres service is running and capable to handle the connection.
Initialization scripts
If you would like to do additional initialization in an image derived
from this one, add one or more *.sql, *.sql.gz, or *.sh scripts under
/docker-entrypoint-initdb.d (creating the directory if necessary).
After the entrypoint calls initdb to create the default postgres user
and database, it will run any *.sql files, run any executable *.sh
scripts, and source any non-executable *.sh scripts found in that
directory to do further initialization before starting the service.