- The code for this document: github.com/LiangJunron…
- Docker series: github.com/LiangJunron…
First, the previous review
After the above explanation, you must have some understanding of some related words of Docker. Here is a little return:
- Host: The system machine that corresponds to your computer
- Dockfile: A file that can be used to create an image
- Image: Similar to a CD-ROM, it can be used to create containers. It is equivalent to an ISO file used to install an operating system
- Container: Lightweight virtualization technology, the equivalent of a system created from IOS files
A thousand Hamlets, each with a different view, welcome to ridicule
A Node.js Demo
So, to plug Node.js onto Docker, we need a Node service, so Jsliang wrote its own base Node.js + TypeScript service.
Its directory structure is as follows:
docker-node
> src
.eslintrc.js
.gitignore
package-lock.json
package.json
README.md
tsconfig.json
Copy the code
Launching the Demo takes only 2 steps:
- The installation package:
npm i
- Start the service:
npm run robot
At the 0th second of every minute, the terminal prints:
NPM run robot can be executed using the ts-node./ SRC /index.ts robot command.
Then the tasks in/SRC /index.ts are mainly:
/ /... The code is omitted
console.log('Hello, we're in program.'); / / print
schedule.scheduleJob('0 * * * * *'.() = > { // Set a scheduled task
const { year, month, day, hour, minute, second } = getDateDetail(new Date());
console.log(`${year}/${month}/${day} ${hour}:${minute}:${second}`);
});
/ /... The code is omitted
Copy the code
That is, print a line of text and then print year/month/day at the 0th second of every minute: minute: second.
If you’re interested, you can stop and watch the Demo, if you’re not, you can move on.
3. Modify node. js service and add necessary Docker files
Let’s see how to plug the Node.js service into Docker.
- Goal: Build the Node.js environment in Docker and start the service
docker-node
> src
+ .dockerignore
.eslintrc.js
.gitignore
+ Dockerfile
package-lock.json
package.json
README.md
tsconfig.json
Copy the code
We add two files,.dockerignore and Dockerfile, and stuff them:
Dockerignore:
node_modules
.dockerignore
Dockerfile
*-debug.log
*-error.log
.git
.hg
.svn
.vscode
Copy the code
.dockerignore, like.gitignore, ignores files/folders, so Docker will selectively ignore the corresponding files/folders when running Dockerfile.
Dockerfile:
This image is copied from Node V14
FROM node:14
# specify the directory to run CMD, that is, CD to it first
WORKDIR /usr/src/app
Copy the files from the host (currently running terminal) to the app directory in the container
COPY.
Install the NPM package
RUN npm install
# Expose port 80 of the mirror
EXPOSE 80
Start the Node service
CMD ["npm"."run"."robot"];
Copy the code
Dockerfile will tell Docker: You just need to do this, then this, then this…
Detailed I do not tremble, the above notes are very clear.
So next we just need to Show the operation, crazy typing instructions can be!
Docker deploys node.js service
- Create an Image:
Docker image build./ -t docker-node:1.0.0
docker image build
Create an image.. /
: Based on the current directory-t
: Reassigns a pseudo-input terminal to the container, usually with-i
At the same time useDocker -node: 1.0.0
: Indicates the image name and TAG
- Create a Container:
Docker container create -p 3333:80 Docker-node :1.0.0
docker container create
Create a container-p 3333:80
: port,A 3333-80
即Host: container
Docker -node: 1.0.0
: Indicates the image and its TAG
- Start a Container:
docker container start dd420fc4267ad3bdb9eadfdbf37d89e2592dbc9d030a501b96fe10b07ac565ff
docker container start
: Start the containerdd420fc4267ad3bdb9eadfdbf37d89e2592dbc9d030a501b96fe10b07ac565ff
: Container ID, that isdocker ps -a
To find theCONTAINER ID
- Check the running status of a Container:
docker ps -a
- View the Container logs:
docker logs -f dd420fc4267a
dd420fc4267a
: container ID, which can be passeddocker ps -a
Find the
- Enter a Container:
docker exec -it dd420fc4267a bash
dd420fc4267a
: container ID, which can be passeddocker ps -a
Find the
- To view
README.md
File:cat -n README.md
cat
The: cat (concatenate) command is used to connect files and print them to the standard output device-n
: Numbers all output lines starting from 1
Five, the summary
Through the above explanation, you must have a general understanding of some instructions of Docker. In the next chapter, we will explain the necessary instructions of Docker newcomers and how to make Hosts isolation and modify the time in the container.
Stay tuned: 05 – Solution & Docker instructions
Do not toss the front end, and what is the difference between salted fish!
Thumbs up/Star if you think the article is good.
If you need to contact jsliang:
- Github
Personal contact information stored in Github home page, welcome to toss ~
Build yourself into a lifelong learning programmer who is eager to explore, play around, and expand your knowledge.
Jsliang’s document library is licensed by Junrong Liang under the Creative Commons Attribution – Non-commercial – Share alike 4.0 International License. Based on the github.com/LiangJunron… On the creation of works. Outside of this license agreement authorized access can be from creativecommons.org/licenses/by… Obtained.