cuong@techmaster.vn
Hướng dẫn này có nhiều tình huống thực tế hơn các tuts khác trên mạng !
.
├── Dockerfile
├── ReadMe.md
├── build
└── myweb
├── demo.js
├── entry.sh
├── package.json
├── server.js
└── yarn.lock
{
"name": "nodeapp",
"version": "1.0.0",
"description": "NodeApp listens at 3000",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node_modules/nodemon/bin/nodemon.js server.js"
},
"author": "Cuong",
"license": "MIT",
"dependencies": {
"express": "^4.16.2",
"nodemon": "^1.14.6"
}
}
yarn add express
yarn add v1.3.2
info No lockfile found.
[1/4] 🔍 Resolving packages...
[2/4] 🚚 Fetching packages...
[3/4] 🔗 Linking dependencies...
[4/4] 📃 Building fresh packages...
success Saved lockfile.
success Saved 47 new dependencies.
├─ accepts@1.3.4
├─ array-flatten@1.1.1
├─ body-parser@1.18.2
├─ bytes@3.0.0
├─ content-disposition@0.5.2
├─ content-type@1.0.4
├─ cookie-signature@1.0.6
├─ cookie@0.3.1
├─ debug@2.6.9
├─ depd@1.1.1
.....
├─ range-parser@1.2.0
├─ raw-body@2.3.2
├─ safe-buffer@5.1.1
├─ send@0.16.1
├─ serve-static@1.13.1
├─ setprototypeof@1.1.0
├─ statuses@1.3.1
├─ type-is@1.6.15
├─ unpipe@1.0.0
├─ utils-merge@1.0.1
└─ vary@1.1.2
✨ Done in 1.57s.
{
"name": "nodeapp",
"version": "1.0.0",
"description": "NodeApp listens at 3000",
"scripts": {
"start": "node server.js"
},
"author": "Cuong",
"license": "MIT",
"dependencies": {
"express": "^4.16.2"
}
}
const express = require('express');
// Constants
const PORT = 3000;
const HOST = '0.0.0.0';
// App
const app = express();
app.get('/', (req, res) => {
res.send('Hello world');
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
node server.js
yarn start
Về bản chất yarn start sẽ xem scripts.start ở
package.json để từ đó gọi node.server
npm start
const express = require('express');
// Constants
const PORT = 3000;
const HOST = '0.0.0.0';
// App
const app = express();
app.get('/', (req, res) => {
res.send('<h1>LOVE</h1><img src="/cat.png">');
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
app.use(express.static('public'))
.
├── package.json
├── public
│ └── cat.png
├── server.js
└── yarn.lock
<h1>LOVE</h1><img src="/cat.png">
yarn add nodemon
node_modules/nodemon/bin/nodemon.js server.js
> node_modules/nodemon/bin/nodemon.js server.js
[nodemon] 1.14.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server.js`
Running on http://0.0.0.0:3000
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
Running on http://0.0.0.0:3000
FROM mhart/alpine-node:latest
WORKDIR /src/app
COPY ./myweb/package.json /src/app/
RUN yarn
COPY ./myweb/ /src/app/
EXPOSE 3000
ENTRYPOINT ["node", "server.js"]
docker logs công cụ dò lỗi khi docker run bị lỗi
cự kỳ hiệu quả
docker logs nodeapp
yarn run v1.3.2
$ node server.js
Running on http://0.0.0.0:3000
Sử dụng bash script entry.sh
#!/bin/sh
ls /
node demo.js
yarn start
Nhớ trao quyền chmod +x entry.sh
ENTRYPOINT ["./entry.sh"]
FROM mhart/alpine-node:latest
WORKDIR /src/app
COPY ./myweb/package.json /src/app/
RUN yarn global add concurrently \
&& yarn
COPY ./myweb/ /src/app/
EXPOSE 3000
ENTRYPOINT ["./entry.sh"]
FROM mhart/alpine-node:latest
WORKDIR /src/app
COPY ./myweb/package.json /src/app/
RUN yarn global add concurrently \
&& yarn
COPY ./myweb/ /src/app/
EXPOSE 3000
ENTRYPOINT ["./entry.sh"]
#!/bin/sh
concurrently "yarn start" "ls /" "node demo.js"
#!/bin/sh
ls /
node demo.js
yarn start