头图

1. Review of the previous article

After the previous explanation, presumably the little friends have some understanding of some related words of Docker, here we will return a little:

  • host machine : the system machine corresponding to your computer
  • Dockfile : A file that can be used to create an image
  • image : similar to CD, can be used to create containers, equivalent to the ISO file needed to install the operating system
  • Container : Lightweight virtualization technology, equivalent to a system created by IOS files
A thousand Hamlets, everyone has a different opinion, welcome to complain

2. A Node.js Demo

Then, since we want to plug Node.js into Docker, we must have a Node service, so jsliang took a basic Node.js + TypeScript service written by himself.

Its directory structure is as follows:

docker-node
> src
.eslintrc.js
.gitignore
package-lock.json
package.json
README.md
tsconfig.json

It only takes 2 steps to start this demo:

  • Installation package: npm i
  • Start service: npm run robot

At the 0th second of every minute, the terminal will print:

Docker-demo-07.jpg

This Demo is simply, through commander.js , when running npm run robot , take the command ts-node ./src/index.ts robot .

Then the tasks in /src/index.ts , mainly:

// ……代码省略
console.log('你好,已进入程序'); // 打印
schedule.scheduleJob('0 * * * * *', () => { // 设置定时任务
  const { year, month, day, hour, minute, second } = getDateDetail(new Date());
  console.log(`${year}/${month}/${day} ${hour}:${minute}:${second}`);
});
// ……代码省略

i.e. print a line of text, then print year/month/day hour:minute:second at the 0 second of every minute.

Interested friends can stop and watch the Demo first, and those who are not interested can continue to read.

3. Transform the Node.js service and add the necessary files for Docker

Let's see how to stuff this Node.js service into Docker.

  • target : build Node.js environment in Docker and start service
docker-node
> src
+ .dockerignore
.eslintrc.js
.gitignore
+ Dockerfile
package-lock.json
package.json
README.md
tsconfig.json

We add two files, .dockerignore and Dockerfile , and then stuff the contents inside:

.dockerignore
node_modules
.dockerignore
Dockerfile
*-debug.log
*-error.log
.git
.hg
.svn
.vscode

.dockerignore , like .gitignore , plays the role of ignoring files/folders, so Docker will selectively ignore the corresponding files/folders when running Dockerfile .

Dockerfile
# 本镜像拷贝自 Node v14 版本
FROM node:14

# 指定执行 CMD 的目录,即先 cd 到该目录上
WORKDIR /usr/src/app

# 拷贝宿主机(当前运行终端的位置)的文件到容器中的 app 目录中
COPY . .

# 安装 npm 包
RUN npm install

# 对外暴露本镜像的 80 端口
EXPOSE 80

# 启动 Node 服务
CMD ["npm", "run", "robot"];

Dockerfile will tell Docker: you just need this, then this, then this...

I won't shiver in detail, the comments above are very clear.

Then we only need to operate under Show, and knock the command wildly!

4. Docker deploys Node.js services

  • Create Image: docker image build ./ -t docker-node:1.0.0

    • docker image build : Create mirror
    • ./ : based on the current directory
    • -t : reassigns a pseudo input terminal for the container, usually used concurrently with -i
    • docker-node:1.0.0 : corresponding image name and TAG

Docker-demo-08.png

  • Create a container (Container): docker container create -p 3333:80 docker-node:1.0.0

    • docker container create : Create container
    • -p 3333:80 : port, 3333:80 ie host: container
    • docker-node:1.0.0 : the corresponding image and its TAG

Docker-demo-09.png

  • Start Container: docker container start dd420fc4267ad3bdb9eadfdbf37d89e2592dbc9d030a501b96fe10b07ac565ff

    • docker container start : Start the container
    • dd420fc4267ad3bdb9eadfdbf37d89e2592dbc9d030a501b96fe10b07ac565ff : container ID, which is docker ps -a found by CONTAINER ID

Docker-demo-10.png

  • Check the operation of the container (Container): docker ps -a

Docker-demo-11.png

  • View the log of the container (Container): docker logs -f dd420fc4267a

    • dd420fc4267a : container ID, which can be found by docker ps -a

Docker-demo-12.png

  • Enter Container: docker exec -it dd420fc4267a bash

    • dd420fc4267a : container ID, which can be found by docker ps -a
  • View README.md file: cat -n README.md

    • cat : cat (English full spell: concatenate) command is used to concatenate files and print to standard output device
    • -n : Number all output lines starting from 1

Docker-demo-13.png

V. Summary

Through the above explanation, you must have a general understanding of some of Docker's instructions. In the next article, we will explain the instruction , which is necessary for newcomers in Docker, and how to do Hosts isolation, and modify the time in the container.

Stay tuned: 05 - Troubleshooting & Docker Instructions


What is the difference between the front end that does not toss and salted fish!

Friends who think the article is good are welcome to like/star.

If you need to contact jsliang :

Personal contact information is stored on the Github homepage, welcome to toss together~

Strive to build yourself into a lifelong learning bar programmer who is full of exploration, likes tossing, and is willing to expand his knowledge.

jsliang's documentation repository is licensed by Liang Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License . <br/>Created based on the work on https://github.com/LiangJunrong/document-library . <br/>Use rights other than authorized by this license agreement may be obtained from https://creativecommons.org/licenses/by-nc-sa/2.5/cn/162294d9a18af3.

jsliang
393 声望31 粉丝

一个充满探索欲,喜欢折腾,乐于扩展自己知识面的终身学习斜杠程序员