{"id":22039502,"url":"https://github.com/mpolinowski/node-express-docker-starter","last_synced_at":"2025-10-09T11:08:23.641Z","repository":{"id":111483998,"uuid":"135029898","full_name":"mpolinowski/node-express-docker-starter","owner":"mpolinowski","description":"Run your Node.js app from a Docker Container","archived":false,"fork":false,"pushed_at":"2018-05-27T08:43:58.000Z","size":238,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-07T21:45:30.241Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/mpolinowski.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":"2018-05-27T08:43:51.000Z","updated_at":"2022-04-11T07:50:18.000Z","dependencies_parsed_at":"2023-03-09T00:15:31.831Z","dependency_job_id":null,"html_url":"https://github.com/mpolinowski/node-express-docker-starter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpolinowski%2Fnode-express-docker-starter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpolinowski%2Fnode-express-docker-starter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpolinowski%2Fnode-express-docker-starter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpolinowski%2Fnode-express-docker-starter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mpolinowski","download_url":"https://codeload.github.com/mpolinowski/node-express-docker-starter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252961858,"owners_count":21832192,"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":[],"created_at":"2024-11-30T11:11:02.501Z","updated_at":"2025-10-09T11:08:18.600Z","avatar_url":"https://github.com/mpolinowski.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Run your Node.js app from a Docker Container\n\n\n\u003c!-- TOC --\u003e\n\n- [Run your Node.js app from a Docker Container](#run-your-nodejs-app-from-a-docker-container)\n  - [01 Create your Node App](#01-create-your-node-app)\n  - [02 Dockerizing your Node.js application](#02-dockerizing-your-nodejs-application)\n    - [Building your image](#building-your-image)\n    - [Running the Container](#running-the-container)\n    - [Running the Container with Kitematic](#running-the-container-with-kitematic)\n\n\u003c!-- /TOC --\u003e\n\n\n## 01 Create your Node App\n\nKeeping it simple - create a folder and use [Node.js](https://nodejs.org) to [npm](https://npmjs.com/package/cylon) install [Express.js](https://expressjs.com)\n\n```bash\nnpm init\nnpm install express\n```\n\nThe __init__ will create a package.json file - we can add a start script to it to start our Express Webserver:\n\n```json\n\"scripts\": {\n    \"start\": \"node ./index.js\"\n  }\n```\n\nThen configure your webserver by adding the __index.js__ file to the root directory:\n\n\n```js\n//Load express module with `require` directive\nvar express = require('express')\nvar app = express()\n\n\n//Define request response in root URL (/)\napp.get('/', function (req, res) {\n  res.send('Hello World!')\n})\n\n\n//Launch listening server on port 3000\napp.listen(3000, function () {\n  console.log('app listening on port http://localhost:3000!')\n})\n```\n\nYou can test the webserver by running the npm script from your console:\n\n\n```bash\nnpm start\n```\n\nAnd access _http://localhost:3000_ in your preferred web browser - you should be greeted by:\n\n\n![Docker \u0026 Node.js](./docker_01.png)\n\n\n## 02 Dockerizing your Node.js application\n\nIf you did not so far, first [install Docker](https://docs.docker.com/install/) on your computer.\n\n\nThe Docker container is launched on the basis of a Docker image, a template with the application details. The Docker image is created with instructions written in the Dockerfile. Let’s add __Dockerfile__ to the directory with our application:\n\n\n```docker\nFROM node:latest\n\n# Create app directory\nWORKDIR /usr/src/app\n\n# Install app dependencies\n# A wildcard is used to ensure both package.json AND package-lock.json are copied\n# where available (npm@5+)\nCOPY package*.json ./\n\nRUN npm install\n# If you are building your code for production\n# RUN npm install --only=production\n\n# Bundle app source\nCOPY . .\n\nEXPOSE 8080\nCMD [ \"npm\", \"start\" ]\n```\n\n\n* The first thing we need to do is define from what image we want to build from. Here we will use the latest version of node available from the [Docker Hub](https://hub.docker.com/): __FROM node:latest__\n* Next we create a directory to hold the application code inside the image, this will be the working directory for your application: __WORKDIR /app__\n* This image comes with Node.js and NPM already installed so the next thing we need to do is to install your app dependencies using the npm binary: __COPY package*.json ./__, __RUN npm install__\n\nNote that, rather than copying the entire working directory, we are only copying the package.json file.\n\n* To bundle your app's source code inside the Docker image, use the COPY instruction: __COPY . /app__\n* Your app binds to port 3000 so you'll use the EXPOSE instruction to have it mapped by the docker daemon: __EXPOSE 3000__\n* Last but not least, define the command to run your app using CMD which defines your runtime. Here we will use the basic npm start which will run node server.js to start your server: __CMD [ \"npm\", \"start\" ]__\n\n\nCreate a __.dockerignore__ file in the same directory as your Dockerfile with following content:\n\n\n```\nnode_modules\nnpm-debug.log\n```\n\nThis will prevent your local modules and debug logs from being copied onto your Docker image and possibly overwriting modules installed within your image.\n\n\n### Building your image\n\nGo to the directory that has your Dockerfile and run the following command to build the Docker image. The -t flag lets you tag your image so it's easier to find later using the docker images command: \n\n\n```bash\ndocker build -t hello-world .\n```\n\n\n![Docker \u0026 Node.js](./docker_02.png)\n\n\nYou can now list the Docker image:\n\n\n```bash\ndocker images\n```\n\n\n![Docker \u0026 Node.js](./docker_03.png)\n\n\n### Running the Container\n\nRunning your image with -d runs the container in detached mode, leaving the container running in the background. The -p flag redirects a public port to a private port inside the container. Run the image you previously built:\n\n\n```bash\ndocker run -p 8080:3000 hello-world\n```\n\n\n![Docker \u0026 Node.js](./docker_04.png)\n\n\nThe Container is now running and listening on Port 3000 - to access the container we have to use the exposed Port 8080 on localhost or the local IP address of the machine we running it on.\n\n\n### Running the Container with Kitematic\n\nAlternatively, we can use the graphical user interface [Kitematic](https://kitematic.com). First install the [Docker Toolbox](https://docker.com/toolbox) for Windows and macOS and start Kitematic. Choose __My Images__ and __Create__ on our hello-world container:\n\n\n![Docker \u0026 Node.js](./docker_05.png)\n\n\nOur container is now running:\n\n\n![Docker \u0026 Node.js](./docker_06.png)\n\n\nBe aware that the __Port 3000__ that we defined is only the internal Docker Port. You can check (and change) the exposed port under __Settings__ in the __Hostname/Ports__ tab:\n\n\n![Docker \u0026 Node.js](./docker_07.png)\n\n\nAs seen above, our container is exposed on Port 32772.\n\n\nAnd can be accessed on __http://localhost:32772__ as well as on the local network IP (in this case the 192.168.1.112):\n\n\n![Docker \u0026 Node.js](./docker_08.png)\n\n\n\n\n\n\n\n\n\n\n\n---\n__Further Readings__:\n\nhttps://medium.com/statuscode/dockerising-a-node-js-and-mongodb-app-d22047e2806f","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpolinowski%2Fnode-express-docker-starter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmpolinowski%2Fnode-express-docker-starter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpolinowski%2Fnode-express-docker-starter/lists"}