How can I connect using a script from docker-container to a database on the local host (not in docker containers)

Greetings! Please tell me how can I connect to a local database from a docker container? I just start the container with this Dockerfile

COPY ["/workflow/", "/usr/local/"]

RUN python -m venv venv && source venv/bin/activate && \
pip install -r requirements.txt && deactivate

ENV PYTHONPATH=/usr/local/airflow/workflow/
>>> docker ps
CONTAINER ID   IMAGE                        COMMAND                  CREATED        STATUS                    PORTS                      NAMES
6766b298382b   work_5658e3/airflow:latest   "tini -- /entrypoint…"   22 hours ago   Up 51 minutes (healthy)   127.0.0.1:8080->8080/tcp   work_5658e3-webserver-1
c8c58abde823   work_5658e3/airflow:latest   "tini -- /entrypoint…"   22 hours ago   Up 51 minutes                                        work_5658e3-triggerer-1
2ddcad34fad1   work_5658e3/airflow:latest   "tini -- /entrypoint…"   22 hours ago   Up 51 minutes                                        work_5658e3-scheduler-1
748dd8609df1   postgres:12.6                "docker-entrypoint.s…"   10 days ago    Up 51 minutes             127.0.0.1:5432->5432/tcp   work_5658e3-postgres-1

Then in astro dev bash I use the following commands:

After which I get an error. What could be the problem and how to solve it?

It’s also interesting how I can run the astro postgresql container on a different port, not 5432, so as not to disable the service on the local host

Hello @Rett-oo

If you want to connect to your local instance you can use host.docker.internal as explained in the Docker docs.

To run Postgres on a different port, you can use the Astro CLI command:

astro config set postgres.port 5433

There are other properties as well that you can configure as explained in Configure the Astro CLI.

Thanks
Manmeet

2 Likes