|
| 1 | +# Use local db instead of dockerized db |
| 2 | + |
| 3 | +Set up `osmox-api` docker container to use local postgres database |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +- Override the existing Docker Compose file to exclude creation of the PostgreSQL container and its associated volume |
| 8 | +- Utilize local PostgreSQL database as the data source for a Dockerized API |
| 9 | + |
| 10 | +## Steps |
| 11 | + |
| 12 | +1. Clone the repository: |
| 13 | + |
| 14 | + ```bash |
| 15 | + git clone https://github.com/OsmosysSoftware/osmo-x.git |
| 16 | + cd osmo-x/apps/api/docs/use-local-db-instead-of-dockerized-db/ |
| 17 | + ``` |
| 18 | + |
| 19 | +2. Create `docker-compose.override.yml` where `docker-compose.yml` exists and copy the contents of `docker-compose-override-script.txt` in it |
| 20 | + |
| 21 | + ```bash |
| 22 | + # copy script from .txt file and create override script in osmo-x/apps/api |
| 23 | + cp docker-compose-override-script.txt ../../docker-compose.override.yml |
| 24 | + ``` |
| 25 | + |
| 26 | +3. Go to `osmo-x/apps/api` and create `.env` file (if not already created) |
| 27 | + |
| 28 | + ```bash |
| 29 | + # In osmo-x/apps/api/docs/use-local-db-instead-of-dockerized-db/ |
| 30 | + cd ../.. |
| 31 | + # In osmo-x/apps/api |
| 32 | + cp .env.example .env |
| 33 | + ``` |
| 34 | + |
| 35 | +4. Update the `.env` variables to use postgres database on your local machine |
| 36 | + |
| 37 | + 1. You need to set `DB_HOST` to your machine's IP address: |
| 38 | + - This is the IP address that other devices on your local network (including Docker containers) can use to reach your machine. |
| 39 | + - You can simply set `DB_HOST=host.docker.internal` on most setups. |
| 40 | + - If that does not work for your Docker engine, fall back to using the machine’s IP address: |
| 41 | + 2. How to find your host IP: |
| 42 | + - **Windows:** Open Command Prompt/PowerShell, type `ipconfig`, look for IPv4 Address. |
| 43 | + - **macOS/Linux:** Open Terminal, type `ip addr`, look for your active network interface (e.g., en0, eth0, docker0) IP address. |
| 44 | + 3. Update the `.env` variables: |
| 45 | + |
| 46 | + ```env |
| 47 | + # Database configuration |
| 48 | + DB_TYPE=postgres |
| 49 | + DB_HOST=<IP-ADDRESS-USED-TO-REACH-YOUR-HOST-MACHINE> # Value "host.docker.internal" should work on most setups |
| 50 | + DB_PORT=<POSTGRES-PORT-FOR-LOCAL-MACHINE> # Set the port used by postgres of your local machine |
| 51 | + DB_USERNAME=your-username |
| 52 | + DB_PASSWORD=your-password |
| 53 | + DB_NAME=your-database |
| 54 | + DB_DOCKER_PORT=5433 # IGNORE. Will not be used for current setup |
| 55 | + ``` |
| 56 | + |
| 57 | +5. Build and run the docker compose command |
| 58 | + |
| 59 | + ```bash |
| 60 | + # In osmo-x/apps/api |
| 61 | + docker compose up --build |
| 62 | + ``` |
| 63 | + |
| 64 | +6. To dockerize the application normally, remove the override file and build the containers again |
| 65 | + |
| 66 | + ```bash |
| 67 | + docker compose down -v |
| 68 | + rm docker-compose.override.yml |
| 69 | + # Update the .env variables accordingly |
| 70 | + docker compose up --build |
| 71 | + ``` |
| 72 | + |
| 73 | + For more details, go through the [osmo-x production setup using docker](../production-setup.md#using-docker) |
| 74 | + |
| 75 | +## References |
| 76 | + |
| 77 | +- https://docs.docker.com/compose/how-tos/multiple-compose-files/merge/ |
| 78 | +- https://docs.docker.com/reference/compose-file/merge/ |
0 commit comments