Docker Compose is a tool in the form of YAML configuration to run multi-container docker applications. With a single command, we can start or stop all the services specified in YAML configuration.
Docker Compose is a 3 step process
Source Code available under https://github.com/ramasubbareddy1224/dockercompose-node-mysql-express-nodemon
Happy Coding :)
Docker Compose is a 3 step process
- Define app configuration using Dockerfile
- Define the services that app requires in docker-compose.yml so they can be run together in an isolated environment
- Run docker-compose up to starts and runs entire app
In the previous article (http://millionvisit.blogspot.com/2020/05/docker-assignments-12-nodejs-mysql.html) we had written docker run command to spin up "mysql" instance. But, In this article we are going to spin up "mysql" instance by using docker-compose.yml file
Create a folder structure as per below image in the host machine
Create a folder structure as per below image in the host machine
- Add "mysql" and "express" npm packages as a dependency in the package.json file
- Add "nodemon" npm package as a dev dependency in the package.json file
- configure npm start command as "nodemon -L index.js" in package.json
- Write required code into index.js file to connect "mysql" container and expose endpoint using express.js
- Configure Dockerfile with below instructions
- set base image as "mhart/alpine-node"
- set working directory as "/usr/app"
- copy all files from host machine and do npm install
- set CMD command to execute npm run start
6. Write following instructions in docker-compose yml file
- Configure "testmysql-service" service by pulling "mysql:5.7" image, set "MY_SQL_ROOT_PASSWORD" as "mysql123" and port mapping - 3306
- Configure "testnodeexpress-service" service by specifying build context as Dockerfile, create bindmount to /usr/app container path, volume mount to /usr/app/node_modules, dependent on "testmysql-service" and link to "testmysql-service"
7. Spin up containers using docker-compose up command
8. check http://localhost:9030
Step 1:
> npm install mysql
> npm install express
Step 2:
> npm install -D nodemon
Step 3:
"scripts": {
"start": "nodemon -L index.js"
}
Step 4:
var express = require('express');
var mysql=require('mysql');
var app = express();
app.get('/mysql', function (req, res) {
var con = mysql.createConnection({
host: "testmysql",
user: "root",
password: "mysql123",
port:"3306",
});
con.connect(function(err) {
if (err) throw err;
res.send("mysql connected successfully");
});
});
app.listen(9030, function () {
console.log('App listening on port 9030!');
});
Step 5:
FROM mhart/alpine-node
WORKDIR /usr/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 9030
CMD ["npm", "run", "start"]
Step 6:
version: '3'
services:
testnodeexpress-service:
build: .
volumes:
- .:/usr/app
- /usr/app/node_modules
environment:
NODE_ENV: development
ports:
- 9030:9030
command: npm run start
depends_on:
- testmysql-service
links:
- "testmysql-service:testmysql"
testmysql-service:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: mysql123
ports:
- 3306:3306
Step 7:
> docker-compose up
Source Code available under https://github.com/ramasubbareddy1224/dockercompose-node-mysql-express-nodemon
Happy Coding :)
No comments:
Post a Comment