{"id":19086691,"url":"https://github.com/jangbl/test-node-with-docker","last_synced_at":"2025-07-19T20:36:56.599Z","repository":{"id":99632292,"uuid":"265055485","full_name":"jangbl/test-node-with-docker","owner":"jangbl","description":"Test a Node server with a Postgres database with docker and docker compose","archived":false,"fork":false,"pushed_at":"2023-05-13T11:04:27.000Z","size":929,"stargazers_count":11,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-03T21:37:57.908Z","etag":null,"topics":["docker","docker-compose","express","expressjs","knex","knexjs","mocha","node","nodejs","postgresql","postgresql-database","postgressql","test","testing"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/jangbl.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":"2020-05-18T20:35:14.000Z","updated_at":"2024-02-02T14:28:05.000Z","dependencies_parsed_at":"2023-05-29T11:00:38.864Z","dependency_job_id":null,"html_url":"https://github.com/jangbl/test-node-with-docker","commit_stats":null,"previous_names":["jangbl/test-node-with-docker"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jangbl%2Ftest-node-with-docker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jangbl%2Ftest-node-with-docker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jangbl%2Ftest-node-with-docker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jangbl%2Ftest-node-with-docker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jangbl","download_url":"https://codeload.github.com/jangbl/test-node-with-docker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223779728,"owners_count":17201287,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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","docker-compose","express","expressjs","knex","knexjs","mocha","node","nodejs","postgresql","postgresql-database","postgressql","test","testing"],"created_at":"2024-11-09T03:00:13.239Z","updated_at":"2024-11-09T03:00:15.605Z","avatar_url":"https://github.com/jangbl.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Test a Node.js server with a database with Docker and docker-compose\n\nThis repository contains sample code on how to a Node.js app with Docker and docker-compose.\n\n\u003ch3 align=\"center\"\u003ePlease help this repo with a ⭐️ if you find it useful! 😁\u003c/h3\u003e\n\nThis repository is part of the [Test Node with Docker](https://www.youtube.com/playlist?list=PL1Nml43UBm6eTkjJtAPfdfjk-x2I_1r-Y) tutorial series provided by [productioncoder.com](https://productioncoder.com/).\n\n[![Test Node with Docker](images/test-node-with-docker.png)](https://www.youtube.com/playlist?list=PL1Nml43UBm6eTkjJtAPfdfjk-x2I_1r-Y)\n\nFor updates, please reach out to [@_jgoebel](https://twitter.com/_jgoebel) on Twitter.\n\n# 2. How to run this application\n\n## 2.1. Running the project locally\n\nYou can run the project locally with your own local [Postgres](https://www.postgresql.org) instance\n\n```\nnpm run dev\n```\n\nMake sure to update the configuration app with your database credentials in the `.config/local.json` file.\n\n## 2.2. Running with docker compose\n\n### 2.2.1 docker-compose up\n\nRunning:\n\n```\ndocker-compose up\n```\n\nwill spin up:\n\n1. express server itself (internally port `8080`) forwarded to port `8080` on the host machine\n2. Postgres database (internally `5432`), forwarded to port `2345`\n\nWe forward to port `2345` on purpose so it does not collide with a local [Postgres](https://www.postgresql.org) instance that might be running on the host machine.\n\nTo run in detached mode, use\n\n```\ndocker-compose up -d\n```\n\n### 2.2.2 clean docker-compose run\n\nIf you would like to do a clean run (i.e. remove all data in already stopped containers), you can run\n\n```\ndocker container prune\n```\n\nto remove all unused containers\n\nAs an alternative you can run\n\n```\ndocker system prune\n```\n\nto clean up all unused resources. Note that this removes all the unused images, volumens etc. on your host machine.\n\n### 2.2.3 docker-compose stop vs docker-compose down\n\nNote that typing `ctrl` + `c` when running `docker-compose up` will only stop the container, but not remove it - which is equivalent of running:\n\n```\ndocker-compose stop\n```\n\nI.e. if you start up the container again, the database will contain the data you have put in before.\n\nPlease refer to the above section on the clean docker-compose section on how to clean up all unused containers.\n\n## 2.2. Running the webserver with docker but accessing db on host machine\n\nIf you want to to only run the webapp in a docker container, you need to adapt your configuration first.\n\nSince the docker container itself is isolated, setting the database host to `localhost` would result in the container trying to connect to itself to connect to the database.\n\nTo connect to the database running on your host machine, you need to set the host name to `host.docker.internal`.\n\nChange the `hostname` field under the `db` object inside the `./config.local.json` like so.\n\n```\n{\n  \"db\": {\n    \"host\": \"host.docker.internal\"\n  }\n}\n```\n\nYou can leave the remaining config as it was.\n\nThis is a special DNS entry within the container that allows the container to connect to your host machine. Please refer to the [documentation](https://docs.docker.com/docker-for-mac/networking/#httphttps-proxy-support) for more details.\n\nOnce you have updated your config, you need to build an image and start a container from it.\n\nExecute the following command from the root directory of the project (where the `Dockerfile` is located)\n\n```\ndocker image build -t productioncoder/test-node-with-docker:local .\n```\n\nAfter that you can start a container\n\n```\ndocker container run -p 8080:8080 productioncoder test-node-with-docker:local\n```\n\nThe `-p` flag publishes the port from the docker container to your host machine.\n\n# 3 Testing\n\n## 3.1. Testing with docker-compose\n\nRunning\n\n```\nnpm test\n```\n\nwill spin up a [Postgres](https://www.postgresql.org) database (internally port `5432`), port-forwarded to port `2345` on the host machine.\n\nIt will then run all the tests with `mocha` as a test runner and then destroy the containers afterwards.\n\nBy testing with a real database, we achieve a high test-fidelity.\n\n## 3.2 Running tests locally\n\nYou can run the tests with your local [Postgres](https://www.postgresql.org) database by running:\n\n```\nnpm run test-local\n```\n\nNote that this add data to your local database and that the tests might fail if unique constraints are violated.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjangbl%2Ftest-node-with-docker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjangbl%2Ftest-node-with-docker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjangbl%2Ftest-node-with-docker/lists"}