Alpine介绍

Alpine 操作系统是一个面向安全的轻型 Linux 发行版。它不同于通常 Linux 发行版,Alpine 采用了 musl libc 和 busybox 以减小系统的体积和运行时资源消耗,但功能上比 busybox 又完善的多,因此得到开源社区越来越多的青睐。在保持瘦身的同时,Alpine 还提供了自己的包管理工具 apk,可以通过 https://pkgs.alpinelinux.org/packages 网站上查询包信息,也可以直接通过 apk 命令直接查询和安装各种软件。

Alpine 由非商业组织维护的,支持广泛场景的 Linux发行版,它特别为资深/重度Linux用户而优化,关注安全,性能和资源效能。Alpine 镜像可以适用于更多常用场景,并且是一个优秀的可以适用于生产的基础系统/环境。

Alpine Docker 镜像也继承了 Alpine Linux 发行版的这些优势。相比于其他 Docker 镜像,它的容量非常小,仅仅只有 5 MB 左右(对比 Ubuntu 系列镜像接近 200 MB),且拥有非常友好的包管理机制。官方镜像来自 docker-alpine 项目。

目前 Docker 官方已开始推荐使用 Alpine 替代之前的 Ubuntu 做为基础镜像环境。这样会带来多个好处。包括镜像下载速度加快,镜像安全性提高,主机之间的切换更方便,占用更少磁盘空间等。

Alpine的特点:

  • 小巧:基于Musl libc和busybox,和busybox一样小巧,最小的Docker镜像只有5MB;
  • 安全:面向安全的轻量发行版;
  • 简单:提供APK包管理工具,软件的搜索、安装、删除、升级都非常方便。
  • 适合容器使用:由于小巧、功能完备,非常适合作为容器的基础镜像。
[root@master ~]# docker images
REPOSITORY     TAG         IMAGE ID            CREATED             SIZE
alpine         latest      a187dde48cd2        16 hours ago        5.6MB

可以看到alpine镜像只有5.6MB,而ubuntu镜像则近百MB,centos镜像近三百MB,Alpine的体积优势非常明显。

Alpine软件包管理

配置软件源

#修改源替换成阿里源,将里面 dl-cdn.alpinelinux.org 的 改成 mirrors.aliyun.com
vi etc/apk/repositories
https://dl-cdn.alpinelinux.org/alpine/v3.16/main
https://dl-cdn.alpinelinux.org/alpine/v3.16/community

官方源在国内很慢,甚至无法连接,我们将其改为国内镜像源

 将 dl-cdn.alpinelinux.org 改为 mirrors.aliyun.com 即可
/ # cat /etc/apk/repositories 
https://mirrors.aliyun.com/alpine/v3.16/main
https://mirrors.aliyun.com/alpine/v3.16/community

修改完成后更新一下仓库
/# apk update

软件包管理

alpine 提供了非常好用的apk软件包管理工具,可以方便地安装、删除、更新软件。

查询软件

/ # apk search wget
wget-1.21.3-r0
wget-doc-1.21.3-r0
wireless-tools-30_pre9-r1
owfs-3.2p4-r0
wgetpaste-2.32-r0
wgetpaste-zsh-completion-2.32-r0

安装软件

/ # apk add wget
(1/3) Installing libunistring (1.0-r0)
(2/3) Installing libidn2 (2.3.2-r2)
(3/3) Installing wget (1.21.3-r0)
Executing busybox-1.35.0-r17.trigger
OK: 56 MiB in 37 packages

卸载软件

/ # apk del wget
(1/3) Purging wget (1.21.3-r0)
(2/3) Purging libidn2 (2.3.2-r2)
(3/3) Purging libunistring (1.0-r0)
Executing busybox-1.35.0-r17.trigger
OK: 54 MiB in 34 packages

apk使用帮助信息

/ # apk --help
apk-tools 2.12.9, compiled for x86_64.

usage: apk [<OPTIONS>...] COMMAND [<ARGUMENTS>...]

Package installation and removal:
  add        Add packages to WORLD and commit changes
  del        Remove packages from WORLD and commit changes

System maintenance:
  fix        Fix, reinstall or upgrade packages without modifying WORLD
  update     Update repository indexes
  upgrade    Install upgrades available from repositories
  cache      Manage the local package cache

Querying package information:
  info       Give detailed information about packages or repositories
  list       List packages matching a pattern or other criteria
  dot        Render dependencies as graphviz graphs
  policy     Show repository policy for packages
  search     Search for packages by name or description

Repository maintenance:
  index      Create repository index file from packages
  fetch      Download packages from global repositories to a local directory
  manifest   Show checksums of package contents
  verify     Verify package integrity and signature

Miscellaneous:
  audit      Audit system for changes
  stats      Show statistics about repositories and installations
  version    Compare package versions or perform tests on version strings

This apk has coffee making abilities.
For more information: man 8 apk

使用alpine作为基础镜像,构建httpd镜像

[root@localhost ~]# mkdir apache
[root@localhost ~]# cd apache/
[root@localhost ~]# mkdir files
[root@localhost ~]# cd files
[root@localhost files]#wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
[root@localhost files]#wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
[root@localhost files]#wget https://dlcdn.apache.org/httpd/httpd-2.4.54.tar.gz
[root@localhost files]#ls
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.54.tar.gz
[root@localhost files]# cd ..
[root@localhost apache]# touch Dockerfile
[root@localhost apache]# vim Dockerfile
FROM alpine
LABEL MAINTAINER "2279236475@qq.com"
ENV apr_version=1.7.0 apr_util_version=1.6.1 httpd_version=2.4.54
ADD files/* /tmp


RUN sed -i s/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g /etc/apk/repositories && apk update &&  \
    adduser -SHs /sbin/nologin apache && \
    apk add --no-cache -U gcc openssl-dev libc-dev  pcre-dev libtool expat-dev make && \
    cd /tmp/apr-${apr_version} && \
    sed -i '/$RM "$cfgfile"/d' configure && \
    ./configure --prefix=/usr/local/apr && make && make install && \
    cd /tmp/apr-util-${apr_util_version} && \
    ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \
    make && make install && \
    cd /tmp/httpd-${httpd_version} && \
   ./configure --prefix=/usr/local/apache \
   --sysconfdir=/etc/httpd \
   --with-apr=/usr/local/apr \
   --with-apr-util=/usr/local/apr-util \
   --enable-so \
   --enable-mods-shared=most  \
   --enable-ssl  \
   --enable-cgi  \
   --enable-rewrite  \
   --with-zlib   \
   --with-pcre     \
   --enable-modules=most    \
   --enable-mpms-shared=all  \
    && make && make install && \
    sed -i '/#ServerName/s/#//g' /etc/httpd/httpd.conf && \
     rm -rf /tmp/* &&  apk del gcc make



EXPOSE 80
WORKDIR /usr/local/apache
CMD ["/usr/local/apache/bin/apachectl","-D","FOREGROUND"]

下面执行build命令生成image文件,如果执行成功,可以通过docker images来查看新生成的镜像文件,使用alpine作为基础镜像,构建的镜像会比centos构建出来的小很多。

podman build -t docker.io/dugxq/httpd:v4 apache/
[root@localhost ~]# podman images
REPOSITORY                TAG         IMAGE ID      CREATED        SIZE
docker.io/dugxq/httpd     v4          256a7dc7c168  3 hours ago    122 MB

centos作为基础镜像,构建出来的镜像确实很大

[root@localhost ~]# podman build -t docker.io/dugxq/httpd:v2 apache/
[root@localhost ~]# podman images 
REPOSITORY             TAG         IMAGE ID      CREATED        SIZE
docker.io/dugxq/httpd  v2          784b911d18a7  5 hours ago    633 MB

搭建网站

使用制作的httpd镜像搭建一个网站

[root@localhost ~]# mkdir data
[root@localhost data]# wget   www.1314520.link/upload/2022/07/斗地主.zip
[root@localhost data]# unzip 斗地主.zip
[root@localhost data]# ls
DJDDZ.js  img  index.html  JControls.js  Prototype.js  ResourceData.js

[root@localhost data]# podman run -d  --name web -p 80:80 -v  /root/data:/usr/local/apache/htdocs/   docker.io/dugxq/httpd:v4

[root@localhost data]# podman run -d  --name web1 -p 80:80 -v  /root/data:/usr/local/apache/htdocs/   docker.io/dugxq/httpd:v4  
0bff9d81714f0667b8bdb5c731457cde0e7045c214bca35aaaffd93512d24e6f
[root@localhost data]# ss -anlt
State          Recv-Q         Send-Q                   Local Address:Port                   Peer Address:Port         Process         
LISTEN         0              128                            0.0.0.0:80                          0.0.0.0:*                            
LISTEN         0              128                            0.0.0.0:22                          0.0.0.0:*                            
LISTEN         0              128                               [::]:22                             [::]:*  

访问测试

image-1661949514804!