ubuntu和alpine的一些管理命令和部署nginx测试

Dockerfile实例

一、整理ubuntu的软件包管理命令和服务管理命令

dpkg -i /**/**/**.deb 安装软件 
dpkg -x **.deb 解开.deb文件 
dpkg -r /-p 删除并清配置 
更详细的 用dpkg --help 查询 如下: 
dpkg -i|--install <.deb 文件的文件名> ... | -R|--recursive <目录> ... 
dpkg --unpack <.deb 文件的文件名> ... | -R|--recursive <目录> ... 
dpkg -A|--record-avail <.deb 文件的文件名> ... | -R|--recursive <目录> ... 
dpkg --configure <软件包名> ... | -a|--pending 
dpkg -r|--remove | -P|--purge <软件包名> ... | -a|--pending 
dpkg --get-selections [<表达式> ...] 把已选中的软件包的列表打印到标准输出 
dpkg --set-selections 从标准输入里读出要选择的软件包列表 
dpkg --update-avail 替换现可安装的软件包信息 
dpkg --merge-avail 把文件中的信息合并到系统中 
dpkg --clear-avail 清除现有的软件包信息 
dpkg --forget-old-unavail 忘却已被删除,现在却不能安装的软件包 
dpkg -s|--status <软件包名> ... 显示软件包详尽的状态信息 
dpkg -p|--print-avail <软件包名> ... 显示软件包当前可供安装的版本的详细信 息 
dpkg -L|--listfiles <软件包名> ... 列出所有“属于”该软件包(或多个软件包)的文件 
dpkg -l|--list [<表达式> ... 简明地列出软件包的状态 
dpkg -S|--search <表达式> ... 搜寻拥有该文件(或多个文件)的软件包 
dpkg -C|--audit 检查搜寻残损的软件包 
dpkg --print-architecture 显示目标机的体系架构(借助 GCC) 
dpkg --print-gnu-build-architecture 显示目标机体系架构的 GNU 版本的表示 
dpkg --print-installation-architecture 显示本地主机的体系架构(用于安装) 
dpkg --compare-vesions <甲> <关系> <乙> 比较版本号 - 见下 
dpkg --help | --version 显示本帮助文档以及版本号 
dpkg --force-help | -Dh|--debug=help 强制操作时,有关出错方面的帮助 
dpkg --licence 显示版权和许可证的条文 


服务管理命令
Ubuntu服务管理也提供了另外一个简单的命令来实现管理。但首先服务必须已在/etc/init.d目录中存在。如:
添加一个服务: sudo update-rc.d ServiceName defaults
删除一个服务: sudo update-rc.d ServiceName remove
还可以安装另外一个比较强的工具: sudo apt-get install sysv-rc-conf sysvconfig
启动: sudo sysv-rc-conf 它可心配置各服务在各级别上的启动情况.
随时想启动某个服务, 可以这样: sudo /etc/init.d/ServiceName start
比如我要远程登录, 要用ssh服务: sudo /etcinit.d/ssh start (别的系统可能是sshd)

untu服务管理中还可以做别的操作:
start : 启动服务  stop : 停止服务  restart : 关闭服务,然后重新启动  reload : 使服不重新启动而重读配置文件   status : 提供服务的当前状态   condrestart : 如果服务锁定,则这个来关闭服务,然后再次启动。


(1)查看某Ubuntu软件包的安装内容
dpkg-Lxxx
(2)查找软件库中的软件包
apt-cachesearch正则表达式
(3)显示系统安装包的统计信息
apt-cachestats
(4)显示系统全部可用Ubuntu软件包的名称
apt-cachepkgnames
(5)显示某软件包的详细信息
apt-cacheshowxxx
(6)查找某文件属于哪个包
apt-filesearchxxx
(7)查看已经安装了哪些Ubuntu软件包
dpkg-l
(8)查询某软件依赖哪些软件包
apt-cachedependsxxx
(9)查询某软件被哪些软件包依赖
apt-cacherdependsxxx
(10)增加一个光盘源
sudoapt-cdromadd
注:顾名思义,就是安装更新Ubuntu软件包时让其优先从Ubuntu光盘上找(如果你不能上网安装/更新,但有Ubuntu的DVDISO,这会对你非常有用)
(11)系统升级
sudoapt-getupdate
(12)清除所有已删除软件包的残馀配置文件
dpkg-l|grep^rc|awk'{print$2}'|sudoxargsdpkg-P
(13)编译时缺少h文件的自动处理
sudoauto-aptrun./configure
(14)查看安装软件时下载Ubuntu软件包的临时存放目录
ls/var/cache/apt/archives
(15)备份当前系统安装的所有软件包的列表
dpkg--get-selections|grep-vdeinstall>~/somefile
(16)从上面备份的安装包的列表文件恢复所有包
dpkg--set-selections<~/somefile
sudodselect
(17)清理旧版本的软件缓存
sudoapt-getautoclean
(18)清理所有软件缓存
sudoapt-getclean
(19)删除系统不再使用的孤立软件
sudoapt-getautoremove

二、整理alpine的软件包管理命令和服务管理命令

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;
        }

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RbjfxvUw-1661944451409)(./1661942774637.png)]

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dpUZKVld-1661944451410)(./1661943759913.png)]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值