{"id":16082139,"url":"https://github.com/varunon9/getting-started-docker-mysql-nodejs","last_synced_at":"2025-07-28T08:34:09.402Z","repository":{"id":48661735,"uuid":"140015642","full_name":"varunon9/getting-started-docker-mysql-nodejs","owner":"varunon9","description":"Running a nodejs application with mysql database  as microservices using docker","archived":false,"fork":false,"pushed_at":"2022-12-08T06:43:37.000Z","size":910,"stargazers_count":31,"open_issues_count":3,"forks_count":30,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-02T08:35:56.969Z","etag":null,"topics":["docker","microservice","mysql-docker","nodejs-docker"],"latest_commit_sha":null,"homepage":null,"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/varunon9.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-07-06T17:57:06.000Z","updated_at":"2025-03-28T08:59:16.000Z","dependencies_parsed_at":"2023-01-24T17:00:19.820Z","dependency_job_id":null,"html_url":"https://github.com/varunon9/getting-started-docker-mysql-nodejs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/varunon9/getting-started-docker-mysql-nodejs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/varunon9%2Fgetting-started-docker-mysql-nodejs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/varunon9%2Fgetting-started-docker-mysql-nodejs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/varunon9%2Fgetting-started-docker-mysql-nodejs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/varunon9%2Fgetting-started-docker-mysql-nodejs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/varunon9","download_url":"https://codeload.github.com/varunon9/getting-started-docker-mysql-nodejs/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/varunon9%2Fgetting-started-docker-mysql-nodejs/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267485681,"owners_count":24095296,"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","status":"online","status_checked_at":"2025-07-28T02:00:09.689Z","response_time":68,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["docker","microservice","mysql-docker","nodejs-docker"],"created_at":"2024-10-09T11:25:53.045Z","updated_at":"2025-07-28T08:34:09.356Z","avatar_url":"https://github.com/varunon9.png","language":"JavaScript","readme":"# getting started with docker-mysql-nodejs\n\nRunning a nodejs application with mysql database using docker and microservice architecture\n\n### Our end goal\n\n- Launch mysql server in a docker container.\n- Launch our simple node app in a separate container.\n- Link these two containers and test our integrated mysql-nodejs app.\n\n### Youtube link-\n\nWatch this tutorial at https://youtu.be/tIbMSqTEpfY\n\n### Prerequisite\n\n- must have docker set up and running on your system\n\n### Launching mysql in a container\n\n1. Create a directory for our tutorial `mkdir getting-started-docker-mysql-nodejs`\n2. Move to this directory `cd getting-started-docker-mysql-nodejs/`\n3. Create a directory for our mysql microservice `mkdir mysql-microservice`\n4. Move to this directory `cd mysql-microservice/`\n5. Create a Dockerfile with following content (name of file will be `Dockerfile`)\n    ```\n    ## Pull the mysql:5.7 image\n    FROM mysql:5.7\n\n    ## The maintainer name and email\n    MAINTAINER Your Name \u003cname@email.com\u003e\n\n    # database = test and password for root = password\n    ENV MYSQL_DATABASE=test \\\n        MYSQL_ROOT_PASSWORD=password\n\n    # when container will be started, we'll have `test` database created with this schema\n    COPY ./test-dump.sql /docker-entrypoint-initdb.d/\n\n    ```\n6. We'll initialize our test database with a sample schema. \nDownload [test-dump.sql](https://github.com/varunon9/getting-started-docker-mysql-nodejs/blob/master/mysql-microservice/test-dump.sql) and put it inside mysql-microservice folder along with Dockerfile\n\n7. Create a data directory where mysql will store its content `mkdir data`. \nWe will specify this directory while running our mysql container. \nOn Linux default storage directory is `/var/lib/mysql` but in this tutorial we'll use a custom storage directory.\n\n8. Build the image with Dockerfile `docker build -t test-mysql .` \nNote that we are inside mysql-microservice directory. `test-mysql` would be name of our image\n\n9. You can check your newly built image using `docker images`\n![Building the image using Dockerfile](./screenshots/building-test-mysql-image.png)\n\n10. Run the newly created docker image as container \n    ```\n    docker run  -d \\\n    --publish 6603:3306 \\\n    --volume=/home/varunkumar/getting-started-docker-mysql-nodejs/mysql-microservice/data:/var/lib/mysql \\\n    --name=test-mysql-microservice test-mysql\n    ```\n\n11. With above command we started our container in detach mode `-d` and mapped host(your machine) port 6603 with container port 3306 (mysql server) `--publish 6603:3306`. \nWe are also using our custom data storage directory by specifying host path volume `--volume`.\nReplace  `/home/varunkumar/getting-started-docker-mysql-nodejs/mysql-microservice/data` path to absolute path of data directory which you created on your system.\nWe are also naming our container as test-mysql-microservice `--name`\n\n12. Check logs to see if everything went smooth `docker logs test-mysql-microservice`\n\n13. Check your container state `docker ps`\n![Running the docker image](./screenshots/running-test-mysql-microservice-container.png)\n\n14. We have successfully launched a mysql container\n\n\n### Connecting to newly launched mysql container from host (optional)\n\nTo verify that our test-mysql-microservice container is up and running, we'll connect to it.\nFollow below steps if you have mysql (mysql-client) installed on your system.\n\n1. Check the ip of your system. On Linux use `ifconfig`. Lets say that ip is 192.168.43.147\n2. Connect to test-mysql-microservice container with following params-\nuser-root, host=192.168.43.147, port=6603, database=test and password=password. \nRemember that we have specified root username and password in Dockerfile. \nAlso our container is initialized with  test-dump.sql (a schema with database name test)\n\n3. `mysql -u root -p -h 192.168.43.147 -P 6603 -D test` \nUse password=password when prompt and hit enter\n\n4. If connected successfully you can see a sample table students `show tables` \n`exit` when done.\n![Connecting to mysql container from host](./screenshots/connecting-to-test-mysql-microservice.png)\n\n### Launching nodejs app in a container\n\n1. Right now we are in mysql-microservice directory. We go to project root directory `cd ..`\n2. create directory for node microservice `mkdir nodejs-microservice`\n3. Move to this directory `cd nodejs-microservice/`\n4. Create a Dockerfile with following content (name of file will be `Dockerfile`)\n    ```\n    # Use Node v8 as the base image.\n    FROM node:8\n\n    # create and set app directory\n    RUN mkdir -p /usr/src/app\n    WORKDIR /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+)\n    COPY package*.json ./\n    RUN npm install\n\n    # Copy app source from current host directory to container working directory\n    COPY . .\n\n    # Run app\n    CMD [\"npm\", \"start\"]\n\n    ```\n5. We need a package.json file for our node-microservice app as well as source code.\nFor this tutorial, I've already created one. \nDownload [package.json](https://github.com/varunon9/getting-started-docker-mysql-nodejs/blob/master/nodejs-microservice/package.json) as well as [index.js](https://github.com/varunon9/getting-started-docker-mysql-nodejs/blob/master/nodejs-microservice/index.js) and put it inside nodejs-microservice folder along with Dockerfile.\n\n6. Build the image with Dockerfile `docker build -t test-nodejs .` \nNote that we are inside nodejs-microservice directory. `test-nodejs` would be name of our image\n\n7. You can check your newly built image using `docker images`\n![Building the image using Dockerfile](./screenshots/building-test-nodejs-image.png)\n\n8. Run the newly created docker image as container \n    ```\n    docker run  -d \\\n    --publish 4000:4000 \\\n    -e MYSQL_USER='root' \\\n    -e MYSQL_PASSWORD='password' \\\n    -e MYSQL_DATABASE='test' \\\n    -e MYSQL_HOST='172.17.0.2' \\\n    --link test-mysql-microservice:db \\\n    --name=test-nodejs-microservice test-nodejs\n    ```\n![Running the image using Dockerfile](./screenshots/running-test-nodejs-microservice-container.png)\n\n9. Explaination of above command-\n* `-d` run in detach mode\n* `--publish` map the host port 4000 to the container port 4000\n* `-e` pass environment variables to nodejs app necessary to make mysql connection (check index.js file)\n* `--link test-mysql-microservice:db` link to the container named test-mysql-microservice and refer to it as db\n* `--name` naming our container as test-nodejs-microservice\n\n10. How to know your MYSQL_HOST- \nNote that I am using `172.17.0.2` ip-address as MYSQL_HOST. This is the IpAddress of our test-mysql-microservice container.\nYou must replace this value to your container's ipAddress. Use `docker inspect test-mysql-microservice | grep IPAddress`\n\n\n### Testing our complete app \n\nIf everything is good so far then congratulations :smile: You have a complete app running with two microservices. To test this you can use CURL command from your host machine\n\n1. Get homepage of your app `curl -X GET localhost:4000`\n\n2. Get list of all students from test database `curl -X POST 192.168.43.147:4000/get-students`\nHere 192.168.43.147 is my host IpAddress `ifconfig | grep inet`\n\n3. Add a new student to your test db `curl --header \"Content-Type: application/json\" -d '{\"rollNo\": 1130360, \"name\": \"Abhishek Goswami\"}' -X POST localhost:4000/add-student`\n\n4. Again fetch all students to see updated results `curl -X POST 192.168.43.147:4000/get-students`\n\n5. Modify source code of nodejs app, build image, run container and test again.\n\n### Queries/Comments\n\nYou can contact me at varunon9@gmail.com or create github issues.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvarunon9%2Fgetting-started-docker-mysql-nodejs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvarunon9%2Fgetting-started-docker-mysql-nodejs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvarunon9%2Fgetting-started-docker-mysql-nodejs/lists"}