gpt-4.1-mini-2025-04-14· OpenAI LMArena Human Preference 140K
用Python写一个在线命令生成的网页,主要是docker相关的,包括镜像、端口映射(支持多个,包括任何TCP、UDP协议、)…
- User
用Python写一个在线命令生成的网页,主要是docker相关的,包括镜像、端口映射(支持多个,包括任何TCP、UDP协议、) 、自定义环境变量(支持多个)、重启策略、容器名、挂载卷(支持多个)、以及Host域名映射(域名:IP)
- Assistant
当然可以!下面是一个用Python的Flask框架写的简单网页应用示例,用户可以在线填写Docker相关参数,生成对应的
docker run命令。支持:- 镜像名
- 多个端口映射(支持TCP/UDP)
- 多个环境变量
- 重启策略
- 容器名
- 多个挂载卷
- Host域名映射(域名:IP)
代码示例
from flask import Flask, render_template_string, request app = Flask(__name__) HTML = ''' <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Docker Run 命令生成器</title> <style> body { font-family: Arial, sans-serif; margin: 20px; } label { display: block; margin-top: 10px; } input[type=text], select { width: 300px; padding: 5px; } .inline { display: inline-block; margin-right: 10px; } .section { margin-bottom: 20px; padding: 10px; border: 1px solid #ccc; } button { margin-top: 10px; } textarea { width: 100%; height: 100px; } </style> <script> function addPortMapping() { const container = document.getElementById('port-mappings'); const div = document.createElement('div'); div.innerHTML = ` <input type="text" name="host_port" placeholder="Host端口" size="6" required> <input type="text" name="container_port" placeholder="容器端口" size="6" required> <select name="protocol"> <option value="tcp">TCP</option> <option value="udp">UDP</option> </select> <button type="button" onclick="this.parentNode.remove()">删除</button> `; container.appendChild(div); } function addEnvVar() { const container = document.getElementById('env-vars'); const div = document.createElement('div'); div.innerHTML = ` <input type="text" name="env_key" placeholder="变量名" required> = <input type="text" name="env_value" placeholder="变量值" required> <button type="button" onclick="this.parentNode.remove()">删除</button> `; container.appendChild(div); } function addVolume() { const container = document.getElementById('volumes'); const div = document.createElement('div'); div.innerHTML = ` <input type="text" name="host_volume" placeholder="宿主机路径" size="30" required> : <input type="text" name="container_volume" placeholder="容器路径" size="30" required> <button type="button" onclick="this.parentNode.remove()">删除</button> `; container.appendChild(div); } function addHostMapping() { const container = document.getElementById('host-mappings'); const div = document.createElement('div'); div.innerHTML = ` <input type="text" name="host_domain" placeholder="域名" required> : <input type="text" name="host_ip" placeholder="IP地址" required> <button type="button" onclick="this.parentNode.remove()">删除</button> `; container.appendChild(div); } </script> </head> <body> <h1>Docker Run 命令生成器</h1> <form method="post"> <label>镜像名: <input type="text" name="image" required placeholder="例如 nginx:latest"> </label> <div class="section"> <label>端口映射 (HostPort:ContainerPort Protocol):</label> <div id="port-mappings"></div> <button type="button" onclick="addPortMapping()">添加端口映射</button> </div> <div class="section"> <label>环境变量 (KEY=VALUE):</label> <div id="env-vars"></div> <button type="button" onclick="addEnvVar()">添加环境变量</button> </div> <label>重启策略: <select name="restart_policy"> <option value="">无</option> <option value="no">no</option> <option value="on-failure">on-failure</option> <option value="always">always</option> <option value="unless-stopped">unless-stopped</option> </select> </label> <label>容器名: <input type="text" name="container_name" placeholder="可选"> </label> <div class="section"> <label>挂载卷 (HostPath:ContainerPath):</label> <div id="volumes"></div> <button type="button" onclick="addVolume()">添加挂载卷</button> </div> <div class="section"> <label>Host域名映射 (域名:IP):</label> <div id="host-mappings"></div> <button type="button" onclick="addHostMapping()">添加Host映射</button> </div> <button type="submit">生成命令</button> </form> {% if command %} <h2>生成的 Docker Run 命令</h2> <textarea readonly>{{ command }}</textarea> {% endif %} </body> </html> ''' @app.route('/', methods=['GET', 'POST']) def index(): command = None if request.method == 'POST': image = request.form.get('image').strip() restart_policy = request.form.get('restart_policy', '').strip() container_name = request.form.get('container_name', '').strip() # 端口映射 host_ports = request.form.getlist('host_port') container_ports = request.form.getlist('container_port') protocols = request.form.getlist('protocol') port_mappings = [] for h, c, p in zip(host_ports, container_ports, protocols): if h and c and p: port_mappings.append(f"-p {h}:{c}/{p}") # 环境变量 env_keys = request.form.getlist('env_key') env_values = request.form.getlist('env_value') env_vars = [] for k, v in zip(env_keys, env_values): if k and v: env_vars.append(f"-e {k}={v}") # 挂载卷 host_volumes = request.form.getlist('host_volume') container_volumes = request.form.getlist('container_volume') volumes = [] for h, c in zip(host_volumes, container_volumes): if h and c: volumes.append(f"-v {h}:{c}") # Host域名映射 host_domains = request.form.getlist('host_domain') host_ips = request.form.getlist('host_ip') hosts = [] for d, ip in zip(host_domains, host_ips): if d and ip: hosts.append(f"--add-host {d}:{ip}") parts = ["docker run -d"] if container_name: parts.append(f"--name {container_name}") if restart_policy: parts.append(f"--restart {restart_policy}") parts.extend(port_mappings) parts.extend(env_vars) parts.extend(volumes) parts.extend(hosts) parts.append(image) command = " ".join(parts) return render_template_string(HTML, command=command) if __name__ == '__main__': app.run(debug=True)
使用说明
- 安装依赖:
pip install flask- 运行脚本:
python app.py- 打开浏览器访问
http://127.0.0.1:5000/,填写参数,点击“生成命令”即可看到对应的docker run命令。
功能说明
- 支持动态添加多个端口映射、环境变量、挂载卷和Host域名映射。
- 支持TCP和UDP协议端口映射。
- 支持设置重启策略和容器名。
- 生成的命令格式清晰,方便复制粘贴使用。
如果你需要更复杂的功能(比如验证输入格式、支持更多Docker参数等),可以在此基础上扩展。需要我帮忙也可以告诉我!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.