{"id":23685875,"url":"https://github.com/trandung2k1/docker-swarm-nodejs","last_synced_at":"2026-04-12T09:47:52.949Z","repository":{"id":242346262,"uuid":"809261329","full_name":"trandung2k1/docker-swarm-nodejs","owner":"trandung2k1","description":null,"archived":false,"fork":false,"pushed_at":"2024-06-02T11:10:55.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-29T21:15:56.337Z","etag":null,"topics":["docker","docker-compose","docker-swarm","nodejs"],"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/trandung2k1.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":"2024-06-02T07:19:55.000Z","updated_at":"2024-06-06T09:30:12.000Z","dependencies_parsed_at":"2024-06-02T12:45:36.628Z","dependency_job_id":null,"html_url":"https://github.com/trandung2k1/docker-swarm-nodejs","commit_stats":null,"previous_names":["trandung2k1/docker-swarm-nodejs"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trandung2k1%2Fdocker-swarm-nodejs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trandung2k1%2Fdocker-swarm-nodejs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trandung2k1%2Fdocker-swarm-nodejs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trandung2k1%2Fdocker-swarm-nodejs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trandung2k1","download_url":"https://codeload.github.com/trandung2k1/docker-swarm-nodejs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239742363,"owners_count":19689308,"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-swarm","nodejs"],"created_at":"2024-12-29T21:16:02.266Z","updated_at":"2026-01-08T05:30:17.864Z","avatar_url":"https://github.com/trandung2k1.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Docker-swarm-nodejs\nA sample of Docker Swarm with nodejs\n\nCreate a new folder called api.\n```bash\n$ cd api\n```\n\nCreate a new file index.js and paste the code below : \n\n```node\nconst http = require('http');\nconst os = require('os');\n\nhttp.createServer(function (req, res) {\n    res.writeHead(200, {'Content-Type': 'text/html'});\n    res.end(`\u003ch1\u003eI'm ${os.hostname()}\u003c/h1\u003e`);\n}).listen(8080);\n```\n\n#### Create Dockerfile\nNow we need to dockerize the app, we’ll create a file named Dockerfile with the following code:\n\n```docker\nFROM node\nRUN mkdir -p /usr/src/app\nCOPY index.js /usr/src/app\nEXPOSE 8080\nCMD [ \"node\", \"/usr/src/app/index\" ]\n```\n#### Build the Docker image\nTo build a docker image of our newly awesome Node.js app from the docker file instructions; we’ll write the docker build command line, where our Dockerfile file is located:\n\n```bash\n$ docker build -t api .\n```\n\n#### Let's run\nNow we have a docker image of our simple (and awesome) Node.js app, and we can create containers from that image.\n\nLet's say we want 3 running containers of that image and all behind a load balancing server.\nFor our HTTP server we’ll use HAProxy that will listen to port 3000\nLet’s create a docker-compose.yml file:\n\n```docker\nversion: '3'\n\nservices:\n    api:\n        image: api\n        ports:\n            - 8080\n        environment:\n            - SERVICE_PORTS=8080\n        deploy:\n            replicas: 3\n            update_config:\n                parallelism: 5\n                delay: 10s\n            restart_policy:\n                condition: on-failure\n                max_attempts: 3\n                window: 120s\n        networks:\n            - trandung\n\n    proxy:\n        image: dockercloud/haproxy\n        depends_on:\n            - api\n        environment:\n            - BALANCE=leastconn\n        volumes:\n            - /var/run/docker.sock:/var/run/docker.sock\n        ports:\n            - 3000:80\n        networks:\n            - trandung\n        deploy:\n            placement:\n                constraints: [node.role == manager]\n\nnetworks:\n    trandung:\n        driver: overlay\n\n ```\n \n \n #### Docker SWARM !!!!!\n Now let’s create a swarm (with one computer for now, but you can easily add more to the swarm). To do this we'll write docker swarm init and we created a swarm!! \n \n ```bash\n docker swarm init\n ```\n \n It’s also added our current computer to the swarm, and since our computer is the first it’s also the manager of the swarm.\n \n Let's build the stack with the following comand line :\n \n ```bash\n docker stack deploy --compose-file=docker-compose.yml production\n ```\n We are doing two things : \n  1 - Build the services \n  2 - deploy them to our local stack called production\n  \n Once done you browse http://localhost:3000 and see the nodejs message, \n \n Hitting F5 will display a different hostname since HAproxy will load balance the request.\n \n ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrandung2k1%2Fdocker-swarm-nodejs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrandung2k1%2Fdocker-swarm-nodejs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrandung2k1%2Fdocker-swarm-nodejs/lists"}