Docker can package the runtime environment and code into an image, and run the image directly when using it, which can avoid the trouble of deployment on different machines. Take a look at the basic use of Docker today. Package a node with docker

The node code is as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async ctx => {
  ctx.body = 'Hello World';
});

app.listen(3333);
Copy the code

Then create a dockerfile in the root directory as follows:

FROM node
COPY.
RUN npm install
EXPOSE 3333
CMD ["npx"."pm2"."-n"."main"."restart"."/src/index.js"]
Copy the code

My steps are simple: use node images to provide the Node environment, and then copy the contents of the current directory into the docker container. I understand containers as empty directories using the local Linux kernel. So when I copy it, the directory structure inside the container is the same as that of my project, which is as follows:

Node - test ├ ─ dockerfile ├ ─ package - lock. Json ├ ─ package. The json ├ ─ SRC | └ index, jsCopy the code

Node-test is equivalent to the container root directory, so the next step is to install dependencies directly in the container root directory. There is a port 3333 exposed, because listen is port 3333, which is easy to map to external ports. Run the NPX pm2 -n main restart ‘/ SRC /index.js’ command.

Use the docker build -t node-test command to pack the image, -t is used to name the image, and the last one. Used to tell the location of a dockerfile. No error was reported after the command was run, then I used Docker images to check, as follows:

REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
node-test    latest    b0cd73088ae0   27 minutes ago   963MB
node         latest    1db64f55f800   3 days ago       936MB
Copy the code

Node is the image I downloaded and Node-test is just typed out. Looks like we’re close, huh? Docker run node-test: / SRC /index.js does not exist. / SRC /index.js does not exist. Dockerfile: [‘ls’, ‘-al’]

total 240
drwxr-xr-x   1 root root   4096 Jan 24 14:19 .
drwxr-xr-x   1 root root   4096 Jan 24 14:19 ..
-rwxr-xr-x   1 root root      0 Jan 24 14:19 .dockerenv
drwxr-xr-x   1 root root   4096 Jan 12 04:01 bin
drwxr-xr-x   2 root root   4096 Jul 10  2020 boot
drwxr-xr-x   5 root root    340 Jan 24 14:19 dev
-rw-r--r--   1 root root    129 Jan 24 14:18 dockerfile
drwxr-xr-x   1 root root   4096 Jan 24 14:19 etc
drwxr-xr-x   1 root root   4096 Jan 12 10:30 home
drwxr-xr-x   1 root root   4096 Jan 12 04:01 lib
drwxr-xr-x   2 root root   4096 Jan 11 00:00 lib64
drwxr-xr-x   2 root root   4096 Jan 11 00:00 media
drwxr-xr-x   2 root root   4096 Jan 11 00:00 mnt
drwxr-xr-x   1 root root   4096 Jan 24 14:19 node_modules
drwxr-xr-x   1 root root   4096 Jan 20 20:26 opt
-rw-r--r--   1 root root 154255 Jan 24 14:19 package-lock.json
-rw-r--r--   1 root root    290 Jan 24 14:19 package.json
dr-xr-xr-x 110 root root      0 Jan 24 14:19 proc
drwx------   1 root root   4096 Jan 24 14:19 root
drwxr-xr-x   3 root root   4096 Jan 11 00:00 run
drwxr-xr-x   1 root root   4096 Jan 12 04:00 sbin
drwxr-xr-x   2 root root   4096 Jan 24 13:45 src
drwxr-xr-x   2 root root   4096 Jan 11 00:00 srv
dr-xr-xr-x  13 root root      0 Jan 24 14:19 sys
drwxrwxrwt   1 root root   4096 Jan 20 20:26 tmp
drwxr-xr-x   1 root root   4096 Jan 11 00:00 usr
drwxr-xr-x   1 root root   4096 Jan 11 00:00 var
Copy the code

As you can see, Docker still adds some familiar folders, such as /var, /bin, etc. However, the COPY command works exactly as I expected. The contents of the project are copied directly to the root directory of the container. Pm2 should have used the start command. Docker run -p 8000:3333 node-test docker run -p 8000:3333 node-test docker run -p 8000:3333 Node-test I use docker PS to check the docker process running, found no. So pM2 is dead. Baidu provides several methods, the only thing I can understand is to add a tail -f command to the end of CMD, so that it will never finish executing. So I changed it to this:

FROM node
COPY . .
RUN npm install
EXPOSE 3333
CMD npx pm2 -n main start /src/index.js && npx pm2 logs main
Copy the code

Run successfully, refresh the browser port 8000, you can see the normal output of Hello World. And then run.

To make sure you really don’t depend on the local environment. I changed the name of the local NPX to the following:

➜ bin CD /usr/local/bin ➜ bin ls 2to3 git kubectl.docker python3-32 2to3-3.7 git-credential-osxkeychain node Python3-config MotionPro git-cvsserver notary python3.7-com.docker. cli git-shell NPM python3.7-32 docker gitk NPX Python3.7-config docker-compose hub-tool pip3 python3.7m docker-credential-desktop Hyperkit pip3.7 python3.7m-config Docker-credential-ecr-login idle3 pyDoc3 Pyvenv Docker-credential-osxkeychain idle3.7 pyDoc3.7 pyvenv-3.7 Easy_install-3.7 kubectl python3 VpnKit ➜ bin NPX -v 6.14.5 ➜ bin mv NPX npx2 ➜ bin NPX -v ZSH: Command not found: NPXCopy the code

Then stop the container and run again, still fine!

Running containers can still go in, just like normal terminals.

➜ docker docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9071e2FA8c20 node-test "docker-entryPoint.s..." 39 seconds ago Up 38 seconds 0.0.0.0:8000->3333/ TCP upbeat_chatelet ➜ docker docker exec -it 9071 sh # NPX pm2 list ┌ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │ id name │ namespace │ version │ mode │ │ pid uptime │ ↺ │ status │ │ CPU mem │ user │ watching │ ├ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ───┤ │ 0 │ main │ default │ N/A │ fork │ 44 │ 72s │ 0 │ online │ 0% │ root │ disabled │ └ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ #Copy the code

A set of demo down for 3 hours, I feel quite simple to start with, more research when free