3月6日 docker 镜像构建

课程价值

学习如何使用dockerfile制作镜像

课程大纲

  1. docker commit和dockerfile的使用
  2. 多from指令的使用方式
  3. 分层镜像系统的原理

时长

1.5h

预习内容:

dockerfile 语法

命令

docker run --name=prometheus -d -p 9090:9090 -v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

课程价值

学习如何使用dockerfile制作镜像

课程大纲

  1. docker commit和dockerfile的使用
  2. 多from指令的使用方式
  3. 分层镜像系统的原理

时长

1.5h

预习内容:

dockerfile 语法

命令

docker run --name=prometheus -d -p 9090:9090 -v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

docker run --name grafana -d -p 8000:3000 grafana/grafana

global:
  scrape_interval:     15s
  evaluation_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']
  - job_name: 'node_exporter'
    static_configs:
    - targets: ['k8s.testing-studio.com:9100']
  - job_name: 'mysql'
    static_configs:
    - targets: ['k8s.testing-studio.com:9104']
  - job_name: 'cadvisor'
    static_configs:
    - targets: ['k8s.testing-studio.com:8091']

课堂作业

编写一个基于后端服务的dockerfile
代码地址:https://github.com/ycwdaaaa/holmes.git

答案:
dockerfile:

FROM centos:7

ADD entrypoint.sh /root
ADD requirements.txt /root
WORKDIR /root
USER root

RUN yum install -y epel-release \
    && yum install -y python34 git python34-setuptools python34-devel.x86_64 \
    && easy_install-3.4 pip \
    && yum install -y gcc \
    && yum install -y soci-mysql-devel.x86_64 \
    && ln -s /usr/local/mysql/bin/mysql /usr/bin \
    && pip3 install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/


ENTRYPOINT ["/root/entrypoint.sh"]

启动脚本:

#!/bin/bash

mkdir -p /opt/web
cd /opt/web
git clone https://github.com/ycwdaaaa/holmes.git
cd holmes
pip3 install -r requirement.txt -i https://mirrors.aliyun.com/pypi/simple/
/usr/bin/python3.4 app.py


while true
do
   sleep 10
done

搭建镜像仓库:

docker pull registry:2
docker run -d -p 5000:5000 -v /usr/local/registry:/var/lib/registry --restart=always --name registry registry:2
docker pull busybox
docker tag busybox localhost:5000/bosybox:v1.0
docker push localhost:5000/bosybox:v1.0
curl http://localhost:5000/v2/_catalog

多FROM指令

FROM centos:6.9 as builder

# 下载基础工具
RUN echo "this is a test" > gaofei.tar


FROM busybox

WORKDIR /root

COPY --from=builder gaofei.tar .