{"id":31051826,"url":"https://github.com/ajay-develops/docker-nextjs-express-mongodb","last_synced_at":"2026-02-12T21:01:27.080Z","repository":{"id":291304222,"uuid":"860715792","full_name":"ajay-develops/docker-nextjs-express-mongodb","owner":"ajay-develops","description":null,"archived":false,"fork":false,"pushed_at":"2024-09-23T17:43:34.000Z","size":17,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-15T00:55:17.658Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"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/ajay-develops.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,"zenodo":null}},"created_at":"2024-09-21T02:03:48.000Z","updated_at":"2024-09-23T17:43:37.000Z","dependencies_parsed_at":"2025-05-03T19:19:47.423Z","dependency_job_id":"477c153d-5148-4f7f-b1e4-7cab7eb93a1e","html_url":"https://github.com/ajay-develops/docker-nextjs-express-mongodb","commit_stats":null,"previous_names":["ajay-develops/docker-nextjs-express-mongodb"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ajay-develops/docker-nextjs-express-mongodb","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajay-develops%2Fdocker-nextjs-express-mongodb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajay-develops%2Fdocker-nextjs-express-mongodb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajay-develops%2Fdocker-nextjs-express-mongodb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajay-develops%2Fdocker-nextjs-express-mongodb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ajay-develops","download_url":"https://codeload.github.com/ajay-develops/docker-nextjs-express-mongodb/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajay-develops%2Fdocker-nextjs-express-mongodb/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29381028,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-12T20:34:40.886Z","status":"ssl_error","status_checked_at":"2026-02-12T20:23:00.490Z","response_time":55,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6: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":[],"created_at":"2025-09-15T00:55:16.043Z","updated_at":"2026-02-12T21:01:27.015Z","avatar_url":"https://github.com/ajay-develops.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Docker: Next.js - Express - MongoDB\n\nDocker deployment for a Next.js app with api's in Express.js and MongoDB as database using secure jwt cookies for authentication\n\n## Learning Docker\n\n```bash\ndocker ps # list of running containers\ndocker ps -a # list of all containers\ndocker images # list of used images\ndocker images -a # list of all images\n\ndocker container prune # deletes all not running containers\ndocker images prune # deletes all unused images\n\ndocker stop \u003ccontainer_id/container_name\u003e # stops the container gracefully takes time\ndocker kill \u003ccontainer_id/container_name\u003e # instantly kills/stops the container\n\ndocker rmi \u003cimage_name/image_id\u003e # removes image from docker\n\ndocker network create \u003cnetwork_name\u003e # create a new docker network\ndocker network ls # get the list of all the docker networks\ndocker network rm \u003cnetwork_name/network_id\u003e # delete the network\ndocker volume create \u003cvolume_name\u003e # create a new docker volume\ndocker volume ls # list of all docker volumes\ndocker volume rm \u003cvolume_name\u003e # deletes a docker volume\n\ndocker exec -it \u003ccontainer_name\u003e /bin/bash # puts us inside the bash shell of the container\ndocker exec -it busybox sh # puts us inside the bash shell of the container busybox\n\ndocker run -d \u003cimage_name\u003e # runs the container in detached mode( basically the terminal will available for further use after starting the container)\ndocker logs \u003ccontainer_id / or a few starting characters of the container id just enough to separate it from the rest of the containers\u003e # shows the logs of the container\ndocker logs --follow \u003ccontainer_id /....\u003e # to show the current logs of the container and keep on showing the logs as they are created\n\ndocker compose up # create and start all the containers and services mentioned in the compose.yaml file\n\n```\n\n### creating an image for express server\n\n#### Dockerfile\n\n```Dockerfile\nFROM node:20 # or alpine-node:20   # it pulls a base image to start with\n\nWORKDIR /usr/src/app  # sets a working directory in the container where the project files will be copied\n\nCOPY package* .  # copies the package.json and package-lock.json files first. Improves build time by taking advantage of caching this and the next step (since the next step is heavy on build time and resources) if the package.json has not changed.\n\nRUN npm i    # installs the dependencies\n\nCOPY . .     # copies all the other project files to the working directory in the container\n\nEXPOSE \u003cport_number_to_expose\u003e\n\n# the commands above run when the container is built using the image\n\n# the below command CMD runs when the container starts\n\nCMD [\"npm\", \"start\"]    # runs when the container starts\n```\n\n#### .dockerignore\n\n```.dockerignore\nnode_modules\n```\n\nbash command to build the image\n\n```bash\ndocker build\n -t \u003cimage_name\u003e # image tag\n . # directory where the Dockerfile is present\n```\n\n### running a container from an image for the first time\n\n```bash\ndocker run\n -p \u003cport_of_local_machine\u003e:\u003cport_of_docker_container\u003e # to forward all the requests coming to the machine on the specified port to the container on the specified port\n --name \u003ccontainer_name\u003e \u003cimage_name\u003e # to organize the images otherwise a random id and name will be given to the image\n```\n\n### starting an already built container\n\n```bash\ndocker start \u003ccontainer_name\u003e\n```\n\n## adding volume to a database in docker\n\n```bash\ndocker volume create \u003cvolume_name\u003e\n# docker volume ls   # to list all the volumes\n# docker volume rm \u003cvolume_name\u003e   # to delete the volume name\n```\n\n## adding network to the database\n\n```bash\ndocker network create \u003cnetwork_name\u003e # select network type to bridge\n# docker network ls    # list all networks\n# docker network rm \u003cnetwork_name\u003e    # delete the specified network/s\n```\n\n### running the database container using the same network\n\n```bash\ndocker build -p 27017:27017 --name mongodb-server --network \u003cnetwork_name\u003e -v \u003cvolume_name\u003e mongo # mongo is the image name of mongodb\n```\n\n### creating an express server to connect to the database on another container\n\n_.index.js._\n\n```node\nconst express = require(\"express\");\nconst mongoose = require(\"mongoose\");\n\nconst app = express();\nconst port = 8080;\n\nmongoose.connect(\"mongodb:/mongodb-server:27017/myDatabase\");\n/* the \"mongodb-server\" in the connection string resolves to the ip address of the mongodb-server container using the same network as this express server container */\n\nconst EntrySchema = new mongoose.Schema({\n  text: String,\n  data: { type: Date, default: Date.now },\n});\n\nconst Entry = mongoose.model(\"Entry\", EntrySchema);\n\napp.get(\"/\", async (req, res) =\u003e {\n  try {\n    const entry = new Entry({ text: \"new entry added\" });\n    await entry.save();\n    res.send(\"New Entry Saved\");\n  } catch (err) {\n    res.send(500).send(\"Error Occurred\");\n  }\n});\n\napp.listen(port, () =\u003e {\n  console.log(\"Example app listening on port\", port);\n});\n```\n\n_.Dockerfile._\n\n```Dockerfile\nFROM node:20\n\nWORKDIR /usr/app/src\n\nCOPY package* .\n\nRUN npm i\n\nCOPY . .\n\nEXPOSE 8080\n\nCMD [\"npm\",\"start\"]\n# or\n# CMD [\"npm\",\"run\",\"dev\"] in development mode\n```\n\n_.dockerignore._\n\n```dockerignore\nnode_modules\n```\n\n_.to build the image._\n\n```bash\ndocker build -t backend-server .\n```\n\n;ls\n\nto run the container from image\n\n```bash\ndocker docker run -p 8080:8080 --name backend-sever --network \u003cnetwork_name\u003e\n```\n\nnow you can send get request to the server at [http://localhost:8080/]([http://localhost:8080/]) and it will save the entry to the database\nas the server will be able to connect to the database over the common network\n\n#### Note\n\n- the image is required to run a container that is build from that image\n\n### Multi Stage Builds\n\nQ: What if we want to allow the dev backend to hot reload?\nBut the prod env to not?\n\nAns: We create Multi Stage Builds to be able to spin two or more different images (development and production) using the same Dockerfile\n\n```Dockerfile\nFROM node:20 AS base\nWORKDIR /usr/src/app\n\nCOPY package* .\n\nRUN npm i\n\nFROM base AS development\nCOPY . .\nCMD [\"npm\",\"run\", \"dev\"]\n\nFROM base AS test\nCOPY . .\nCMD [\"npm\",\"run\", \"dev\"]\n\nFROM base AS production\nCOPY . .\nRUN npm prune --production\nCMD [\"npm\", \"run\", \"start\"]\n\n```\n\n#### Building dev\n\n```bash\ndocker build\n--target development\n-t my_app:dev\n.\n```\n\n```bash\ndocker run\n-e MONGO_URI=mongodb://localhost:27017/\nmy_database\n-p 3000:3000\n-v .:/usr/src/app # mounts the working directory to as the volume of the container, so any changes made in the working directory (on local machine) get propagated to the container, and vice versa(any changes the docker container makes to the files or we make by going inside the terminal get propagated to the local machine working directory (which we set in the 2nd step of Dockerfile))\nmy_app:dev\n```\n\n### Docker Compose\n\nThe problem -\nA project has a lot of auxiliary services it needs to use\nFor example, MongoDB/Postgres/Kafka/MySQL ...\n\nThe solution -\nDocker Compose is a tool for defining and running multi-container Docker Applications.\nWith Docker Compose a YAML file is used to configure the application's services.\nThen with a single command, all the services from the configuration of the application can be created and started.\n\n- All the containers created using the docker compose are already connected via a network and can be referenced by their name\n\n_.example._\n\n```yaml\nservices:\n  mongodb:\n    image: mongo:latest\n    ports:\n      - \"27017:27017\"\n    volumes:\n      - volume-name:/data/db\n\n  backend:\n    build: ./backend/\n    ports:\n      - \"8080:8080\"\n    depends_on:\n      - mongodb\n\n  frontend:\n    build: ./frontend/\n    ports:\n      - \"3000:3000\"\n    depends_on:\n      - backend\n\nvolumes:\n  volume-name:\n```\n\n## References\n\nYoutube Videos of DevOps by [Harkirat](https://github.com/hkirat)\n\n- [part-1](https://youtu.be/fSmLiOMp2qI)\n- [part-2](https://youtu.be/KuCwrySinqI)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajay-develops%2Fdocker-nextjs-express-mongodb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fajay-develops%2Fdocker-nextjs-express-mongodb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajay-develops%2Fdocker-nextjs-express-mongodb/lists"}