Alpine Docker镜像
作为兼顾安全和轻量级的 Alpine Linux 是 Docker 默认的基础镜像,能够极大地缩减Docker镜像,并且降低运行资源,真正体现容器的轻量、灵活。
如果你像我一样,尝试从0开始在 ARM 架构( 例如 Apple ARM架构芯片M1 Pro 和 Raspberry Pi )构建自己的 移动云计算构建 ,那么你可以从最精简的 Alpine Linux 基础镜像开始,只在镜像中添加和运行程序直接相关的库和工具,避免引入不必要的系统开销和安全隐患。
Alpine Docker官方镜像
Alpine DOI(Docker Official Image)是一个包含执行软件堆栈的Alpine Linux Docker镜像,包含你的源代码,库,工具以及应用程序运行的必要核心依赖。
和其他Linux发行版不同:
Alpine基于
musl libc实现C标准库Alpine使用
BusyBox(用一个执行程序替代一组核心功能程序)替代GNU coreutils
Alpine Linux 吸引了不需要强制性兼容和功能的开发人员,并且提供了友好和直接的使用体验(没有复杂的软件组合)。
采用 Alpine Linux 构建的运行容器镜像都非常精简:
镜像内容 |
镜像大小 |
|---|---|
alpine-base |
11MB |
alpine-bash |
13MB |
alpine-nginx |
14.5MB |
基础运行 alpine-base
在
alpine-base目录下Dockerfile:
FROM alpine:latest
RUN apk update && apk upgrade
ENTRYPOINT ["/bin/ash"]
备注
警告
这里你很可能和我一样遇到GFW屏蔽docker registry导致的报错:
[+] Building 30.8s (2/2) FINISHED docker:rancher-desktop
=> [internal] load build definition from Dockerfile 0.4s
=> => transferring dockerfile: 111B 0.0s
=> ERROR [internal] load metadata for docker.io/library/alpine:latest 30.0s
------
> [internal] load metadata for docker.io/library/alpine:latest:
------
Dockerfile:1
--------------------
1 | >>> FROM alpine:latest
2 | RUN apk update && apk upgrade
3 |
--------------------
ERROR: failed to solve: alpine:latest: failed to resolve source metadata for docker.io/library/alpine:latest: failed to do request: Head "https://registry-1.docker.io/v2/library/alpine/manifests/latest": dial tcp 157.240.8.50:443: i/o timeout
构建
alpine-base镜像:
docker build -t alpine-base .
如果在本地Docker中运行,则直接执行:
docker run --name alpine -it alpine-base
基础运行 alpine-bash
备注
很多运维人员习惯使用 bash ,所以也可以构建支持 bash 的 Alpine Linux
在
alpine-bash目录下Dockerfile:
FROM alpine:latest
RUN apk update && apk upgrade
RUN apk add --no-cache bash
ENTRYPOINT ["/bin/bash"]
构建
alpine-bash镜像:
docker build -t alpine-bash .
运行
alpine-bash:
docker run --name alpine-bash -it alpine-bash
NGINX服务 alpine-nginx
备注
提供nginx服务的alpine镜像: 我准备将数据存放在 /data 目录下( Kubernetes ):
html子目录存放 WEB 内容( ZFS NFS )nginx软件包安装后默认nginx.conf将读取的/etc/nginx/http.d目录下配置,其中/etc/nginx/httpd.defult.conf配置可覆盖修改以使用自己定制的数据目录
在
alpine-nginx目录下Dockerfile:
FROM alpine:latest
RUN apk update && apk upgrade
RUN apk add --no-cache bash
RUN apk add --no-cache nginx
COPY default.conf /etc/nginx/http.d/
RUN mkdir -p /data/html
COPY index.html /data/html/
EXPOSE 80/tcp
ENTRYPOINT ["/usr/sbin/nginx", "-g", "daemon off;"]
备注
修订默认的 /etc/nginx/http.d/default.conf 是在 Kubernetes 中运行自定义AlpineLinux NGINX的重要一步
构建
alpine-nginx镜像:
docker build -t alpine-nginx .
注意: 默认的
default.conf:
erver {
listen 80 default_server;
listen [::]:80 default_server;
# Everything is a 404
location / {
return 404;
}
# You may need this to prevent return 404 recursion.
location = /404.html {
internal;
}
}
必须被配置允许访问的 default.conf 替换,否则类似 在kind运行简单的容器 无法启动(见下文)
简单的运行
alpine-nginx验证:
docker run -p 8080:80 --name cloud-atlas \
-v /home/huatai/docs/docker/alpine/nginx/default.conf:/etc/nginx/http.d/default.conf \
-v /home/huatai/docs/github.com/cloud-atlas/build/html:/data/html \
alpine-nginx
现在验证通过的NGINX镜像,是否就可以用到 在kind运行简单的容器 呢?
不能在Kubernetes启动的 alpine-nginx
实际 Alpine Linux 出于安全考虑(这是一个注重安全的面向嵌入式平台的发行版),将 default.conf 设置成禁止访问(直接返回 404 ),而不是一般发行版默认允许访问 index.html 。
这导致我在 在kind运行简单的容器 遇到始终出现 Kubernetes健康检测 失败的 Kubernetes pod CrashLoopBackOff错误排查
解决的方法也很简单,就是准备好一个允许正常访问的 default.conf 在 BUILD 时候覆盖镜像中的配置文件,这个 default.conf 可以如下:
server {
listen 80 default_server;
listen [::]:80 default_server;
location / {
root /data/html;
index index.html index.htm;
}
# You may need this to prevent return 404 recursion.
location = /404.html {
internal;
}
}
下一步
我将部署到 kind(本地docker模拟k8s集群) 集群中来模拟 Kubernetes 运行:
加载kind镜像 或者先部署 kind集群本地Registry 然后统一从Registry下载镜像构建Kubernetes运行的pod
使用 ZFS NFS 提供共享存储
配置 在Kubernetes中部署NFS 将共享卷中我的
cloud-atlasbuild好的html作为NGINX的目录(配置和上文Docker运行方式类似)
SSH服务 alpine-ssh
备注
参考 Debian镜像(tini进程管理器) 配置 alpine-ssh 镜像
使用
tini进程管理器启动需要的服务sshcron:
tini 进程管理启动ssh服务FROM alpine:latest
ENV container=docker
RUN apk update && apk upgrade
# Alpine Linux 仓库内置tini,可以直接安装
RUN apk add --no-cache tini
# Copy tini entrypoint script
COPY entrypoint_ssh_cron_ash /entrypoint.sh
RUN chmod +x /entrypoint.sh
# Devops utilities
RUN apk add --no-cache openssh sudo bind-tools tmux
# add account "admin" and give sudo privilege
RUN adduser -u 501 -G wheel -h /home/admin -s /bin/sh -D admin
RUN echo "%wheel ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
# set TIMEZONE to Shanghai
RUN apk add --no-cache tzdata
RUN ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# init sshd
RUN ssh-keygen -A
# alpine linux sshd default use password auth, empty password will lock account, change to disable password auth
RUN echo "PasswordAuthentication no" >> /etc/ssh/sshd_config
# run service when container started - sshd
EXPOSE 22:1122
# Run your program under Tini
# CMD ["/your/program", "-and", "-its", "arguments"]
ENTRYPOINT ["/sbin/tini", "--", "/entrypoint.sh"]
构建镜像:
docker build -t alpine-ssh .
运行容器: 其中2个bind的卷是Lima提供的HOST物理主机目录
docker run -dt --name alpine-ssh --hostname alpine-ssh \
-p 1122:22 \
-v /Users/admin/secrets:/home/admin/.ssh \
-v /Users/admin/docs:/home/admin/docs \
alpine-ssh:latest
这里挂载了host主机上提供的 secret 目录,包含了容器ssh登陆的公钥
备注
这里我遇到一个比较奇怪的问题,alpine linux系统用户账号 admin 配置了ssh公钥之后,但是我使用密钥登陆总是失败。后来发现,在alipine linux中为该 admin 设置一个密码之后,立即就能够使用密钥登陆。就好像用户账号需要密码才能激活一样,这在其他发行版中没有遇到过。
原来 alpine linux 的发行版sshd默认配置和其他发行版不同,默认配置是 PasswordAuthentication yes ,此时如果账号没有设置密码,就是 lock 状态。解决方法是:
为账号设置密码
修订sshd为
PasswordAuthentication no就能够ssh密钥登陆强制
passwd -u [USER]解锁/激活账号(但是账号存在无密码本地登陆风险)
开发环境 alpine-dev
alpine-dev包含了安装常用工具和开发环境:
FROM alpine:latest
ENV container=docker
RUN apk update && apk upgrade
# Alpine Linux 仓库内置tini,可以直接安装
RUN apk add --no-cache tini
# Copy tini entrypoint script
COPY entrypoint_ssh_cron_ash /entrypoint.sh
RUN chmod +x /entrypoint.sh
# Devops utilities
RUN apk add --no-cache openssh sudo bind-tools tmux
# add account "admin" and give sudo privilege
RUN adduser -u 501 -G wheel -h /home/admin -s /bin/sh -D admin
RUN echo "%wheel ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
# set TIMEZONE to Shanghai
RUN apk add --no-cache tzdata
RUN ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# init sshd
RUN ssh-keygen -A
# alpine linux sshd default use password auth, empty password will lock account, change to disable password auth
RUN echo "PasswordAuthentication no" >> /etc/ssh/sshd_config
# developement
RUN apk add build-base
RUN apk add gdb strace
RUN apk add go
RUN apk add rust
# nodejs选择LTS版本
RUN apk add nodejs-lts
# run service when container started - sshd
EXPOSE 22:1122
# Run your program under Tini
# CMD ["/your/program", "-and", "-its", "arguments"]
ENTRYPOINT ["/sbin/tini", "--", "/entrypoint.sh"]
备注
详细说明见 容器化开发环境 debian-dev
构建镜像:
docker build -t alpine-dev .
运行容器:
docker run -dt --name alpine-dev --hostname alpine-dev \
-p 1122:22 \
-v /Users/admin/secrets:/home/admin/.ssh \
-v /Users/admin/docs:/home/admin/docs \
alpine-dev:latest