{"id":16175964,"url":"https://github.com/sebastiandg7/multi-docker-compose-networking","last_synced_at":"2026-05-07T04:44:20.516Z","repository":{"id":193431306,"uuid":"688782264","full_name":"sebastiandg7/multi-docker-compose-networking","owner":"sebastiandg7","description":"Networking between multiple docker-compose configurations","archived":false,"fork":false,"pushed_at":"2023-09-08T05:36:23.000Z","size":12,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-02T00:41:55.180Z","etag":null,"topics":["docker","docker-compose","docker-network","docker-networking"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sebastiandg7.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-09-08T05:13:18.000Z","updated_at":"2023-09-08T05:23:19.000Z","dependencies_parsed_at":"2023-09-08T06:42:40.895Z","dependency_job_id":null,"html_url":"https://github.com/sebastiandg7/multi-docker-compose-networking","commit_stats":null,"previous_names":["sebastiandg7/multi-docker-compose-networking"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastiandg7%2Fmulti-docker-compose-networking","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastiandg7%2Fmulti-docker-compose-networking/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastiandg7%2Fmulti-docker-compose-networking/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastiandg7%2Fmulti-docker-compose-networking/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sebastiandg7","download_url":"https://codeload.github.com/sebastiandg7/multi-docker-compose-networking/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247640475,"owners_count":20971558,"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","docker-network","docker-networking"],"created_at":"2024-10-10T04:46:56.388Z","updated_at":"2026-05-07T04:44:15.496Z","avatar_url":"https://github.com/sebastiandg7.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Multi Docker Compose Networking\n\nThis repo demonstrates how services running in different docker-compose configs, attached to the same docker network, can communicate with each other while exposing required ports to the host machine.\n\nThis configuration can serve as an example to run backend services in a docker-compose file \u0026 SSR web applications in a separate docker-compose file.\n\n## Prerequisites\n\n1. Docker \u0026 Docker Compose.\n\n2. Create the `multi-compose-net` docker network (`bridge` mode)\n\n```bash\ndocker network create multi-compose-net\n``` \n\n## Running the services\n\n\n| - | Server | Client|\n| - | - | - |\n| **Build** | `docker compose -f server/docker-compose.yml build` | `docker compose -f client/docker-compose.yml build` |\n| **Start** | `docker compose -f server/docker-compose.yml up` | `docker compose -f client/docker-compose.yml up` |\n\nYou should get an output similar to:\n\nServer\n```bash\n➜ docker compose -f server/docker-compose.yml up\n[+] Running 1/0\n ✔ Container demo-backend-server-api-1  Created                            0.0s \nAttaching to demo-backend-server-api-1\ndemo-backend-server-api-1  | [ ready ] on http://0.0.0.0:8080\ndemo-backend-server-api-1  | [ request ] GET /FjsY9bH3ad\ndemo-backend-server-api-1  | [ response ] 200 { msg: 'Hello World FjsY9bH3ad' }\ndemo-backend-server-api-1  | [ request ] GET /mr8mU2LDEI\ndemo-backend-server-api-1  | [ response ] 200 { msg: 'Hello World mr8mU2LDEI' }\n```\n\nClient\n```bash\n➜ docker compose -f client/docker-compose.yml up\n[+] Running 1/0\n ✔ Container demo-frontend-client-1  Created                                               0.0s \nAttaching to demo-frontend-client-1\ndemo-frontend-client-1  | [ client ] Sending GET to http://demo-server-api:8080/FjsY9bH3ad ...\ndemo-frontend-client-1  | [ client ] Response - FjsY9bH3ad: { msg: 'Hello World FjsY9bH3ad' }\ndemo-frontend-client-1  | [ client ] Waiting 10s for next request...\ndemo-frontend-client-1  | [ client ] Sending GET to http://demo-server-api:8080/mr8mU2LDEI ...\ndemo-frontend-client-1  | [ client ] Response - mr8mU2LDEI: { msg: 'Hello World mr8mU2LDEI' }\ndemo-frontend-client-1  | [ client ] Waiting 10s for next request...\n```\n\n## How it works?\n\n\u003cdetails\u003e\n\u003csummary\u003eClick to expand\u003c/summary\u003e\n\nBoth server and client `docker-compose.yml` add their services to the `multi-compose-net` network.\n\n```yml\nversion: '3.8'\n\nservices:\n#   blablabla:\n\nnetworks:\ndefault:\n    name: multi-compose-net\n    external: true\n```\n\n(optional) The server `docker-compose.yml` sets an explicit hostname for the api service.\n\n```diff\nversion: '3.8'\n\nservices:\nserver-api:\n+   hostname: demo-server-api\n    build:\n    context: ./\n    dockerfile: Dockerfile\n    ports:\n    - 8080:8080\n    environment:\n    - PORT=8080\n    - HOST=0.0.0.0\n    - NODE_ENV=production\n    command: ['node', 'server.mjs']\n\nnetworks:\n    # blabla\n```\n\nThe client `docker-compose.yml`, via an environment variable, sets the exact same hostname in the target URL to send the requests to.\n\n```diff\nversion: '3.8'\n\nservices:\nclient:\n    build:\n    context: ./\n    dockerfile: Dockerfile\n    environment:\n+     - API_BASE_URL=http://demo-server-api:8080\n    - REQUEST_INTERVAL=10000\n    command: ['node', 'client.mjs']\n\nnetworks:\n    # blabla\n```\n\nWhen the services are running, you can check all of them are attached to the same network.\n\n```bash\ndocker network inspect multi-compose-net\n```\n\nOutput:\n```diff\n[\n    {\n        \"Name\": \"multi-compose-net\",\n        \"Id\": \"b7906ffe264c6fda784cdd161b03c8c0b49dad1ac2c79c3305c0b9794febd78a\",\n        \"Created\": \"2023-09-08T03:31:38.911731353Z\",\n        \"Scope\": \"local\",\n        \"Driver\": \"bridge\",\n        \"EnableIPv6\": false,\n        \"IPAM\": {\n            \"Driver\": \"default\",\n            \"Options\": {},\n            \"Config\": [\n                {\n                    \"Subnet\": \"172.20.0.0/16\",\n                    \"Gateway\": \"172.20.0.1\"\n                }\n            ]\n        },\n        \"Internal\": false,\n        \"Attachable\": false,\n        \"Ingress\": false,\n        \"ConfigFrom\": {\n            \"Network\": \"\"\n        },\n        \"ConfigOnly\": false,\n+       \"Containers\": {\n+           \"7ff1878e7596ddba148dee2a495cda5964371b9e70344baffad875d7e205258e\": {\n+               \"Name\": \"demo-frontend-client-1\",\n+               \"EndpointID\": \"01f0e5fc3c82c696ef38188e10bf08236504a4ca5e5c909836f8a62d6fdf5038\",\n+               \"MacAddress\": \"02:42:ac:14:00:03\",\n+               \"IPv4Address\": \"172.20.0.3/16\",\n+               \"IPv6Address\": \"\"\n+           },\n+           \"8d0090974c1a497aa50618cb0b00fa1bd198d646ef9ec6b2cf06bb7bea241aa1\": {\n+               \"Name\": \"demo-backend-server-api-1\",\n+               \"EndpointID\": \"cff9ed426db97df95f7eedcf64076f849676d3d1fed30c404b80d3cb84aa38fc\",\n+               \"MacAddress\": \"02:42:ac:14:00:02\",\n+               \"IPv4Address\": \"172.20.0.2/16\",\n+               \"IPv6Address\": \"\"\n+           }\n+       },\n        \"Options\": {},\n        \"Labels\": {}\n    }\n]\n```\n\nThat is what makes the communication possible.\n\u003c/details\u003e\n\n## What about the web browser side of the SSR web app?\n\nIf you happen to be sending requests to the API from the browser side, the host machine won't be able to solve the hostname in `http://demo-server-api:8080`.\n\nTo solve this problem, your SSR app can be aware of two URLs for the same API service. One for the SSR runtime and other one for the web browser runtime.\n\n```diff\nversion: '3.8'\n\nservices:\nclient:\n    build:\n    context: ./\n    dockerfile: Dockerfile\n    environment:\n+     - API_BASE_URL_SSR=http://demo-server-api:8080\n+     - API_BASE_URL_BROWSER=http://localhost:8080\n    - REQUEST_INTERVAL=10000\n    command: ['node', 'client.mjs']\n\nnetworks:\n    # blabla\n```\n\nFor this to work, make sure the `server-api` configuration uses the [ports](https://docs.docker.com/compose/compose-file/compose-file-v3/#ports) options instead of the [expose](https://docs.docker.com/compose/compose-file/compose-file-v3/#expose) one, this will expose ports to the docker network and also map them to the host machine.\n\nWith this config and the proper application code changes, requests sent from the SSR container will resolve to the right IP inside the docker network and requests sent from the web browser (to `localhost`) will also reach the desired `server-api` container.\n\n## License\n\nThis project is licensed under the **MIT License**.\n\nSee [**LICENSE**](LICENSE) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebastiandg7%2Fmulti-docker-compose-networking","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsebastiandg7%2Fmulti-docker-compose-networking","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebastiandg7%2Fmulti-docker-compose-networking/lists"}