docker封装案例

  • docker commit
docker run -it docker.io/centos /bin/bash
yum -y install vim net-tools psmiscl iproute epel-release
docker commit 容器id myos:latest 
  • docker build sshd
FROM myos:latest
RUN yum -y install openssh-server initscripts
RUN  ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
RUN sed -i 's/^#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config 
RUN echo 'root:0' |chpasswd
ENV EnvironmentFile=/etc/sysconfig/sshd
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]
docker build -t myos:sshd .
docker run -itd myos:sshd
  • docker build sshd centos8
From centos:v1
RUN yum -y install openssh-server initscripts
RUN ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
RUN ssh-keygen -t ecdsa  -f /etc/ssh/ssh_host_ecdsa_key -N ''
RUN ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ''
RUN echo "root:0" |chpasswd
ENV EnvironmentFile=/etc/sysconfig/sshd
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]
docker build -t centos:ssh .
docker run -d -h centos-ssh --name centos-ssh -p 40022:22 centos:ssh
docker inspect -f '{{.NetworkSettings.IPAddress}}' e2d
172.17.0.4
ssh localhost -p 40022
  • docker-httpd
FROM myos:latest
RUN yum -y install httpd
ENV EnvironmentFile=/etc/sysconfig/httpd
EXPOSE 80
WORKDIR /var/www/html
ADD index.html index.html
CMD ["/usr/sbin/httpd","-DFOREGROUND"]
docker build -t myos:httpd .
docker run -itd myos:httpd
  • docker-httpd+sshd
FROM myos:sshd
RUN yum -y install httpd
ENV EnvironmentFile=/etc/sysconfig/httpd
WORKDIR /var/www/html
ADD index.html index.html
EXPOSE 80
EXPOSE 22
ADD run.sh /etc/init.d/run.sh
CMD ["/etc/init.d/run.sh"]
vim run.sh
/usr/sbin/sshd -D &
/usr/sbin/httpd -DFOREGROUND
chmod 755 run.sh
docker build -t myos:test .
docker run -itd myos:test

> docker 封装 prometheus

FROM eeb6ee3f44bd
ADD prometheus /prometheus
RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
WORKDIR /prometheus
ENTRYPOINT ["/prometheus/prometheus"]
CMD  [ "--config.file=/prometheus/prometheus.yml", \
       "--storage.tsdb.path=/prometheus/data", \
       "--web.console.libraries=/prometheus/console_libraries", \
       "--web.enable-lifecycle", \
       "--web.console.templates=/prometheus/consoles" ]

> 封装prometheus监控脚本

FROM python:3.7-alpine
ADD ../test/alert5_prometheus_3.py /opt
RUN pip3 install flask
RUN pip3 install requests
WORKDIR /opt
EXPOSE 10080
MAINTAINER lu.cn
ENV pro_usr=usr pro_remiders=remiders pro_webhook_sec=webhook_sec pro_secret=secret
ENTRYPOINT /usr/local/bin/python alert5_prometheus_3.py $usr $remiders $webhook_sec $secret

菜鸟教程链接:https://www.runoob.com/docker/docker-dockerfile.html
env环境变量,格式如下
ENV
ENV = =
使用 key=value方式调用的话,在引用中要输入value

环境变量详解
usr: prometheus地址
remiders: 手机号
webhook_sec: 钉钉机器人的webhook,token地址
secret: 钉钉机器人密钥

构建docker镜像
> docker build -t prometheus_dt:latest .

> docker-compose.yaml启动

version: "2.2"
services:
  prometheus_dingtalk:
    build:
      context: ./
      dockerfile: Dockerfile
    image: prometheus_dt:v3_2
    container_name: pro_v3_2
    ports:
      - 10083:10080
    environment:
      usr: http://10.0.4.2:9090/ # prometheus地址
      remiders: 150xxx # @联系人
      webhook_sec: xxx # 钉钉机器人token
      secret: xxx # 钉钉机器人密钥

你可能感兴趣的:(docker,docker,容器,运维)