Windows Docker Desktop容器自动化管理

Windows环境下Docker Desktop的自动化容器管理
本文介绍了如何在Windows Docker Desktop上使用Docker Compose和Dobi工具进行容器自动化管理。通过Docker Compose配置多容器应用,结合Dobi进行高效构建,实现了包括SSH服务、Google Chrome和JetBrains IDEA的容器环境。详细步骤包括Dockerfile的创建、dobi.yaml和docker-compose.yaml的配置,以及如何启动和管理这些容器。

我们使用Docker compose和开源工具Dobi(GitHub - dnephin/dobi: A build automation tool for Docker applications)从一个Linux基础容器镜像(我们使用Almalinux),构建支持SSH两个容器,以及一个具备X11 forwarding的有Google chrome浏览器和Jetbain idea集成开发工具的容器。借鉴如此容器环境构建,可以很方便来搭建基于Windows下容器环境的Hadoop、Spark大数据教学开发的环境。可以参考本作者的另外几篇文章:Windows PC上创建大数据职业技能竞赛实验环境之一 / 七

首先我们来看看什么是Docker compose。Docker compose 是用于定义和运行多容器 Docker 应用程序的工具。通过 Compose,您可以使用 YML 文件来配置应用程序需要的所有服务。然后,使用一个命令,就可以从 YML 文件配置中创建并启动所有服务。Windows下安装Docker Desktop,应该也有了Docker compose。

C:\MyDevz\dockerp>docker version
Client:
 Cloud integration: v1.0.29
 Version:           20.10.21
 API version:       1.41
 Go version:        go1.18.7
 Git commit:        baeda1f
 Built:             Tue Oct 25 18:08:16 2022
 OS/Arch:           windows/amd64
 Context:           default
 Experimental:      true

Server: Docker Desktop 4.14.1 (91661)
 Engine:
  Version:          20.10.21
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.7
  Git commit:       3056208
  Built:            Tue Oct 25 18:00:19 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.9
  GitCommit:        1c90a442489720eec95342e1789ee8a5e1b9536f
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

C:\MyDevz\dockerp>docker-compose version
Docker Compose version v2.13.0

开源工具Dobi是一款Docker应用的自动化构建工具,用Dobi能保持项目高效组织,方便重构。他采用dobi.yaml来具体定义容器构建的资源与任务,并直接用dobi运行任务。Dobi可以通过github下载,具有windows和linux的不同程序。这里是windows下载链接:https://github.com/dnephin/dobi/releases/download/v0.15.0/dobi-windows.exe

一 构建支持SSH的容器镜像

创建项目目录,我的是C:\mydevz\dockerp。进入项目于目录。将下载好的dobi拷贝到目录,修改文件名。

dobi-windows.exe -> dobi.exe

1 在当前目录下创建Dockerfile。

FROM almalinux:latest
MAINTAINER "LIU Gang from Hiroki Takeyama"

# sshd
RUN dnf -y install openssh-server openssh-clients; \
    sed -i 's/^\(UsePAM yes\)/# \1/' /etc/ssh/sshd_config; \
    ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N '' && \
    ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N '' && \
    ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key  -N ''; \
    sed -i 's/^#\(PermitRootLogin\) .*/\1 yes/' /etc/ssh/sshd_config; \
    rm -f /run/nologin; \
    dnf clean all;

# 免密
RUN sed -i 's/^#\(PubkeyAuthentication yes\)/\1/' /etc/ssh/sshd_config && \
		echo "StrictHostKeyChecking no" >> /etc/ssh/ssh_config; \
		ssh-keygen -t rsa -f /root/.ssh/id_rsa -P '' && \
		cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys;
		
# 基础应用
RUN dnf -y install net-tools iproute procps;
RUN dnf -y install bzip2 wget vim less;
RUN dnf clean all;

# 环境变量
ENV TIMEZONE Asia/Shanghai
ENV ROOT_PASSWORD *******   # root登录密码

RUN ln -fs /usr/share/zoneinfo/${TIMEZONE} /etc/localtime; \
    echo "root:${ROOT_PASSWORD}" | chpasswd; \
    touch /var/log/lastlog;

# 暴露ssh端口
EXPOSE 22

ENTRYPOINT ["/usr/sbin/init"]

 在Dockerfile中通过ENTRYPOINT ["/usr/sbin/init"],让容器启动时启动systemd。

 2 创建dobi.yaml。

meta:
  project: dockerp
  default: create
    
    
image=almalinux:
  image: almalinux
  context: .
  dockerfile: Dockerfile
  tags: 
    - 'S0.1'
#    - '{env.VERSION}'
  annotations:
    description: "Build the almalinux image"


alias=create:
    tasks: ['almalinux:build']
    annotations:
        description: "Create the images and containers"
        
alias=clean:
    tasks: ['almalinux:rm']
    annotations:
        description: "Remove the images and containers"

这时我们可以来创建新的容器镜像almalinux:S0.1。

C:\MyDevz\dockerp>dobi --version
dobi version 0.15.0 (build: 5c1481d, date: Sat Feb 27 19:05:16 UTC 2021)

C:\MyDevz\dockerp>dobi list
Resources:
  almalinux            Build the almalinux image
  clean                Remove the images and containers
  create               Create the images and containers

C:\MyDevz\dockerp>dobi
[WARN] Failed to load auth config: no docker configuration found
Step 1/12 : FROM almalinux:latest
 ---> 39f63d416992
Step 2/12 : MAINTAINER "LIU Gang from Hiroki Takeyama"
 ---> Running in ecec068122c7
Removing intermediate container ecec068122c7
 ---> b84502db34ad
Step 3/12 : RUN dnf -y install openssh-server openssh-clients;     sed -i 's/^\(UsePAM yes\)/# \1/' /etc/ssh/sshd_config;     ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N '' &&     ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N '' &&     ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key  -N '';     sed -i 's/^#\(PermitRootLogin\) .*/\1 yes/' /etc/ssh/sshd_config;     rm -f /run/nologin;     dnf clean all;
 ---> Running in 21c50551fb82
AlmaLinux 8 - BaseOS                            1.3 MB/s | 2.9 MB     00:02
AlmaLinux 8 - AppStream                         3.2 MB/s | 9.9 MB     00:03
AlmaLinux 8 - Extras                            7.5 kB/s |  18 kB     00:02
Dependencies resolved.
================================================================================
 Package              Arch        Version                     Repository   Size
================================================================================
Installing:
 openssh-clients      
<think>我们正在处理一个关于在Windows上使用DockerDesktop构建容器化监控系统的请求。根据引用[1],DockerDesktop是一个为开发人员设计的应用程序,它简化了在本地机器上使用Docker和Kubernetes的过程。引用[2]提供了在Windows系统上搭建Docker环境的教程,而引用[3]则展示了在Windows上使用Docker搭建Linux深度学习环境的示例。为了搭建容器化的监控系统,我们通常需要以下组件:1.监控数据收集器(如Prometheus)2.可视化工具(如Grafana)3.告警系统(如Alertmanager,通常与Prometheus一起使用)4.可能需要一个轻量级的Linux容器来运行这些服务(因为Prometheus和Grafana通常运行在Linux环境中)步骤概述:1.确保Windows系统满足DockerDesktop的要求(Windows10以上,开启虚拟化等)。2.安装并配置DockerDesktop。3.使用Docker拉取监控系统所需的镜像(例如Prometheus,Grafana等)。4.编写docker-compose.yml文件来定义和运行多个容器(推荐,因为监控系统通常由多个组件组成)。5.配置各个组件的设置(如Prometheus的配置文件,Grafana的数据源等)。6.启动容器并访问服务。详细步骤:###1.安装DockerDesktop-参考引用[2],在Windows上安装DockerDesktop。确保启用Hyper-V和容器特性(如果使用Windows10专业版或企业版)。对于Windows10家庭版,需要安装DockerToolbox或者升级到支持WSL2的版本(推荐使用WSL2后端,因为性能更好)。-安装完成后,启动DockerDesktop,并在系统托盘中看到Docker图标。###2.准备项目目录-创建一个项目目录,例如`docker-monitoring`,并在其中创建子目录和配置文件。-通常我们需要为Prometheus创建配置文件,为Grafana创建数据源配置等。目录结构示例:```docker-monitoring/├──prometheus/│├──prometheus.yml├──grafana/│└──provisioning/#可选,用于预配置数据源和仪表板│├──dashboards/│├──datasources/└──docker-compose.yml```###3.配置Prometheus在`prometheus`目录下创建`prometheus.yml`文件,基本配置如下:```yamlglobal:scrape_interval:15sscrape_configs:-job_name:'prometheus'static_configs:-targets:['localhost:9090']```这个配置让Prometheus监控自身。###4.配置Grafana(可选)在`grafana/provisioning/datasources`目录下创建`datasource.yml`,用于自动配置Prometheus数据源:```yamlapiVersion:1datasources:-name:Prometheustype:prometheusaccess:proxyurl:http://prometheus:9090isDefault:true```###5.编写docker-compose.yml在项目根目录创建`docker-compose.yml`,内容如下:```yamlversion:'3'services:prometheus:image:prom/prometheus:latestcontainer_name:prometheusports:-"9090:9090"volumes:-./prometheus:/etc/prometheuscommand:--config.file=/etc/prometheus/prometheus.ymlgrafana:image:grafana/grafana:latestcontainer_name:grafanaports:-"3000:3000"volumes:-./grafana/provisioning:/etc/grafana/provisioningenvironment:-GF_SECURITY_ADMIN_PASSWORD=admindepends_on:-prometheus```###6.启动服务在项目目录(有docker-compose.yml的目录)打开PowerShell或命令提示符,运行:```bashdocker-composeup-d```等待镜像拉取和容器启动。###7.访问服务-Prometheus:打开浏览器访问http://localhost:9090-Grafana:访问http://localhost:3000,使用用户名`admin`和密码`admin`登录。###8.配置Grafana(手动)如果未使用预配置,登录Grafana后需要手动添加数据源:1.点击设置(齿轮图标)->DataSources->Adddatasource->选择Prometheus。2.URL填写:http://prometheus:9090(注意:在Docker网络中,使用服务名作为主机名)3.保存并测试。然后可以导入或创建仪表板。###注意事项-确保Windows防火墙允许相关端口(9090,3000等)。-如果需要监控其他容器,需要在Prometheus配置中添加新的job,并确保目标容器暴露了metrics端点。-对于生产环境,需要设置更安全的密码和持久化存储(例如将Grafana的数据目录挂载到宿主机)。以上步骤提供了一个基本的容器化监控系统(Prometheus+Grafana)的搭建方法。§§相关问题§§1.如何配置Prometheus监控其他Docker容器?2.如何在Grafana中创建监控仪表板?3.如何设置告警规则并发送通知?4.如何持久化存储Prometheus和Grafana的数据?</think>### 在Windows系统使用Docker Desktop搭建容器化监控系统 以下是分步实现方案,基于Prometheus + Grafana + Node Exporter的经典监控组合: --- #### 1. **环境准备** - 确认系统要求: - Windows 10/11(64位)专业版/企业版/教育版 - 开启虚拟化(BIOS中启用VT-x/AMD-V) - 至少4GB内存(推荐8GB+) - 安装Docker Desktop: - 从[官网](https://www.docker.com/products/docker-desktop/)下载安装包 - 安装时勾选**启用WSL 2后端**(性能提升关键)[^2] ```powershell # 验证安装成功 docker --version # 输出:Docker version 24.0.7, build 1110ad5 ``` --- #### 2. **创建监控项目目录** ```powershell mkdir docker-monitoring cd docker-monitoring mkdir prometheus grafana ``` --- #### 3. **配置监控组件** **(1) Prometheus配置文件** 创建 `prometheus/prometheus.yml`: ```yaml global: scrape_interval: 15s scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'node-exporter' static_configs: - targets: ['node-exporter:9100'] ``` **(2) Grafana配置文件**(可选) 创建 `grafana/grafana.ini`: ```ini [security] admin_user = admin admin_password = monitor123 ``` --- #### 4. **编写Docker Compose文件** 创建 `docker-compose.yml`: ```yaml version: '3.8' services: prometheus: image: prom/prometheus:latest ports: - "9090:9090" volumes: - ./prometheus:/etc/prometheus command: --config.file=/etc/prometheus/prometheus.yml grafana: image: grafana/grafana:latest ports: - "3000:3000" volumes: - ./grafana:/etc/grafana environment: - GF_SECURITY_ADMIN_PASSWORD=admin123 depends_on: - prometheus node-exporter: image: prom/node-exporter:latest ports: - "9100:9100" command: --path.rootfs=/host volumes: - /:/host:ro,rslave ``` --- #### 5. **启动监控系统** ```powershell docker-compose up -d ``` 验证容器状态: ```powershell docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" ``` 输出示例: ``` NAMES STATUS PORTS docker-monitoring-prometheus Up 2 minutes 0.0.0.0:9090->9090/tcp docker-monitoring-grafana Up 2 minutes 0.0.0.0:3000->3000/tcp docker-monitoring-node-exporter Up 2 minutes 0.0.0.0:9100->9100/tcp ``` --- #### 6. **访问监控服务** | 组件 | 访问地址 | 默认凭证 | |---------------|--------------------------|---------------| | Prometheus | http://localhost:9090 | 无需登录 | | Grafana | http://localhost:3000 | admin/admin123| | Node Exporter | http://localhost:9100/metrics | 无需登录 | --- #### 7. **配置Grafana仪表板** 1. 登录Grafana后添加数据源: - 类型选择 **Prometheus** - URL填写 `http://prometheus:9090` 2. 导入预置仪表板: - 控制台搜索ID **1860**(Node Exporter仪表板) - 或手动创建自定义面板 ![监控仪表板示例](https://grafana.com/static/img/docs/v65/exemplars_node_exporter.png) --- ### 关键配置说明 1. **数据持久化** `volumes` 映射确保配置和数据在容器重启后不丢失 2. **资源监控原理** Node Exporter通过挂载根目录(`/`)收集宿主机指标,Prometheus定期抓取数据 3. **网络配置** Docker Compose自动创建共享网络,容器间通过服务名通信(如 `prometheus:9090`) --- ### 常见问题解决 1. **端口冲突** 修改`docker-compose.yml`中的端口映射,如将`3000:3000`改为`8080:3000` 2. **WSL 2性能优化** 在Docker Desktop设置中: ```json { "wslEngineEnabled": true, "memoryMiB": 8192 // 分配更多内存 } ``` 3. **Windows文件权限** 若遇到volume权限错误,在`docker-compose.yml`中添加: ```yaml grafana: user: "472" # Grafana默认用户ID ``` --- ### 进阶扩展 - **添加监控目标**:在`prometheus.yml`中增加新job监控其他容器 - **告警配置**:集成Alertmanager实现邮件/钉钉告警 - **日志监控**:添加Loki+Promtail日志收集系统 > 提示:生产环境建议配置数据卷持久化存储时序数据库: > `volumes: - prom_data:/prometheus` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值