#! /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"}
@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
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?
@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..
Thanks @iho. We'll update the docs asap too :)
Please look at this answer https://stackoverflow.com/a/26090569
@coco98
how to link migrations dir from Docker container to have them outside too?
@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
@rikinsk Can you update the docs per: https://stackoverflow.com/questions/17770902/forward-host-port-to-docker-container/26090569#26090569
Especially for this page I think: https://docs.hasura.io/1.0/graphql/manual/deployment/docker/index.html
Close when docs issue is closed.
hasura/graphql-engine-docs#32 is closed
@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.
How can I make this using docker-compose.yml?
@shahidhk docker-compose is my question too.
@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
@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.
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:
3.7).5432.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.
Hi Team,
How can I connect the remote host machine database from Docker container
I have a spring boot container
Most helpful comment
@coco98 thank you for fast response! I tried 172.17.0.1, but it does not work too.
Solved with
--net hostparameter to docker command..