Docker部署 nodejs项目应用 二 :运行sampleWeb,测试docker/VMare 环境

在上一章,我们成功的在Vmare CentOS里安装好了Docker.

下面我们启动一个Sample web来测试环境是否ok.


一:启动Sample HelloWorld Docker镜像

1.拉取镜像 sudo docker pull training/webapp 等待镜像下载完,然后查看镜像。

[root@franklocalhost ~]# sudo docker pull training/webapp
Using default tag: latest
latest: Pulling from training/webapp
Image docker.io/training/webapp:latest uses outdated schema1 manifest format. Please upgrade to a schema2 image for better future compatibility. More information at https://docs.docker.com/registry/spec/deprecated-schema-v1/
e190868d63f8: Pull complete 
909cd34c6fd7: Pull complete 
0b9bfabab7c1: Pull complete 
a3ed95caeb02: Pull complete 
10bbbc0fc0ff: Pull complete 
fca59b508e9f: Pull complete 
e7ae2541b15b: Pull complete 
9dd97ef58ce9: Pull complete 
a4c1b0cb7af7: Pull complete 
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status: Downloaded newer image for training/webapp:latest
docker.io/training/webapp:latest

2.启动上面的项目。 sudo docker run -d -P training/webapp python app.py

[root@franklocalhost ~]#  sudo docker run -d -P training/webapp python app.py
WARNING: IPv4 forwarding is disabled. Networking will not work.
1ff4bae6e207a8078b16b526ca17aa13e39d266ee8e191a7a3cefe8e8d017c09
[root@franklocalhost ~]# ^C

3.使用sudo docker ps 查看当前正在运行的容器。

[root@franklocalhost ~]# sudo docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS                                                                                               NAMES
1ff4bae6e207   training/webapp       "python app.py"          32 seconds ago   Up 31 seconds   0.0.0.0:49154->5000/tcp, :::49154->5000/tcp                                                         nervous_goldstine

4.查看Ubuntu的ip。使用命令:ifconfig -a    如果查看ip失败。根据提示安装。然后再查看ip

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.48.129  netmask 255.255.255.0  broadcast 192.168.48.255
        inet6 fe80::9a1:5721:ed7:e254  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:32:47:fd  txqueuelen 1000  (Ethernet)
        RX packets 99935  bytes 144142052 (137.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7335  bytes 1443553 (1.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

5.在外面的浏览器访问:http://192.168.48.129:49154/

 

自此简单的Helloworld实现成功!

8.我们也可以通过 -p 参数来设置不一样的端口:

sudo docker run -d -p 5000:5000 training/webapp python app.py

9.终止web容器 使用sudo docker stop 'names' or '容器ID'

二:启动Sample HelloWorld Nodejs Docker镜像

1.制作一个Nodjs的简单web. 确保Nodejs web 在本地可以正常运行。

 

C:\Users\Frank\git\Auto-weighting-feeder-monitor-system\src\main\serverHttp\myApp>npm run start

> myapp@0.0.0 start C:\Users\Frank\git\Auto-weighting-feeder-monitor-system\src\main\serverHttp\myApp
> nodemon ./bin/www

[nodemon] 2.0.13
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json  
[nodemon] starting `node ./bin/www`

2.制作Dockerfile

#设置基础镜像,如果本地没有该镜像,会从Docker.io服务器pull镜像
FROM node
 
#创建app目录,保存我们的代码
RUN mkdir -p /usr/src/node
#设置工作目录
WORKDIR /usr/src/node
 
#复制所有文件到 工作目录。
COPY . /usr/src/node
 
#编译运行node项目,使用npm安装程序的所有依赖,利用taobao的npm安装
 
WORKDIR /usr/src/node/website
RUN npm install --registry=https://registry.npm.taobao.org
RUN npm install nodemon

#暴露container的端口
EXPOSE 8888
 
#运行命令

CMD ["npm", "start"]

3.复制项目文件和 dockerfile 到Centos 

   1.在Centros新建一个文件夹用来存放nodejs应用代码。

[root@franklocalhost ~]# mkdir node-sample-helloworld2
[root@franklocalhost ~]# cd node-sample-helloworld2
[root@franklocalhost node-sample-helloworld2]# 

   2.复制本地项目文件到文件夹

 4.build Dockerfile: docker build -t sample-nodejs-web:v1 .

[root@franklocalhost node-sample-helloworld2]# docker build -t sample_NodeJs_wel:v1 .
invalid argument "sample_NodeJs_wel:v1" for "-t, --tag" flag: invalid reference format: repository name must be lowercase
See 'docker build --help'.
[root@franklocalhost node-sample-helloworld2]# docker build -t sample-nodejs-web:v1 .
Sending build context to Docker daemon    180MB
Step 1/9 : FROM node
 ---> c4fbd6393264
Step 2/9 : RUN mkdir -p /usr/src/node
 ---> Running in 700da8d3a6c3
Removing intermediate container 700da8d3a6c3
 ---> 79dca1a9c7d9
Step 3/9 : WORKDIR /usr/src/node
 ---> Running in 995738d3ceea
Removing intermediate container 995738d3ceea
 ---> bb56eacd7038
Step 4/9 : COPY . /usr/src/node
 ---> 3be8bccb5d5b
Step 5/9 : WORKDIR /usr/src/node/website
 ---> Running in 9469afd48f4c
Removing intermediate container 9469afd48f4c
 ---> aae9fb259440
Step 6/9 : RUN npm install --registry=https://registry.npm.taobao.org
 ---> Running in 2dedb81ee909
npm WARN old lockfile 
npm WARN old lockfile The package-lock.json file was created with an old version of npm,
npm WARN old lockfile so supplemental metadata must be fetched from the registry.
npm WARN old lockfile 
npm WARN old lockfile This is a one-time fix-up, please be patient...
npm WARN old lockfile 
npm WARN deprecated constantinople@3.0.2: Please update to at least constantinople 3.1.1
npm WARN deprecated transformers@2.1.0: Deprecated, use jstransformer
npm WARN deprecated jade@1.11.0: Jade has been renamed to pug, please install the latest version of pug instead of jade

added 100 packages in 11s
npm notice 
npm notice New minor version of npm available! 8.0.0 -> 8.1.0
npm notice Changelog: <https://github.com/npm/cli/releases/tag/v8.1.0>
npm notice Run `npm install -g npm@8.1.0` to update!
npm notice 
Removing intermediate container 2dedb81ee909
 ---> bc82d10bc031
Step 7/9 : RUN npm install nodemon
 ---> Running in e4b3b40585f8

5.查看docker 镜像列表。

oot@franklocalhost node-sample-helloworld2]# docker images
REPOSITORY          TAG       IMAGE ID       CREATED          SIZE
sample-nodejs-web   v1        da0dd602d7f2   49 seconds ago   1.1GB

6.启动sample-nodejs-web 容器

docker run -d -p 8000:8000 sample-nodejs-web:v1

查看容器列表,看到sample-nodejs-web 已经启动。

[root@franklocalhost node-sample-helloworld2]# docker run -d -p 8000:8000 sample-nodejs-web:v1
79b9d87f5d3dde50935762d5714e37966e157a24a6d4ebcb589cdb1635537501
[root@franklocalhost node-sample-helloworld2]# 
[root@franklocalhost node-sample-helloworld2]# docker ps -a
CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS                     PORTS                                                 NAMES
79b9d87f5d3d   sample-nodejs-web:v1   "docker-entrypoint.s猞"   29 seconds ago   Up 28 seconds              0.0.0.0:8000->8000/tcp, :::8000->8000/tcp, 8888/tcp   beautiful_franklin
1ff4bae6e207   training/webapp        "python app.py"          2 hours ago      Up About an hour           0.0.0.0:49153->5000/tcp, :::49153->5000/tcp           nervous_goldstine

7.查看,网址 http://192.168.48.129:8000/

至此一个简单的nodejs web应用以docker运行起来。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值