k3s(一)

前提:需要提前安装node环境和需要的依赖包express。如果 npm install 安装很慢,可以尝试以下几个方法来加快速度:

使用镜像源:使用国内镜像源(如淘宝镜像)来提高下载速度。可以使用以下命令更改源:

npm config set registry https://registry.npmmirror.com/
npm install express

要制作一个简单的 hello world 应用程序并通过 Docker 部署它,我们可以选择使用 Node.js 作为应用程序的运行环境。

1. 创建 Node.js 应用

首先,创建一个简单的 Node.js 应用程序。下面是 Node.js 应用的代码示例。

目录结构:

hello-world/
├── Dockerfile
├── package.json
└── index.js

package.json 文件:

{
  "name": "hello-world",
  "version": "1.0.0",
  "description": "A simple hello world app",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "express": "^4.19.2"
  }
}

index.js 文件:

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello, World!');
});

app.listen(port, () => {
  console.log(`Server is running on http://192.168.110.45:${port}`);
});

2. 创建 Dockerfile 文件

Dockerfile 文件

# 使用官方 Node.js 运行时作为基础镜像
FROM node:latest

# 设置工作目录
WORKDIR /usr/src/app

# 复制 package.json 和 package-lock.json(如果有的话)
COPY package*.json ./

# 安装应用依赖
RUN npm install

# 复制应用代码
COPY . .

# 暴露应用所用的端口
EXPOSE 3000

# 定义启动应用的命令
CMD ["npm", "start"]

3. 构建 Docker 镜像

在包含 Dockerfile 的目录下运行以下命令来构建 Docker 镜像:

docker build -t hello-world-app .

4. 运行 Docker 容器

使用下面的命令启动一个 Docker 容器:

docker run -p 3000:3000 hello-world-app

这将会将容器内的 3000 端口映射到主机的 3000 端口。你可以通过访问 http://localhost:3000 来查看 Hello, World! 消息。

5. 访问应用

打开你的网页浏览器,访问 http://192.168.110.45:3000,你应该会看到页面显示 Hello, World!

相关推荐
shenghuiping20011 个月前
windows11 安装 Rancher Desktop
k8s·rancher·k3s
腾讯数据架构师3 个月前
cube studio开源一站式机器学习平台:k3s部署cube-studio
人工智能·机器学习·云原生·开源·mlops·k3s