{"id":15725857,"url":"https://github.com/ndrean/pg-doc","last_synced_at":"2026-04-14T14:32:23.171Z","repository":{"id":124426108,"uuid":"367634060","full_name":"ndrean/pg-doc","owner":"ndrean","description":"Reverse proxying two web-apps ( Ruby \u0026 Node) containers interacting with a dockerized Postgres database","archived":false,"fork":false,"pushed_at":"2021-05-23T06:03:25.000Z","size":1040,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-31T01:28:43.899Z","etag":null,"topics":["docker","koa","nginx","nodejs","postgres","reverse-proxy","ruby","sequel","sequelize","sinatra","static-server"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ndrean.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-15T13:06:55.000Z","updated_at":"2021-05-23T06:03:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"7aac4785-da92-4161-9a36-e344f1867c05","html_url":"https://github.com/ndrean/pg-doc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ndrean/pg-doc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ndrean%2Fpg-doc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ndrean%2Fpg-doc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ndrean%2Fpg-doc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ndrean%2Fpg-doc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ndrean","download_url":"https://codeload.github.com/ndrean/pg-doc/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ndrean%2Fpg-doc/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31801312,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-14T11:13:53.975Z","status":"ssl_error","status_checked_at":"2026-04-14T11:13:53.299Z","response_time":153,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","koa","nginx","nodejs","postgres","reverse-proxy","ruby","sequel","sequelize","sinatra","static-server"],"created_at":"2024-10-03T22:24:36.886Z","updated_at":"2026-04-14T14:32:23.086Z","avatar_url":"https://github.com/ndrean.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Reverse-proxying dockerized apps with Nginx connected to a PostgreSQL database\n\nWe have two dockerized web-apps, based on Node.js and on Ruby/Sinatra, that save the querried URL into a Postgres database.\n\n![drawing](./webserver/images/Docker-Nginx-PG-Node-Ruby.svg)\n\nIt simply displays(at port 9000 for Ruby and port 3000 for Node) the connection to the database and saves the request (ip and container id) to the database.\n\n![result](./webserver/images/Screenshot.png)\n\nThe Postgres container has been opened (port 5000 here) for testing purposes. We use the adapter `pg` and the ORM `'sequel` on top of it for Ruby, and the ORM `sequelize` for Node.js.\n\n## Start up\n\nTwo configurations:\n\n- launch the Postgres container (`docker-compose up db`) and run locally the 2 apps , `/sinatra/rackup` and `/koa nodemon index.js`. You must set `POSTGRES_HOST=localhost` and `POSTGRES_PORT=5000`. This means that the PG container is opened at port 5000 and the two apps should connect to the \"localhost\" server. The Ruby app is available at port:9292, and the node app at port:3000.\n\n- run everything in containers. Then set `POSTGRES_HOST=db` and `POSTGRES_PORT=5432`, and `docker-compose up --build`. The Ruby app is available at port:9000, and the node app at port:4000.\n\n- remove the volume `docker volume rm db-vol`\n- remove the image `docker image rm db-img` if you want to change the name of the database\n- run `docker-compose up --build`.\n- the app is available at `port:9000` and `port:3000`\n- the Postgres db is opened at `port 5000` for testing purposes, e.g. using \u003chttps://dbeaver.io\u003e.\n\n## Env variables between Postgres, Sinatra\n\n- PG: Postgres uses environment variables: `POSTGRES_DB`, `POSTGRES_USER`, `POSTGRES_PASSWORD`. They are passed to the PG container via the `.env` file.\n\n- SINATRA: we use the `dotenv` gem to read the env variables.\n  It should be loaded as early as possible in the **config.ru** file with `require 'dotenv'; Dotenv.load('../.env')`\n\n\u003e We can access the variables with `ENV['PORT']` or `ENV.fetch('PORT') { 9292 }`,\n\u003e the latter raises an exception if not present and also yield a default value.\n\n## Connection between the app container and the db container\n\nWhen using Docker, we pass the env var `POSTGRES_HOST=db` where **db** is the name of the postgres service (named in **docker-compose.yml**).\n\n\u003e when we don't use containers (i.e. we just do `rackup`), then the host is \"localhost\"\n\nIndeed, when we use containers, if we use **host: localhost** for the ORM adapter, this would mean that we are trying to connect to localhost inside of the app container, and there is nothing there. In fact, what we want is to connect to the the postgres container (at port 5432), so we specify `host: POSTGRES_HOST ( = \"db\" )` where the Postgres service is named **db** here.\n\n## DB adapters\n\n- ruby: the database can be accessed from the ruby app by the ORM `sequel`.\n  It allows to query the database **without a model**.\n\n- postgres: in the docker-compose file, we mapped 5000:5432 to make the db container accessible,\n  for example via DBeaver.\n\n## Postgres init\n\nWe create a table and populate it with a **sql** request in an initiazation script.\nWe add an `*.sql` script under `/docker-entrypoint-initdb.d` The database\n\n## Node.js \u0026 Koa container\n\n\u003chttps://semaphoreci.com/community/tutorials/dockerizing-a-node-js-web-application\u003e\n\n\u003chttps://jimfrenette.com/docker/node-js-koa-container/\u003e\n\n\u003e to get IP address, \u003chttps://nodejs.org/api/http.html#http_request_socket\u003e\n\n## PM2\n\n\u003chttps://pm2.keymetrics.io/docs/usage/docker-pm2-nodejs/\u003e\n\n## Ruby extras:\n\n- code reloader: we used the extension `require \"sinatra/reloader\"` (`bundle add sinatra-contrib`)\n\n- `pg_db = URI.parse(\"postgres://postgres:psql@db/5432\")` and the methods `host`, `user`, `password`, `scheme` are available.\n\n- Ruby time class: \u003chttps://www.rubyguides.com/2015/12/ruby-time/\u003e and \u003chttp://strftime.net/\u003e\n\n`bundle exec irb` and then `Bundler.require` to attach all gems.\n\n## Postgres init:\n\n```sql\nCREATE TABLE IF NOT EXISTS public.persons (\n   id int PRIMARY KEY,\n   firstName varchar,\n   lastName varchar\n   );\n\nINSERT INTO public.persons (id, firstname, lastname) VALUES (1, 'Luke', 'Skywalker'), (2, 'Leia', 'Organa'), (3, 'Han', 'Solo');\n```\n\n## DBeaver\n\nSince the postgres container has been opened at port 5000, you can connect from your localhost to a selected database; you need to enter the database name and user / password.\n\n![connect to db](./webserver/images/connect-to-db.png)\n![select db](./webserver/images/select-db.png)\n![connect to pg](./webserver/images/connect-query.png)\n\n```sh\ndocker run -p 5000:5423 --name pg-cont --rm -d -v pg-vol:/var/lib/postgresql/data postgres:13.2-alpine\n\ndocker exec -it pg-cont psql -U postgres\n```\n\n`g_ctl -D /var/lib/postgresql/data -l logfile start`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fndrean%2Fpg-doc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fndrean%2Fpg-doc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fndrean%2Fpg-doc/lists"}