Following sample uses node script file that I created on here.
create package.json file for 'tweet.js'.
[root@localhost express]# vi package.json
{
"name": "twitter_api_docker",
"version": "1.0.0",
"description": "",
"main": "tweet.js",
"scripts": {
"start": "node tweet.js",
"test": "echo \"error: no test specified\" && exit 1"
},
"author": "Yosuke",
"license": "ISC",
"dependencies": {
"express": "^4.16.3",
"twitter": "^1.7.1"
}
}
Check created package.json file with npm command.
[root@localhost express]# npm start
> twitter_api_docker@1.0.0 start /root/express
> node tweet.js
[ { name: 'ラピュタ',
query: '%E3%83%A9%E3%83%94%E3%83%A5%E3%82%BF',
tweet_volume: 1015481,
date: '8/31/2019, 9:04:00 PM' },
{ name: '#VakaTuberは誰だ',
query: '%23VakaTuber%E3%81%AF%E8%AA%B0%E3%81%A0',
tweet_volume: 13462,
date: '8/31/2019, 9:04:00 PM' },
{ name: '国語の教科書',
query: '%E5%9B%BD%E8%AA%9E%E3%81%AE%E6%95%99%E7%A7%91%E6%9B%B8',
tweet_volume: 39878,
date: '8/31/2019, 9:04:00 PM' }
]
Here we create 'Docker' file for image build.
It is based on 'node' template image.
[root@localhost express]# vi Dockerfile
FROM node
WORKDIR /opt/tweetjs
COPY . .
RUN npm install
CMD ["npm", "start"]
Let's run 'docker build' command.
The command read 'Docker' file.
[root@localhost express]# docker build -t node-docker-tut .
Removing intermediate container cc70f978f4e7
Successfully built 1ff3ca634e76
Check built image. You can see necessary files were copied to the image.
[root@localhost express]# docker run -it node-docker-tut bash
root@785a50241c04:/opt/tweetjs# ls
Dockerfile insert.js_bk node_modules package.json server.js2 tweet.js
insert.js lib package-lock.json server.js server.js_
root@785a50241c04:/opt/tweetjs# exit
exit
Solely run the image, and it will be executed 'npm start' as we specified in Docker file.
[root@localhost express]# docker run -it node-docker-tut
> twitter_api_docker@1.0.0 start /root/express
> node tweet.js
[ { name: 'ラピュタ',
query: '%E3%83%A9%E3%83%94%E3%83%A5%E3%82%BF',
tweet_volume: 1015481,
date: '8/31/2019, 9:04:00 PM' },
{ name: '#VakaTuberは誰だ',
query: '%23VakaTuber%E3%81%AF%E8%AA%B0%E3%81%A0',
tweet_volume: 13462,
date: '8/31/2019, 9:04:00 PM' },
{ name: '国語の教科書',
query: '%E5%9B%BD%E8%AA%9E%E3%81%AE%E6%95%99%E7%A7%91%E6%9B%B8',
tweet_volume: 39878,
date: '8/31/2019, 9:04:00 PM' }
]
[root@localhost express]# vi package.json
{
"name": "twitter_api_docker",
"version": "1.0.0",
"description": "",
"main": "tweet.js",
"scripts": {
"start": "node tweet.js",
"test": "echo \"error: no test specified\" && exit 1"
},
"author": "Yosuke",
"license": "ISC",
"dependencies": {
"express": "^4.16.3",
"twitter": "^1.7.1"
}
}
Check created package.json file with npm command.
[root@localhost express]# npm start
> twitter_api_docker@1.0.0 start /root/express
> node tweet.js
[ { name: 'ラピュタ',
query: '%E3%83%A9%E3%83%94%E3%83%A5%E3%82%BF',
tweet_volume: 1015481,
date: '8/31/2019, 9:04:00 PM' },
{ name: '#VakaTuberは誰だ',
query: '%23VakaTuber%E3%81%AF%E8%AA%B0%E3%81%A0',
tweet_volume: 13462,
date: '8/31/2019, 9:04:00 PM' },
{ name: '国語の教科書',
query: '%E5%9B%BD%E8%AA%9E%E3%81%AE%E6%95%99%E7%A7%91%E6%9B%B8',
tweet_volume: 39878,
date: '8/31/2019, 9:04:00 PM' }
]
Here we create 'Docker' file for image build.
It is based on 'node' template image.
[root@localhost express]# vi Dockerfile
FROM node
WORKDIR /opt/tweetjs
COPY . .
RUN npm install
CMD ["npm", "start"]
Let's run 'docker build' command.
The command read 'Docker' file.
[root@localhost express]# docker build -t node-docker-tut .
Removing intermediate container cc70f978f4e7
Successfully built 1ff3ca634e76
Check built image. You can see necessary files were copied to the image.
[root@localhost express]# docker run -it node-docker-tut bash
root@785a50241c04:/opt/tweetjs# ls
Dockerfile insert.js_bk node_modules package.json server.js2 tweet.js
insert.js lib package-lock.json server.js server.js_
root@785a50241c04:/opt/tweetjs# exit
exit
Solely run the image, and it will be executed 'npm start' as we specified in Docker file.
[root@localhost express]# docker run -it node-docker-tut
> twitter_api_docker@1.0.0 start /root/express
> node tweet.js
[ { name: 'ラピュタ',
query: '%E3%83%A9%E3%83%94%E3%83%A5%E3%82%BF',
tweet_volume: 1015481,
date: '8/31/2019, 9:04:00 PM' },
{ name: '#VakaTuberは誰だ',
query: '%23VakaTuber%E3%81%AF%E8%AA%B0%E3%81%A0',
tweet_volume: 13462,
date: '8/31/2019, 9:04:00 PM' },
{ name: '国語の教科書',
query: '%E5%9B%BD%E8%AA%9E%E3%81%AE%E6%95%99%E7%A7%91%E6%9B%B8',
tweet_volume: 39878,
date: '8/31/2019, 9:04:00 PM' }
]