Alpine笔记

和光启
2023-12-01

强烈建议update一下

# apk update
fetch http://dl-cdn.alpinelinux.org/alpine/v3.9/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.9/community/x86_64/APKINDEX.tar.gz
v3.9.6-143-ga5f34edab6 [http://dl-cdn.alpinelinux.org/alpine/v3.9/main]
v3.9.6-138-ge069a77b3b [http://dl-cdn.alpinelinux.org/alpine/v3.9/community]
OK: 9780 distinct packages available

apk 源

 # cat /etc/apk/repositories
http://dl-cdn.alpinelinux.org/alpine/v3.9/main
http://dl-cdn.alpinelinux.org/alpine/v3.9/community

可以把上面的默认源换成下面源,比如,把dl-cdn.alpinelinux.org换成mirror.tuna.tsinghua.edu.cn

清华大学:https://mirror.tuna.tsinghua.edu.cn/alpine/
阿里云:https://mirrors.aliyun.com/alpine/
中科大:http://mirrors.ustc.edu.cn/alpine/
网易:http://mirrors.163.com/

alpine包管理基本命令

1. 更新最新镜像源列表:apk update
apk update
2. 搜索软件包:apk search
apk search                # 查找所以可用软件包
apk search -v             # 查找所以可用软件包及其描述内容
apk search -v 'acf*'      # 通过软件包名称查找软件包
apk search -v -d 'docker' # 通过描述文件查找特定的软件包
3. 安装软件包:apk add
apk add openssh                  # 安装一个软件
apk add openssh openntp vim      # 安装多个软件
apk add --no-cache mysql-client  # 不使用本地镜像源缓存,相当于先执行update,再执行add
4. 列出已安装的软件包:apk info
apk info               # 列出所有已安装的软件包
apk info -a zlib       # 显示完整的软件包信息
apk info --who-owns /sbin/lbu # 显示指定文件属于的包
5. 升级软件版本:apk upgrade
apk upgrade                       # 升级所有软件
apk upgrade openssh               # 升级指定软件
apk upgrade openssh openntp vim   # 升级多个软件
apk add --upgrade busybox         # 指定升级部分软件包
6. 删除软件包:apk del
apk del openssh  # 删除一个软件

基础alpine镜像

FROM alpine:3.9.2

# 更新最新镜像源列表
RUN apk update

# 设置Docker 时间为上海时区
RUN apk add -U tzdata
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo "Asia/shanghai" >> /etc/timezone

# 依次安装命令:curl、scp、telnet
RUN apk add curl
RUN apk add openssh-client
RUN apk add busybox-extras

# 这里添加top命令是为了方便本地测试,防止启动该基本镜像容器后自动运行停止
ENTRYPOINT ["top"]

基于Java jdk的镜像构建
由于java:8-alpine里面已有telnet
jdk7的话只需修改java:8-alpine为java:7-alpine即可

FROM java:8-alpine

# 更新最新镜像源列表
RUN apk update
# 设置Docker 时间为上海时区
RUN apk add -U tzdata
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo "Asia/shanghai" >> /etc/timezone

ENTRYPOINT ["/bin/sh"]

创建用户、id、属组

adduser -u 1001 otoyix -D -G www

nginx + php

FROM alpine:3.9.2
#php
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk update \
&& apk add -U tzdata \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/shanghai" >> /etc/timezone 
RUN apk add gcc g++ make libffi-dev openssl-dev \
&& apk add php7-bcmath php7-bz2 php7-calendar php7-ctype php7-curl php7-dom php7-pecl-event php7-exif php7-fileinfo php7-ftp php7-gd php7-gettext php7-gmp php7-iconv php7-pecl-igbinary php7-json php7-mbstring php7-mysqli php7-mysqlnd php7-openssl php7-pcntl php7-pdo_mysql php7-pdo_sqlite php7-posix php7-pecl-redis php7-session php7-shmop php7-sockets php7-sqlite3 php7-sysvmsg php7-sysvsem php7-sysvshm php7-tokenizer php7-wddx php7-xmlrpc php7-xmlreader php7-xml php7-simplexml php7-xmlwriter php7-xmlreader php7-xmlwriter php7-xsl php7-zip php7-fpm \
&& adduser -u 1001 www-data -D \
&& sed -i 's/nobody/www-data/g' /etc/php7/php-fpm.d/www.conf \
&& sed -i 's/127.0.0.1/0.0.0.0/g' /etc/php7/php-fpm.d/www.conf 


#nginx
RUN apk add iotop gcc libgcc libc-dev libcurl libc-utils pcre-dev zlib-dev libnfs make pcre pcre2 zip unzip net-tools wget openssl openssl-dev
RUN wget -c http://nginx.org/download/nginx-1.17.0.tar.gz \
&& tar -xf nginx-1.17.0.tar.gz \
&& rm -rf nginx-1.17.0.tar.gz \
&& cd nginx-1.17.0 \
&& ./configure --user=www-data --group=www-data --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module  --with-http_stub_status_module\
&& make && make install \
&& ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/ \
&& echo -e "<?php\nphpinfo();\n?>" >/usr/local/nginx/html/index.php
COPY nginx.conf /usr/local/nginx/conf/

RUN echo -e "/usr/sbin/php-fpm7\n/usr/local/nginx/sbin/nginx\n/bin/sh\n" >/usr/init.sh
CMD sh /usr/init.sh

mariadb10.3

FROM alpine:3.9.2
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& apk update \
&& apk add -U tzdata \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime  \
&& echo "Asia/shanghai" >> /etc/timezone \
RUN apk add iotop gcc libgcc libc-dev libcurl libc-utils pcre-dev zlib-dev libnfs make pcre pcre2 zip unzip net-tools wget openssl openssl-dev \
&& adduser -u 1000 mysql -D \
&& apk add mariadb mariadb-client \
&& mkdir -p /data/mysql \
&& chown -R mysql:mysql /data/mysql/ \
&& mysql_install_db --user=mysql --datadir=/data/mysql \
&& cp -r /usr/share/mariadb/mysql.server /etc/init.d/mysqld \
&& sed -i 's#/var/lib/mysql#/data/mysql#' /etc/init.d/mysqld \
&& sed -i 's/skip-networking/#skip-networking/' /etc/my.cnf.d/mariadb-server.cnf \
&& /etc/init.d/mysqld start \
&& /usr/bin/mysqladmin -u root password '123456' \
&& echo -e "/etc/init.d/mysqld start\n/bin/sh\n" >/usr/init.sh
CMD sh /usr/init.sh

redis

注: 此redis目录已经经过处理,非原版

FROM alpine:3.9.2
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk update \
&& adduser -u 1000 curefun -D \
&& apk add -U tzdata \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/shanghai" >> /etc/timezone 
RUN apk add gcc g++ make libffi-dev openssl-dev \
&& wget -c https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.29-r0/glibc-2.29-r0.apk \
&& wget -c https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub \
&& mv sgerrand.rsa.pub /etc/apk/keys/ \
&& apk add glibc-2.29-r0.apk \
&& mkdir -p /usr/local/redis/
COPY redis /usr/local/redis/
CMD /usr/local/redis/redis-server /usr/local/redis/redis.conf

------------------end

 类似资料: