docker 实战(五) 使用alpine 精简镜像

alpine 镜像使用

  1. alpine 是一个精简的linux镜像,里面仅仅包含了apk(alpine专属的镜像管理工具)以及linux系统内核
  2. alpine镜像甚至连 bash 都没有,不过echo是有的
  3. 使用alpine也很简单
    • docker run -it alpine echo "hello world"
  4. 由于包含的linux软件少,所以体积仅有 5M 大小,使用alpine的思想就是,需要什么软件就安装什么,其他不需要的一概不安装,以维持最小的docker镜像运行服务
  5. Alpine 相关地址
    • Alpine 官网:https://www.alpinelinux.org/
    • Alpine 官方仓库:https://github.com/alpinelinux
    • Alpine 官方镜像:https://hub.docker.com/_/alpine/
    • Alpine 官方镜像仓库:https://github.com/gliderlabs/docker-alpine

使用步骤

  • 简要方法
  1. 查找 dockerHub 或 github 直接查找已经做好的alpine镜像
  2. 使用Dockerfile进行制作
  • 从头开始
  1. 在apk仓库中查找到自己需要的apk进行安装
  2. 组装成相应的apline镜像

报错

ERROR: http://dl-cdn.alpinelinux.org/alpine/v3.4/community: temporary error (try again later)
WARNING: Ignoring APKINDEX.a2e6dac0.tar.gz: No such file or directory
  1. github 上的解决方案
    as pointed out by @azyata its can be a problem with the DNS settings

    If you are performing apk add inside the container, below might be helpful

    From docs.docker.com:

    By default, a container inherits the DNS settings of the Docker daemon
    including the /etc/hosts and /etc/resolv.conf

    Try to ping
    ping dl-cdn.alpinelinux.org
    if it doesn’t work

    Check DNS configuration in /etc/resolve.conf in host machine, try adding
    nameserver 8.8.8.8
    then perform ping

    ping dl-cdn.alpinelinux.org
    if it works for you, then

    Restart the docker daemon
    sudo systemctl restart docker
    ^ This solved the problem for me.

  2. 国内的解决方案

     
    
    #vim /etc/default/docker
    
     
    
    #DOCKER_OPTS="--dns 114.114.114.114"
    
     
    
    #systemctl restart docker
    
  3. 基本上是因为dns服务有问题,换镜像源也是一个不错的方案

python-alpine镜像使用

  1. dockerhub地址 https://hub.docker.com/_/python
  2. 安装 python3.6
    • docker pull python:3.6.8-alpine
  3. 如何找到linux软件依赖的相关apk包
    • 在dockerhub中搜索相应的包
    • 查看其中的dockerfile文件
  4. docker run -it python:3.6.8-alpine python--version
  5. 安装uwsgi
from python:3.6-alpine

# 更新并安装gcc bash
RUN apk --update add gcc linux-headers musl-dev bash-doc bash-completion

# 安装uwsgi
RUN pip install uwsgi
# 清除缓存
RUN rm -rf /var/cache/apk*

python 常用包,安装报错

apk 包通用查找方法

  1. apk 安装 lxml 报错
RUN apk --no-cache gcc musl-dev libxslt libxml2-dev libxml2 libxslt-dev
  1. apk 安装 pynacl 报错
RUN apk --no-cache add gcc linux-headers musl-dev libffi-dev build-base
  1. apk 安装 uwsgi 报错
RUN apk --no-cache add gcc linux-headers musl-dev
  1. apk 安装 cffi 报错
RUN apk --no-cache add gcc libffi-dev
  1. apk 安装 napalm 报错
RUN apk --no-cache add gcc linux-headers musl-dev bash-doc bash-completion libffi-dev libxslt \
libssl1.1 openssl libffi openssl-dev libxml2-dev libxml2-utils libxml2 libxslt-dev build-base

猜你喜欢

转载自blog.csdn.net/weixin_42290927/article/details/104728942