博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
4 Project Outline
阅读量:3753 次
发布时间:2019-05-22

本文共 6851 字,大约阅读时间需要 22 分钟。

文章目录

1. Project Demo

Here we outline a project of NodeJS,which we are going deploy to Docker ,and connect it with the explorer in our local machine.And to make this project impressive,we intendedly add some mistakes,which ,I bet ,you will make whatsoever.

在这里插入图片描述

2. Do some preparation

Basically,we have two texts for preparation.

  1. Create a new file called package.json and copy paste the following into it:
{  "dependencies": {    "express": "*"  },  "scripts": {    "start": "node index.js"  }}
  1. Create a new file called index.js and copy paste the following into it:
const express = require('express');const app = express();app.get('/', (req, res) => {  res.send('How are you doing');});app.listen(8080, () => {  console.log('Listening on port 8080');});
  1. The Dockerfile that defines deps and cmds
# specify a base imageFROM alpine# install depsRUN npm install# default cmdCMD ["npm", "start"]
  1. Make sure you have the directory and file as this structure.
$ ls -a./  ../  Dockerfile  index.js  package.json

3. A Few Planned Errors

Assuming that you have done the preparation as i said in last section, at present,you run docker build . ,soon you find out a error msg:

$ docker build .Sending build context to Docker daemon  4.096kBStep 1/3 : FROM alpine ---> caf27325b298Step 2/3 : RUN npm install ---> Running in 7a06dee511f0/bin/sh: npm: not found   【by author: here comes the error !】The command '/bin/sh -c npm install' returned a non-zero code: 127

4. Base Image Issues

We select our base image based upon the collection of default programs that we need to successfully build our image by default.

In terms of alpine image ,it is a rather small one of only 5 megabytes, and it does not cover the necessary programs including “npm” that we need for real.

The tag of “alpine” tends to indicate that this image is as compact and small as possible,and it should only involves very limited sets of programs. Here we must distinguish the tag of “alpine” from the image of “alpine”.

We are not to use the alpine image in the DockerHub.

# specify a base imageFROM node:alpine# install depsRUN npm install# default cmdCMD ["npm", "start"]

Soon we find glitches again:

在这里插入图片描述
The configuration file of package.json is NOT in the container at all ,and it is outside of it , so during step2/3 of running npm install, the node in the container is just not able to find the package.json file,thus throwing an error message .It is a matter of communication stuff.

5. Copying Build Files

We need a copy cmd to copy necessary files from outside the container into it ,and after that, rebuild the container .

# specify a base imageFROM node:alpine# install depsCOPY ./ ./   RUN npm install# default cmdCMD ["npm", "start"]

About the ‘copy’ cmd:

在这里插入图片描述
We’ll look into the ‘copy’ line:

  • the first ./ means the working directory where you run the build cmd ,i.e. the one in which you place the package.json , index.js, etc.
  • the second ./ refers the working directory where, inside the container, the project is really build.

Flip over back to the terminal ,rebuild the project and see what happens.

It’s good that you finally successfully build the project. Even though i presume that you are already aware of building and running the image,yet i still post the code snippet down here.

docker build .docker run ContainerID

or :

we use tag to flag the container.

docker build -t justinwins/simpleweb .  【there's a dot at the end of the line】docker run  justinwins/simpleweb

When you see:

> @ start /> node index.jsListening on port 8080

Congratulations!You’ve made it!

6. Container Port Mapping

Unfortunately, when you open up your brower ,and attempt to visit localhost:8080 ,you only get an error page.That’s wierd,isn’t it?

Now I list the cmds that may interest you:

$ winpty docker run -it e92b580f4f9b sh/ # lsDockerfile         etc                lib                node_modules       package.json       run                sys                varbin                home               media              opt                proc               sbin               tmpdev                index.js           mnt                package-lock.json  root               srv                usr/ #

You will find that package.json , index.js ,etc have been copied into the root directory.That is not the recommeneded practice ,as we usually place our own application into /usr/ or /home directory. However ,there is no tremendous difference at all ,as long as you are not to place custome apps into the root directory .It is adivised that you refer to FHS on Linux for a little bit help.

So,we introduce the environment variable:WORKDIR ,which ,by analogy ,looks like PATH, LANG,etc in Bash.There we go.

在这里插入图片描述
That is really a brief diagram, and i presume that you get it inside out.
We then rewrite the Docker file,and rebuid the project ,
First of all, the Docker file :

# specify a base imageFROM node:alpine# FROM node:8.15.0-alpine# specify a workdirWORKDIR /usr/app# install depsCOPY ./ ./RUN npm install# default cmdCMD ["npm", "start"]

Then , build the project:

Terminal1:$ docker build -t justinwins/simpleweb . 【.  is assigned by WORKDIR --> /usr/app】Sending build context to Docker daemon  4.096kBStep 1/5 : FROM node:alpine ---> ebbf98230a82Step 2/5 : WORKDIR /usr/app  【here ,the WORKDIR is reassigned 】 ---> Running in 0a348b56e1c4Removing intermediate container 0a348b56e1c4 ---> 2701c9499413Step 3/5 : COPY ./ ./ ---> 9d5b5c9d4dc2Step 4/5 : RUN npm install.......(too long to list in detail)$ docker build -t justinwins/simpleweb .$ winpty docker run -p 8080:8080 a8cef003d48d> @ start /usr/app> node index.jsListening on port 8080【Here you refresh your brower page,and see what happens.】Terminal2:$ winpty docker run -it a8cef003d48d sh/usr/app # lsDockerfile         index.js           node_modules       package-lock.json  package.json/usr/app #【Here you find that the work directory has been changed to /usr/app ,instead of / 】

7. Unnecessary Rebuilds

If we modify the index.js,say ,just one line of it ,we still need to rebuild the entire project ,and that may take a few seconds. But when it comes to the real project for productivity ,it may taker a few minutes or longer.

However, it is not necessary to rebuild the whole project,since we do not change the dependencies.

# specify a base imageFROM node:alpine# FROM node:8.15.0-alpine# specify a workdir# WORKDIR /usr/app# install depsCOPY ./package.json ./    【keep the necessary files miminum,thus using cache as much as possible 】RUN npm installCOPY ./ ./     【overwrite the whole WORKDIR 】# default cmdCMD ["npm", "start"]

8. Conclusions

Here are the cmd list mentioned before .

docker build -t repo-name/project-name  . 【build the project with a tag attached, t   is short for 'tag'】docker run -it ContainerID sh【run the container with the shell open】docker exec -it ContainerID sh【open the shell terminal within the Container】

转载地址:http://uknsn.baihongyu.com/

你可能感兴趣的文章
linux系统的网络桥接配置及链路聚合
查看>>
关于DNS部署
查看>>
类的内存模型(二)
查看>>
生产者消费者模型
查看>>
#剑指offer Day2 一类可以用“框架”快速搞定的二叉树问题
查看>>
#剑指offer Day3 一类 “ 斐波那契 ”问题
查看>>
#剑指offer Day4 一类 “ 双指针 ”问题
查看>>
#剑指offer Day5 # 分享两个题的其他解法
查看>>
缓存淘汰算法的实现与应用介绍(LRU,LFU)
查看>>
JZ15. 反转链表
查看>>
1. 两数之和
查看>>
2. 两数相加
查看>>
JZ1.二维数组的查找
查看>>
String 类
查看>>
什么是接口
查看>>
Java高级篇之进程
查看>>
类加载机制
查看>>
了解jdk1.8版本一些新的特性
查看>>
Java高级篇之网络通讯
查看>>
浅谈篇之线程池
查看>>