Alpine Linux 包管理
apk update
$ apk update #更新最新镜像源列表
apk search
$ apk search #查找所以可用软件包
$ apk search -v #查找所以可用软件包及其描述内容
$ apk search -v 'acf*' #通过软件包名称查找软件包
$ apk search -v -d 'docker' #通过描述文件查找特定的软件包
apk add
$ apk add openssh #安装一个软件
$ apk add openssh openntp vim #安装多个软件
$ apk add --no-cache mysql-client #不使用本地镜像源缓存,相当于先执行update,再执行add
apk info
$ apk info #列出所有已安装的软件包
$ apk info -a zlib #显示完整的软件包信息
$ apk info --who-owns /sbin/lbu #显示指定文件属于的包
apk upgrade
$ apk upgrade #升级所有软件
$ apk upgrade openssh #升级指定软件
$ apk upgrade openssh openntp vim #升级多个软件
$ apk add --upgrade busybox #指定升级部分软件包
apk del
$ apk del openssh #删除一个软件
Alpine Linux服务管理
简介
alpine没有使用fedora的systemctl来进行服务管理,使用的是RC系列命令
rc-update
rc-update主要用于不同运行级增加或者删除服务。
# rc-update --help
Usage: rc-update [options] add <service> [<runlevel>...]
or: rc-update [options] del <service> [<runlevel>...]
or: rc-update [options] [show [<runlevel>...]]
Options: [ asuChqVv ]
-a, --all Process all runlevels
-s, --stack Stack a runlevel instead of a service
-u, --update Force an update of the dependency tree
-h, --help Display this help output
-C, --nocolor Disable color output
-V, --version Display software version
-v, --verbose Run verbosely
-q, --quiet Run quietly (repeat to suppress errors)
rc-status
rc-status 主要用于运行级的状态管理。
# rc-status --help
Usage: rc-status [options] <runlevel>...
or: rc-status [options] [-a | -c | -l | -m | -r | -s | -u]
Options: [ aclmrsuChqVv ]
-a, --all Show services from all run levels
-c, --crashed Show crashed services
-l, --list Show list of run levels
-m, --manual Show manually started services
-r, --runlevel Show the name of the current runlevel
-s, --servicelist Show service list
-u, --unused Show services not assigned to any runlevel
-h, --help Display this help output
-C, --nocolor Disable color output
-V, --version Display software version
-v, --verbose Run verbosely
-q, --quiet Run quietly (repeat to suppress errors)
rc-service
rc-service主用于管理服务的状态
# rc-service --help
Usage: rc-service [options] [-i] <service> <cmd>...
or: rc-service [options] -e <service>
or: rc-service [options] -l
or: rc-service [options] -r <service>
Options: [ ce:ilr:INChqVv ]
-e, --exists <arg> tests if the service exists or not
-c, --ifcrashed if the service is crashed then run the command
-i, --ifexists if the service exists then run the command
-I, --ifinactive if the service is inactive then run the command
-N, --ifnotstarted if the service is not started then run the command
-l, --list list all available services
-r, --resolve <arg> resolve the service name to an init script
-h, --help Display this help output
-C, --nocolor Disable color output
-V, --version Display software version
-v, --verbose Run verbosely
-q, --quiet Run quietly (repeat to suppress errors)
openrc
openrc主要用于管理不同的运行级。
# openrc --help
Usage: openrc [options] [<runlevel>]
Options: [ a:no:s:SChqVv ]
-n, --no-stop do not stop any services
-o, --override <arg> override the next runlevel to change into
when leaving single user or boot runlevels
-s, --service <arg> runs the service specified with the rest
of the arguments
-S, --sys output the RC system type, if any
-h, --help Display this help output
-C, --nocolor Disable color output
-V, --version Display software version
-v, --verbose Run verbosely
-q, --quiet Run quietly (repeat to suppress errors)
常用的RC系列命令
1.增加服务到系统启动时运行,下例为docker
rc-update add docker boot
2.重启网络服务
rc-service networking restart
3.列出所有服务
rc-status -a
五:关机重启
$ reboot #重启系统
$ poweroff #关机
三、使用dockerfile,alpine作为基础镜像,部署nginx。
1、apk 安装nginx
[root@localhost ~]# ls
anaconda-ks.cfg Centos-7.repo epel-7.repo initial-setup-ks.cfg nginx-1.16.1.tar.gz pcre-8.44.tar.gz
[root@localhost ~]# vim Dockerfile
[root@localhost ~]# cat Dockerfile
FROM alpine
RUN echo "https://mirrors.aliyun.com/alpine/v3.11/main/" > /etc/apk/repositories; \
echo "https://mirrors.aliyun.com/alpine/v3.11/community/" >> /etc/apk/repositories; \
apk add nginx && mkdir /run/nginx/
EXPOSE 80
ENTRYPOINT [ "nginx", "-g", "daemon off;" ]
[root@localhost ~]# docker build -t nginx:alpine .
Sending build context to Docker daemon 3.151MB
Step 1/4 : FROM alpine
latest: Pulling from library/alpine
59bf1c3509f3: Pull complete
Digest: sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300
Status: Downloaded newer image for alpine:latest
---> c059bfaa849c
Step 2/4 : RUN echo "https://mirrors.aliyun.com/alpine/v3.11/main/" > /etc/apk/repositories; echo "https://mirrors.aliyun.com/alpine/v3.11/community/" >> /etc/apk/repositories; apk add nginx && mkdir /run/nginx/
---> Running in 6bd117725e16
fetch https://mirrors.aliyun.com/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
fetch https://mirrors.aliyun.com/alpine/v3.11/community/x86_64/APKINDEX.tar.gz
(1/2) Installing pcre (8.43-r1)
(2/2) Installing nginx (1.16.1-r8)
Executing nginx-1.16.1-r8.pre-install
Executing busybox-1.34.1-r3.trigger
OK: 7 MiB in 16 packages
Removing intermediate container 6bd117725e16
---> 7ec86e56fb42
Step 3/4 : EXPOSE 80
---> Running in b55604a10de9
Removing intermediate container b55604a10de9
---> beb2cd30b2b4
Step 4/4 : ENTRYPOINT [ "nginx", "-g", "daemon off;" ]
---> Running in c94c99493399
Removing intermediate container c94c99493399
---> 13b3affbfa8b
Successfully built 13b3affbfa8b
Successfully tagged nginx:alpine
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx alpine 13b3affbfa8b 15 seconds ago 8.58MB
alpine latest c059bfaa849c 9 months ago 5.59MB
[root@localhost ~]# docker run -d --name zhanlonghui -p 66:80 nginx:alpine
fe29d323270d21110988b4e6e06fcd58b3552d1081810b1d38e515759bbf9951
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fe29d323270d nginx:alpine "nginx -g 'daemon of…" 6 seconds ago Up 5 seconds 0.0.0.0:66->80/tcp, :::66->80/tcp zhanlonghui
[root@localhost ~]# docker exec -it zhanlonghui /bin/sh
/ # cd /etc/nginx/conf.d/
/etc/nginx/conf.d # ls
default.conf
/etc/nginx/conf.d # vi default.conf
/etc/nginx/conf.d # nginx -s reload
listen 80 default_server;
listen [::]:80 default_server;
# Everything is a 404
location / {
index index.html;
}
2、源代码编译安装nginx
[root@localhost ~]# vim Dockerfile
[root@localhost ~]# cat Dockerfile
FROM alpine:latest
ADD nginx-1.16.1.tar.gz /home/
ADD pcre-8.44.tar.gz /home/
RUN echo http://mirrors.aliyun.com/alpine/v3.10/main/ > /etc/apk/repositories && \
echo http://mirrors.aliyun.com/alpine/v3.10/community/ >> /etc/apk/repositories
RUN apk update && apk upgrade && \
apk add gcc g++ make && \
addgroup -S nginx && \
adduser -DHS -s /sbin/nologin -G nginx nginx && \
cd /home/nginx-1.16.1 && \
./configure --prefix=/usr/local/nginx --with-pcre=/home/pcre-8.44 --without-http_gzip_module && \
make && make install && \
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ && \
mkdir -p /usr/local/nginx/conf/vhost/
EXPOSE 80
CMD ["/usr/sbin/nginx","-g","daemon off;"]
[root@localhost ~]# docker build -t nginx:alpine2 .
Sending build context to Docker daemon 3.152MB
Step 1/7 : FROM alpine:latest
---> c059bfaa849c
Step 2/7 : ADD nginx-1.16.1.tar.gz /home/
---> f5493a11a12e
Step 3/7 : ADD pcre-8.44.tar.gz /home/
---> e34c23b4c6e0
Step 4/7 : RUN echo http://mirrors.aliyun.com/alpine/v3.10/main/ > /etc/apk/repositories && echo http://mirrors.aliyun.com/alpine/v3.10/community/ >> /etc/apk/repositories
---> Running in a9fdfee1ef5c
Removing intermediate container a9fdfee1ef5c
---> 47bea39a39d2
Step 5/7 : RUN apk update && apk upgrade && apk add gcc g++ make && addgroup -S nginx && adduser -DHS -s /sbin/nologin -G nginx nginx && cd /home/nginx-1.16.1 && ./configure --prefix=/usr/local/nginx --with-pcre=/home/pcre-8.44 --without-http_gzip_module && make && make install && ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ && mkdir -p /usr/local/nginx/conf/vhost/
---> Running in c2ebccc929b4
fetch http://mirrors.aliyun.com/alpine/v3.10/main/x86_64/APKINDEX.tar.gz
fetch http://mirrors.aliyun.com/alpine/v3.10/community/x86_64/APKINDEX.tar.gz
v3.10.9-43-g3feb769ea3 [http://mirrors.aliyun.com/alpine/v3.10/main/]
v3.10.6-10-ged79a86de3 [http://mirrors.aliyun.com/alpine/v3.10/community/]
OK: 10357 distinct packages available
OK: 6 MiB in 14 packages
Successfully tagged nginx:alpine2
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx alpine2 0b0ec12a3558 24 seconds ago 197MB
nginx alpine 13b3affbfa8b 15 minutes ago 8.58MB
alpine latest c059bfaa849c 9 months ago 5.59MB
f94421ee514cccf6eca4c6c66cd171a24363d2b70d3c52daad297d04ebe14a89
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f94421ee514c nginx:alpine2 "/usr/sbin/nginx -g …" 11 seconds ago Up 10 seconds 0.0.0.0:67->80/tcp, :::67->80/tcp zlh
fe29d323270d nginx:alpine "nginx -g 'daemon of…" 20 minutes ago Up 20 minutes 0.0.0.0:66->80/tcp, :::66->80/tcp zhanlonghui