|
| 1 | +# Access local database or API |
| 2 | + |
| 3 | +By default, docker-hosted instances uses a [bridge network](https://docs.docker.com/network/bridge/), which does not support access to service on the host via `127.0.0.1` or `localhost`. Here we take PostgreSQL as an example and show you how to access local API or database in different operating systems (OS) for docker-hosted Openblocks. |
| 4 | + |
| 5 | +## Preparation |
| 6 | + |
| 7 | +Assume that a Postgres service is ready on your local host with port `5432`. Confirm that access to the Postgres service functions well. |
| 8 | + |
| 9 | +<figure><img src="../.gitbook/assets/access-local-pre.png" alt=""><figcaption></figcaption></figure> |
| 10 | + |
| 11 | +## How to access local database or API |
| 12 | + |
| 13 | +{% tabs %} |
| 14 | +{% tab title="Linux" %} |
| 15 | +1. Enter `ifconfig docker0` in the terminal, and verify the IP configuration of Docker's virtual bridge (VB). |
| 16 | + |
| 17 | + <figure><img src="../.gitbook/assets/access-local-linux.png" alt=""><figcaption></figcaption></figure> |
| 18 | + |
| 19 | + Having set up Docker, the OS would automatically create `docker0` with the IP address `172.17.0.1` (by default), through which the Docker service communicates with the host. |
| 20 | +2. Enter the same IP address into Openblocks to configure the Postgres data source (`172.17.0.1` in this example, but use the actual IP address displayed on your terminal in real case). |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + <figure><img src="../.gitbook/assets/access-local-linux-2.png" alt=""><figcaption></figcaption></figure> |
| 25 | + |
| 26 | +{% hint style="warning" %} |
| 27 | +On some OS (such as Ubuntu), port access might be blocked by the firewall. To solve that, configure in the following steps: |
| 28 | + |
| 29 | +1. Stop the firewall from blocking access from **docker0** by typing in the terminal:\ |
| 30 | + <mark style="background-color:yellow;">**`iptables -A INPUT -p tcp -i docker0 --dport YOUR_DB_PORT -j ACCEPT`**</mark>\ |
| 31 | + `Note: In this case`Here we use Postgres port <mark style="background-color:yellow;">**`5432`**</mark> for <mark style="background-color:yellow;">**`YOUR_DB_PORT`**</mark>, and you should replace it with your own. |
| 32 | +2. Save the firewall configuration by typing in the terminal:\ |
| 33 | + <mark style="background-color:yellow;">**`iptables-save > /etc/iptables.up.rules`**</mark> |
| 34 | +{% endhint %} |
| 35 | +{% endtab %} |
| 36 | + |
| 37 | +{% tab title="Windows & Mac" %} |
| 38 | +On Windows and masOS, Docker does not create the virtual bridge `docker0`. Then the following DNS is helpful to resolve the host IP: |
| 39 | + |
| 40 | +``` |
| 41 | +host.docker.internal |
| 42 | +``` |
| 43 | + |
| 44 | +In Openblocks, configure the Postgres data source as the following figure shows: |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +<figure><img src="../.gitbook/assets/access-local-win-mac.png" alt=""><figcaption></figcaption></figure> |
| 49 | +{% endtab %} |
| 50 | +{% endtabs %} |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
0 commit comments