|
1 | | -# getting-started-docker-mysql-nodejs |
2 | | -Running a nodejs application with mysql database as microservices using docker |
| 1 | +# getting started with docker-mysql-nodejs |
| 2 | + |
| 3 | +Running a nodejs application with mysql database as microservices using docker |
| 4 | +using microservice architecture |
| 5 | + |
| 6 | +### our end goal |
| 7 | + |
| 8 | +- Launch mysql server in a docker container. |
| 9 | +- Launch our simple node app in a separate container. |
| 10 | +- Link these two containers and test our integrated mysql-nodejs app. |
| 11 | + |
| 12 | +### prerequisite |
| 13 | + |
| 14 | +- must have docker set up and running on your system |
| 15 | + |
| 16 | +### Launching mysql in a container |
| 17 | + |
| 18 | +1. create a directory for our tutorial `mkdir getting-started-docker-mysql-nodejs` |
| 19 | +2. move to this directory `cd getting-started-docker-mysql-nodejs/` |
| 20 | +3. create a directory for our mysql microservice `mkdir mysql-microservice` |
| 21 | +4. move to this directory `cd mysql-microservice/` |
| 22 | +5. create a Dockerfile with following content (name of file will be `Dockerfile`) |
| 23 | + ``` |
| 24 | + ## Pull the mysql:5.7 image |
| 25 | + FROM mysql:5.7 |
| 26 | + |
| 27 | + ## The maintainer name and email |
| 28 | + MAINTAINER Your Name <name@email.com> |
| 29 | + |
| 30 | + # database = test and password for root = password |
| 31 | + ENV MYSQL_DATABASE=test \ |
| 32 | + MYSQL_ROOT_PASSWORD=password |
| 33 | + |
| 34 | + # when container will be started, we'll have `test` database created with this schema |
| 35 | + COPY ./test-dump.sql /docker-entrypoint-initdb.d/ |
| 36 | + |
| 37 | + ``` |
| 38 | +6. we'll initialize our test database with a sample schema. |
| 39 | +Download [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 |
| 40 | + |
| 41 | +7. create a data directory where mysql will store its content `mkdir data`. |
| 42 | +We will specify this directory wile running our mysql container. |
| 43 | +On Linux default storage directory is `/var/lib/mysql` but in this tutorial we'll use a custom storage directory. |
| 44 | + |
| 45 | +8. build the image with Dockerfile `docker build -t test-mysql .` |
| 46 | +Note that we are inside mysql-microservice directory. `test-mysql` would be name of our image |
| 47 | + |
| 48 | +9. you can check your newly built image using `docker images` |
| 49 | + |
| 50 | +10. run the newly created docker image as container |
| 51 | + ``` |
| 52 | + docker run -d \ |
| 53 | + --publish 6603:3306 \ |
| 54 | + --volume=/home/varunkumar/getting-started-docker-mysql-nodejs/mysql-microservice/data:/var/lib/mysql \ |
| 55 | + --name=test-mysql-microservice test-mysql |
| 56 | + ``` |
| 57 | + |
| 58 | +11. 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`. |
| 59 | +We are also using our custom data storage directory by specifying host path volume `--volume`. |
| 60 | +Replace `/home/varunkumar/getting-started-docker-mysql-nodejs/mysql-microservice/data` path to absolute path of data directory which you created on your system. |
| 61 | +We are also naming our container as test-mysql-microservice `--name` |
| 62 | + |
| 63 | +12. check logs to see if everything went smooth `docker logs test-mysql-microservice` |
| 64 | + |
| 65 | +13. check your container state `docker ps` |
| 66 | + |
| 67 | +14. we have successfully launched a mysql container |
| 68 | + |
| 69 | + |
| 70 | +### connecting to newly launched mysql container from host (optional) |
| 71 | + |
| 72 | +To verify that our test-mysql-microservice container is up and running, we'll connect to it. |
| 73 | +Follow below steps if you have mysql (mysql-client) installed on your system. |
| 74 | + |
| 75 | +1. check the ip of your system. On Linux use `ifconfig`. Lets say that ip is 192.168.43.147 |
| 76 | +2. connect to test-mysql-microservice container with following params- |
| 77 | +user-root, host=192.168.43.147, port=6603, database=test and password=password. |
| 78 | +Remember that we have specified root username and password in Dockerfile. |
| 79 | +Also our container is initialized with test-dump.sql (a schema with database name test) |
| 80 | + |
| 81 | +3. `mysql -u root -p -h 192.168.43.147 -P 6603 -D test` |
| 82 | +use password=password when prompt and hit enter |
| 83 | + |
| 84 | +4. if connected successfully you can see a sample table students `show tables` |
| 85 | +`exit` when done. |
| 86 | + |
| 87 | + |
0 commit comments