til: docker_docker-for-mac-container-to-postgresql-on-host.md
This data as json
| path | topic | title | url | body | html | shot | created | created_utc | updated | updated_utc | shot_hash | slug |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| docker_docker-for-mac-container-to-postgresql-on-host.md | docker | Allowing a container in Docker Desktop for Mac to talk to a PostgreSQL server on the host machine | https://github.com/simonw/til/blob/main/docker/docker-for-mac-container-to-postgresql-on-host.md | I like using [Postgres.app](https://postgresapp.com/) to run PostgreSQL on my macOS laptop. I use it for a bunch of different projects. When I deploy applications to Fly.io I build them as Docker containers and inject the Fly PostgreSQL database details as a `DATABASE_URL` environment variable. In order to test those containers on my laptop, I needed to figure out a way to set a `DATABASE_URL` that would point to the PostgreSQL I have running on my own laptop - so that I didn't need to spin up another PostgreSQL Docker container just for testing purposes. ## host.docker.internal The first thing to know is that Docker for Desktop sets `host.docker.internal` as a magic hostname inside the container that refers back to the IP address of the host machine. So ideally something like this should work: docker run --env DATABASE_URL="postgres://docker:docker-password@host.docker.internal:5432/pillarpointstewards" \ -p 8080:8000 pillarpointstewards I'm using `-p 8080:8000` here to set port 8080 on my laptop to forward to the Django application server running on port 8000 inside the container. ## Creating the account and granting permissions To create that PostgreSQL account with username `docker` and password `docker-password` (but pick a better password than that) I used Postico to open a connection to my `postgres` database and ran the following: create role docker login password 'docker-password'; Then I connected to my application database (in this case `pillarpointstewards`) and ran the following to grant permissions to that user: ```sql GRANT ALL ON ALL TABLES IN SCHEMA "public" TO docker; ``` Having done this, the container run with the above `DATABASE_URL` environment variable was able to both connect to the server and run Django migrations too. | <p>I like using <a href="https://postgresapp.com/" rel="nofollow">Postgres.app</a> to run PostgreSQL on my macOS laptop. I use it for a bunch of different projects.</p> <p>When I deploy applications to Fly.io I build them as Docker containers and inject the Fly PostgreSQL database details as a <code>DATABASE_URL</code> environment variable.</p> <p>In order to test those containers on my laptop, I needed to figure out a way to set a <code>DATABASE_URL</code> that would point to the PostgreSQL I have running on my own laptop - so that I didn't need to spin up another PostgreSQL Docker container just for testing purposes.</p> <h2> <a id="user-content-hostdockerinternal" class="anchor" href="#hostdockerinternal" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>host.docker.internal</h2> <p>The first thing to know is that Docker for Desktop sets <code>host.docker.internal</code> as a magic hostname inside the container that refers back to the IP address of the host machine.</p> <p>So ideally something like this should work:</p> <pre><code>docker run --env DATABASE_URL="postgres://docker:docker-password@host.docker.internal:5432/pillarpointstewards" \ -p 8080:8000 pillarpointstewards </code></pre> <p>I'm using <code>-p 8080:8000</code> here to set port 8080 on my laptop to forward to the Django application server running on port 8000 inside the container.</p> <h2> <a id="user-content-creating-the-account-and-granting-permissions" class="anchor" href="#creating-the-account-and-granting-permissions" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Creating the account and granting permissions</h2> <p>To create that PostgreSQL account with username <code>docker</code> and password <code>docker-password</code> (but pick a better password than that) I used Postico to open a connection to my <code>postgres</code> database and ran the following:</p> <pre><code>create role docker login password 'docker-password'; </code></pre> <p>Then I connected to my application database (in this case <code>pillarpointstewards</code>) and ran the following to grant permissions to that user:</p> <div class="highlight highlight-source-sql"><pre><span class="pl-k">GRANT</span> ALL <span class="pl-k">ON</span> ALL TABLES <span class="pl-k">IN</span> SCHEMA <span class="pl-s"><span class="pl-pds">"</span>public<span class="pl-pds">"</span></span> TO docker;</pre></div> <p>Having done this, the container run with the above <code>DATABASE_URL</code> environment variable was able to both connect to the server and run Django migrations too.</p> | <Binary: 72,938 bytes> | 2022-03-31T22:48:17-07:00 | 2022-04-01T05:48:17+00:00 | 2022-03-31T22:48:17-07:00 | 2022-04-01T05:48:17+00:00 | 9ea725043c83df0d505051bd25506766 | docker-for-mac-container-to-postgresql-on-host |