使用docker创建一个简单的Node.js应用
Posted 玛卡巴卡巴巴亚卡
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用docker创建一个简单的Node.js应用相关的知识,希望对你有一定的参考价值。
1、编写app.js代码
const http = require('http');
const os = require('os');
console.log("Kubia server starting...");
var handler = function(request, response)
console.log("Received request from "+ request.connection.remoteAddress);
response.writeHead(200);
response.end("You've hit "+ os.hostname() + "\\n");
;
var www = http.createServer(handler);
www.listen(8080);
在8080端口启动一个http服务器,服务器会以状态码200和文字you've hit hostname 响应每个请求,请求handler会把客户端ip打印到标准输出。
2、为镜像创建Dockfile
为了把应用打包成镜像,需要创建Dockerfile文件,包含一个系列构建镜像时需要执行的指令。Dockerfile和app.js在同一目录。
具体命令如下:
FROM node:7
ADD app.js ./app.js
ENTRYPOINT ["node","app.js"]
FROM定义镜像起始内容(基础镜像为node镜像的tag7版本),第二行把app.js文件从本地文件夹添加到镜像根目录,保持文件名。最后一行是当镜像运行时执行的指令。
3、构建容器镜像
sudo docker build -t kubia .
基于当前文件夹创建kubia镜像,docker根据dockerfile构建镜像。
镜像是分层的,不同镜像可以共享分层,如果创建了多个基于相同基础镜像的镜像,基础镜像的分层只会被存储一次。所以当拉取镜像的时候,docker只会下载没被存储的分层。
查看本地镜像
sudo cocker images
4、运行docker镜像
sudo docker run --name kubia-container -p 8080:8080 -d kubia
告知docker基于kubia镜像创建一个叫kubia-container的新容器,-d表示在后台运行,本机8080端口映射容器内8080端口
访问应用:
10.108.129.151是使用ubuntu的ifconfig查找到的IP地址
也可以用localhost:8080访问
使用
sudo docker ps
可以查看运行中的容器
使用docker inspect可以查看更多信息
sudo docker inspect kubia-container
[
"Id": "0adadc5ba3378355412bd8ecaed1ab10ba2257463ff6dae1a0856acc10df0170",
"Created": "2021-08-06T07:13:10.545943778Z",
"Path": "node",
"Args": [
"app.js"
],
"State":
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 3734,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-08-06T07:13:11.336658169Z",
"FinishedAt": "0001-01-01T00:00:00Z"
,
"Image": "sha256:0cf6b9e525daf72adbace7ed01b0609d09d1a62bf1bfd460baabf86a44b19772",
"ResolvConfPath": "/var/snap/docker/common/var-lib-docker/containers/0adadc5ba3378355412bd8ecaed1ab10ba2257463ff6dae1a0856acc10df0170/resolv.conf",
"HostnamePath": "/var/snap/docker/common/var-lib-docker/containers/0adadc5ba3378355412bd8ecaed1ab10ba2257463ff6dae1a0856acc10df0170/hostname",
"HostsPath": "/var/snap/docker/common/var-lib-docker/containers/0adadc5ba3378355412bd8ecaed1ab10ba2257463ff6dae1a0856acc10df0170/hosts",
"LogPath": "/var/snap/docker/common/var-lib-docker/containers/0adadc5ba3378355412bd8ecaed1ab10ba2257463ff6dae1a0856acc10df0170/0adadc5ba3378355412bd8ecaed1ab10ba2257463ff6dae1a0856acc10df0170-json.log",
"Name": "/kubia-container",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"ExecIDs": null,
"HostConfig":
"Binds": null,
"ContainerIDFile": "",
"LogConfig":
"Type": "json-file",
"Config":
,
"NetworkMode": "default",
"PortBindings":
"8080/tcp": [
"HostIp": "",
"HostPort": "8080"
]
,
"RestartPolicy":
"Name": "no",
"MaximumRetryCount": 0
,
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Capabilities": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
,
"GraphDriver":
"Data":
"LowerDir": "/var/snap/docker/common/var-lib-docker/overlay2/843ed8a5c1cdf295f77bcd902f76d2e120bb6de3616ffa1b0801e50008ba7ccb-init/diff:/var/snap/docker/common/var-lib-docker/overlay2/70e335ec6fa108100b4331284b442c34a3e3ec7b28868b84cd3a174e2fe08608/diff:/var/snap/docker/common/var-lib-docker/overlay2/842de6190f64393edccfe9f4573f679215b9dfc074ae6fe07146eb77df52c7db/diff:/var/snap/docker/common/var-lib-docker/overlay2/3bab2383c47645b2028340a0e3c8023c1b35019b219599918e22dee00375c557/diff:/var/snap/docker/common/var-lib-docker/overlay2/85b78e977e9a908dc47c8aaf496ca1c7b05d94cd7c86f702b3ed144c4344b952/diff:/var/snap/docker/common/var-lib-docker/overlay2/f4aeff8fba32a6fbdea530d391cc311bc4f4e881b3856563fd6417a53499a98b/diff:/var/snap/docker/common/var-lib-docker/overlay2/1f60045021174d53b9a795e3f7207a21a833865490eba459bf546c4bb457fae1/diff:/var/snap/docker/common/var-lib-docker/overlay2/24f8e1d3315276060ca724ec5496ed0cb232d31149328721d4bb8d1de1071317/diff:/var/snap/docker/common/var-lib-docker/overlay2/8cb193d8c5d35416d15114baa075b59c5660427a477af9db12a7d1bfee32db39/diff:/var/snap/docker/common/var-lib-docker/overlay2/5c497db23a5d68d1351b8464ff17aee1a0e76f15f22bbb061f34392a61426ea3/diff",
"MergedDir": "/var/snap/docker/common/var-lib-docker/overlay2/843ed8a5c1cdf295f77bcd902f76d2e120bb6de3616ffa1b0801e50008ba7ccb/merged",
"UpperDir": "/var/snap/docker/common/var-lib-docker/overlay2/843ed8a5c1cdf295f77bcd902f76d2e120bb6de3616ffa1b0801e50008ba7ccb/diff",
"WorkDir": "/var/snap/docker/common/var-lib-docker/overlay2/843ed8a5c1cdf295f77bcd902f76d2e120bb6de3616ffa1b0801e50008ba7ccb/work"
,
"Name": "overlay2"
,
"Mounts": [],
"Config":
"Hostname": "0adadc5ba337",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts":
"8080/tcp":
,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NPM_CONFIG_LOGLEVEL=info",
"NODE_VERSION=7.10.1",
"YARN_VERSION=0.24.4"
],
"Cmd": null,
"Image": "kubia",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"node",
"app.js"
],
"OnBuild": null,
"Labels":
,
"NetworkSettings":
"Bridge": "",
"SandboxID": "6c5b413286c1307c4474511baf478d3cc5ca977678b0652114b14b232e1659ca",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports":
"8080/tcp": [
"HostIp": "0.0.0.0",
"HostPort": "8080"
]
,
"SandboxKey": "/run/snap.docker/netns/6c5b413286c1",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "a86ade7eb22130a3d8fcd837cb83807ef6c16cfe57063ebdbd3121729da7b9e8",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks":
"bridge":
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "38caa15f01921fd4232d49a7292fe6dffd45dac15df4899abcb43265ff986c14",
"EndpointID": "a86ade7eb22130a3d8fcd837cb83807ef6c16cfe57063ebdbd3121729da7b9e8",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
]
5、在容器内部运行shell
sudo docker exec -it kubia-container bash
列出进程
ps aux
-i:确保输入流开放
-t:分配伪终端
查看运行在主机操作系统上容器的进程
容器拥有完整的文件系统
6、停止和删除容器
sudo docker stop kubia-container
sudo docker rm kubia-container
在停止容器时出现错误
解决方法:
sudo aa-remove-unknown
删除成功
7、向镜像库推送镜像
使用附加标签标注镜像,知道自己的docker hub id后可以重命名镜像,用自己的id代替luksa
docker tag kubia luksa/kubia
docker login #根据个人username和password登录
docker push luksa/kubia
以上是关于使用docker创建一个简单的Node.js应用的主要内容,如果未能解决你的问题,请参考以下文章
Docker - Node.js + MongoDB - “错误:无法连接到 [localhost:27017]”