Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect to database outside Docker #140

Closed
iho opened this issue Jul 16, 2018 · 20 comments
Closed

Connect to database outside Docker #140

iho opened this issue Jul 16, 2018 · 20 comments
Labels
c/docs Related to docs

Comments

@iho
Copy link

iho commented Jul 16, 2018

#! /bin/bash
docker run -p 8080:8080 \
       hasura/graphql-engine:v1.0.0-alpha07 \
       graphql-engine \
       --database-url postgres://hasura_user:qwerty@localhost:5432/hasura_database \
       serve --enable-console


psql postgres://hasura_user:qwerty@localhost:5432/hasura_database
psql (9.6.6)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

hasura_database=> \q


./docker-run.sh 
Postgres connection info:
    Host: localhost
    Port: 5432
    User: hasura_user
    Database: hasura_database
{"internal":"could not connect to server: Connection refused\n\tIs the server running on host \"localhost\" (127.0.0.1) and accepting\n\tTCP/IP connections on port 5432?\n","path":"$","error":"connection error","code":"postgres-error"}

@coco98
Copy link
Contributor

coco98 commented Jul 16, 2018

@iho Because hasura is connecting from inside the docker container, you'll have to change HOST_IP in the command below:

docker run -p 8080:8080 \
       hasura/graphql-engine:v1.0.0-alpha07 \
       graphql-engine \
       --database-url postgres://hasura_user:qwerty@HOST_IP:5432/hasura_database \
       serve --enable-console

@coco98
Copy link
Contributor

coco98 commented Jul 16, 2018

Are you on a mac/linux/windows machine?
On mac with the latest docker for desktop: host.docker.internal will work.

Can you paste your ip addr or ifconfig output? One of your local IPs will work!
For example, if your host IP is 192.168.9.13 then set HOST_IP above to that and try it out?

@iho
Copy link
Author

iho commented Jul 16, 2018

@coco98 thank you for fast response! I tried 172.17.0.1, but it does not work too.

6: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:ca:7b:38:43 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:caff:fe7b:3843/64 scope link 
       valid_lft forever preferred_lft forever

Solved with --net host parameter to docker command..

@coco98
Copy link
Contributor

coco98 commented Jul 16, 2018

Thanks @iho. We'll update the docs asap too :)

@iho
Copy link
Author

iho commented Jul 16, 2018

Please look at this answer https://stackoverflow.com/a/26090569

@iho
Copy link
Author

iho commented Jul 16, 2018

@coco98
how to link migrations dir from Docker container to have them outside too?

@shahidhk
Copy link
Member

shahidhk commented Jul 16, 2018

@iho migration files in the migrations/ directory are only created when using the console through Hasura CLI. Here are the docs to get started with migrations: https://docs.hasura.io/1.0/graphql/manual/migrations/index.html

@coco98
Copy link
Contributor

coco98 commented Jul 16, 2018

@shahidhk
Copy link
Member

Close when docs issue is closed.

@shahidhk
Copy link
Member

shahidhk commented Jul 19, 2018

hasura/graphql-engine-docs#32 is closed

@shahidhk
Copy link
Member

@iho We have updated docs with --net=host flag (https://docs.hasura.io/1.0/graphql/manual/deployment/docker/index.html). Thanks for helping out.

@andresespinosapc
Copy link

How can I make this using docker-compose.yml?

@mnlbox
Copy link
Contributor

mnlbox commented Feb 23, 2019

@shahidhk docker-compose is my question too.

@dsmurrell
Copy link

@andresespinosapc, @mnlbox: I believe I have found a way.

Edit /etc/hasura/docker-compose.yml to include

ports:
- "5432:5432"

in the postgres: section

then restart the containers by doing

docker-compose down
docker-compose up -d

from the /etc/hasura folder

If it worked, you should see a new port being forwarded when you run
docker container ls

@jjangga0214
Copy link
Contributor

jjangga0214 commented Aug 5, 2019

@andresespinosapc @mnlbox @dsmurrell @iho
Here is a practical docker-compose example.

version: '3.7'

services:
  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_DB: '${DB_DATABASE}'
      POSTGRES_USER: '${DB_USERNAME}'
      POSTGRES_PASSWORD: '${DB_PASSWORD}'
    # "ports" setting is not required to let hasura connent to db. 
    # Whether to expose it or not is optional. 
    # But it's required if your application needs direct access(not through hasura) to db.
    ports: 
      - target: 5432
        published: ${DB_PORT}
        protocol: tcp
        mode: host
    volumes:
      # Uncomment the below if you want to execute .sh or .sql files on bootstrapping. 
      # Visit official postgress docker page for more detail.
      # - ./data:/docker-entrypoint-initdb.d 
      - db_data:/var/lib/postgresql/data
  graphql-engine:
    image: hasura/graphql-engine
    ports:
      - target: 8080
        published: ${HASURA_PORT}
        protocol: tcp
        mode: host
    depends_on:
      - 'db'
    restart: always
    environment:
      HASURA_GRAPHQL_DATABASE_URL: 'postgres://${DB_USERNAME}:${DB_PASSWORD}@db:${DB_PORT}/${DB_DATABASE}'
      HASURA_GRAPHQL_ENABLE_CONSOLE: 'true' # set to "false" to disable console
      HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log
      ## uncomment next line to set an admin secret
      # HASURA_GRAPHQL_ADMIN_SECRET: myadminsecretkey
volumes:
  db_data:

Where ${...} are host's enviornment variables.

And please note that there is "db" after "@", which is not "localhost", in
HASURA_GRAPHQL_DATABASE_URL: 'postgres://${DB_USERNAME}:${DB_PASSWORD}@db:${DB_PORT}/${DB_DATABASE}'. That's because, as @coco98 mentioned, hasura container would interpret 'localhost' as not host's IP, but its IP (container's localhost).

As "db" is a service name of postgres in this docker compose file, HASURA_GRAPHQL_DATABASE_URL should change along with the change of the service name of postgres.

@jordanmkoncz
Copy link

jordanmkoncz commented Aug 15, 2019

Here's another example of a working docker-compose.yaml that's a bit simpler than the one provided by @jjangga0214. It's based on the 'official' one located at https://github.com/hasura/graphql-engine/blob/master/install-manifests/docker-compose/docker-compose.yaml.

The changes are:

  • It's using the latest compose file format (3.7).
  • It specifies the Postgres version explicitly.
  • It makes the Postgres database accessible from outside the Docker container on port 5432.
  • It specifies container names to make it easier to run commands. For example, you could run docker exec -i postgres psql -U postgres postgres < schema.sql to run an SQL file against the Postgres database.

docker-compose.yaml:

version: '3.7'
services:
  postgres:
    image: "postgres:11.5"
    container_name: "postgres"
    ports:
    - "5432:5432"
    restart: always
    volumes:
    - db_data:/var/lib/postgresql/data
  graphql-engine:
    image: hasura/graphql-engine:v1.0.0-beta.4
    container_name: "hasura"
    ports:
    - "8080:8080"
    depends_on:
    - "postgres"
    restart: always
    environment:
      HASURA_GRAPHQL_DATABASE_URL: postgres://postgres:@postgres:5432/postgres
      HASURA_GRAPHQL_ENABLE_CONSOLE: "true"
      HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log
      ## uncomment next line to set an admin secret
      # HASURA_GRAPHQL_ADMIN_SECRET: myadminsecretkey
volumes:
  db_data:

Once you've added this docker-compose.yaml file, just run docker-compose up -d to run the Docker container.

@cjyothi
Copy link

cjyothi commented Jul 9, 2020

Hi Team,
How can I connect the remote host machine database from Docker container
I have a spring boot container

@ukanwat
Copy link

ukanwat commented Aug 25, 2021

@jjangga0214
what should I put in ${HASURA_PORT} & ${DB_PORT}?

@jjangga0214
Copy link
Contributor

jjangga0214 commented Sep 5, 2021

@ukanwat They should be numbers. like 8080 or so.

For example,

  graphql-engine:
    image: hasura/graphql-engine
    ports:
      - target: 8080
        published: 9090 # This is what you should configure
        protocol: tcp
        mode: host

Then just execute docker-compose up.

If you want to use your own env var, you can define something like ${HASURA_PORT}. For example,

  graphql-engine:
    image: hasura/graphql-engine
    ports:
      - target: 8080
        published: ${HASURA_PORT} # Not hard-coded this time
        protocol: tcp
        mode: host

and you can execute HASURA_PORT=9090 docker-compose up.

@jjangga0214
Copy link
Contributor

jjangga0214 commented Sep 6, 2021

@rikinsk-zz How did this happen?

Screenshot from 2021-09-06 16-06-27

@cjyothi is neither an org member nor collaborator of the repo, but he could unassign you?!

hgiasac pushed a commit to hgiasac/graphql-engine that referenced this issue Dec 19, 2023
GitOrigin-RevId: 8f3e9aa8a35c7610b4f2c6b98c42bd0524534803
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c/docs Related to docs
Projects
None yet
Development

No branches or pull requests