Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0d2c78c

Browse files
committed
added instructions for mysql container
1 parent 83650f9 commit 0d2c78c

File tree

9 files changed

+154
-62
lines changed

9 files changed

+154
-62
lines changed

‎.gitignore

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,5 @@
1-
# Logs
2-
logs
3-
*.log
4-
npm-debug.log*
5-
yarn-debug.log*
6-
yarn-error.log*
1+
# exclude everything form data folder
2+
mysql-microservice/data/*
73

8-
# Runtime data
9-
pids
10-
*.pid
11-
*.seed
12-
*.pid.lock
13-
14-
# Directory for instrumented libs generated by jscoverage/JSCover
15-
lib-cov
16-
17-
# Coverage directory used by tools like istanbul
18-
coverage
19-
20-
# nyc test coverage
21-
.nyc_output
22-
23-
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24-
.grunt
25-
26-
# Bower dependency directory (https://bower.io/)
27-
bower_components
28-
29-
# node-waf configuration
30-
.lock-wscript
31-
32-
# Compiled binary addons (https://nodejs.org/api/addons.html)
33-
build/Release
34-
35-
# Dependency directories
36-
node_modules/
37-
jspm_packages/
38-
39-
# TypeScript v1 declaration files
40-
typings/
41-
42-
# Optional npm cache directory
43-
.npm
44-
45-
# Optional eslint cache
46-
.eslintcache
47-
48-
# Optional REPL history
49-
.node_repl_history
50-
51-
# Output of 'npm pack'
52-
*.tgz
53-
54-
# Yarn Integrity file
55-
.yarn-integrity
56-
57-
# dotenv environment variables file
58-
.env
59-
60-
# next.js build output
61-
.next
4+
# exception to the rule
5+
!mysql-microservice/data/.gitkeep

‎LICENSE renamed to ‎LICENSE.md

File renamed without changes.

‎README.md

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,87 @@
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+

‎mysql-microservice/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## Pull the mysql:5.7 image
2+
FROM mysql:5.7
3+
4+
## The maintainer name and email
5+
MAINTAINER Varun Kumar <varunon9@gmail.com>
6+
7+
# database = test and password for root = password
8+
ENV MYSQL_DATABASE=test \
9+
MYSQL_ROOT_PASSWORD=password
10+
11+
# when container will be started, we'll have `test` database created with this schema
12+
COPY ./test-dump.sql /docker-entrypoint-initdb.d/

‎mysql-microservice/data/.gitkeep

Whitespace-only changes.

‎mysql-microservice/test-dump.sql

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
-- MySQL dump 10.13 Distrib 5.6.16, for debian-linux-gnu (x86_64)
2+
--
3+
-- Host: localhost Database: test
4+
-- ------------------------------------------------------
5+
-- Server version 5.6.16-1~exp1
6+
7+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
8+
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
9+
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
10+
/*!40101 SET NAMES utf8 */;
11+
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
12+
/*!40103 SET TIME_ZONE='+00:00' */;
13+
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
14+
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
15+
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
16+
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
17+
18+
--
19+
-- Table structure for table `students`
20+
--
21+
22+
DROP TABLE IF EXISTS `students`;
23+
/*!40101 SET @saved_cs_client = @@character_set_client */;
24+
/*!40101 SET character_set_client = utf8 */;
25+
CREATE TABLE `students` (
26+
`rollNo` int(11) NOT NULL,
27+
`name` varchar(255) DEFAULT NULL,
28+
PRIMARY KEY (`rollNo`)
29+
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
30+
/*!40101 SET character_set_client = @saved_cs_client */;
31+
32+
--
33+
-- Dumping data for table `students`
34+
--
35+
36+
LOCK TABLES `students` WRITE;
37+
/*!40000 ALTER TABLE `students` DISABLE KEYS */;
38+
INSERT INTO `students` VALUES (1130328,'Varun Kumar');
39+
/*!40000 ALTER TABLE `students` ENABLE KEYS */;
40+
UNLOCK TABLES;
41+
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
42+
43+
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
44+
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
45+
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
46+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
47+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
48+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
49+
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
50+
51+
-- Dump completed on 2018年07月06日 23:57:05
161 KB
Loading[フレーム]
108 KB
Loading[フレーム]
74.9 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /