ubuntu,alpine的软件包管理命令和服务管理命令和Dockerfile实例

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

软件包管理命令
查看软件包xxx安装内容
#dpkg -L xxx 
例子:
#dpkg -L binutils    //查看安装binutils包会安装哪些文件
 
列出系统已安装的或指定deb包的安装状态信息
#dpkg -l [packagename]
 
查找文件属于哪个包
#dpkg -S filename
例子:
#dpkg -S /usr/bin/objdump
binutils: /usr/bin/objdump
 
查找软件 
#apt-cache search 正则表达式 
 
查询软件xxx依赖哪些包(forward dependency)
#apt-cache depends xxx
 
查询软件xxx被哪些包依赖(reverse dependency) 
#apt-cache rdepends xxx
 
查询依赖和被依赖的包
#apt-cache showpkg binutils    //显示binutils包依赖(forward dependency)和依赖于它的包(reverse dependency) 
 
增加一个光盘源
#apt-cdrom add
 
更新软件源索引
#apt-get update
 
安装软件
#apt-get install xxx
#apt-get install xxx --print-uris     //仅仅打印出URL,而不实际安装
 
忽略依赖关系,强制安装
#dpkg -i --force-depends package_a.deb
或
#dpkg -i --ignore-depends=packageneme
 
安装APT已经下载的包,忽略缺少的包
#apt-get install xxx --ignore-missing --no-download
 
将系统中所有软件升级到最新版本
#apt-get upgrade
 
将操作系统连同所有软件升级到最新版本
#apt-get dist-upgrade
 
下载指定软件的源文件
#apt-get source package-name
 
备份当前系统安装的所有包的列表 
#dpkg --get-selections | grep -v  deinstall > ~/install_list
 
从上面备份的安装包的列表文件恢复选择并安装所有包
#dpkg --set-selections <install_list
#apt-get dselect-upgrade
 
卸载指定软件
#apt-get remove package-name
 
清理旧版本的软件缓存
#apt-get autoclean
 
清理所有软件缓存
#apt-get clean
 
删除系统不再使用的孤立软件
#apt-get autoremove 
 
安装软件包到指定目录
$sudo dpkg -i --instdir=/home/cb/opt/openoffice/ *.deb
服务管理命令
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 : 如果服务锁定,则这个来关闭服务,然后再次启动。

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@docker ~]# ls
公共  视频  文档  音乐  anaconda-ks.cfg  Dockerfile   initial-setup-ks.cfg  pcre-8.44.tar.gz
模板  图片  下载  桌面  Centos-7.repo    epel-7.repo  nginx-1.16.1.tar.gz

[root@docker ~]# vim 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@docker ~]# docker build -t nginx:alpine .
Sending build context to Docker daemon   17.3MB
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 820fb8b045e3
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 820fb8b045e3
 ---> 8a53d2ddf5a8
Step 3/4 : EXPOSE 80
 ---> Running in e60e91e8a0e1
Removing intermediate container e60e91e8a0e1
 ---> 222ec5f36a70
Step 4/4 : ENTRYPOINT [ "nginx", "-g", "daemon off;" ]
 ---> Running in ccb211b73227
Removing intermediate container ccb211b73227
 ---> 2e2141c9392e
Successfully built 2e2141c9392e
Successfully tagged nginx:alpine
[root@docker ~]# 
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        alpine    2e2141c9392e   2 minutes ago   8.58MB
[root@docker ~]# docker run -d --name heshuangxi -p 66:80 nginx:alpine 
f075d97367fbc7f3dd643c32627f7f614c2c73bd3fcc4de3c2056e799aac8a6a
[root@docker ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                               NAMES
f075d97367fb   nginx:alpine   "nginx -g 'daemon of…"   6 seconds ago   Up 5 seconds   0.0.0.0:66->80/tcp, :::66->80/tcp   heshuangxi
[root@docker ~]# docker exec -it heshuangxi /bin/sh
/ # cd /etc/nginx/conf.d/
/etc/nginx/conf.d # ls
default.conf
/etc/nginx/conf.d # vi default.conf 
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # Everything is a 404
        location / {
                index index.html;
/etc/nginx/conf.d # nginx -s reload

浏览器访问
在这里插入图片描述

使用Dockerfile源码安装nginx

[root@docker ~]# vim 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@docker ~]# docker build -t nginx:alpine2 .
make[1]: Leaving directory '/home/nginx-1.16.1'
Removing intermediate container 5fcbcd816983
 ---> 4006a0ad019f
Step 6/7 : EXPOSE 80
 ---> Running in e4f8df9e6806
Removing intermediate container e4f8df9e6806
 ---> 623e474936b8
Step 7/7 : CMD ["/usr/sbin/nginx","-g","daemon off;"]
 ---> Running in fa03908e36b3
Removing intermediate container fa03908e36b3
 ---> 116e558793b6
Successfully built 116e558793b6
Successfully tagged nginx:alpine2

[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED              SIZE
nginx        alpine2   116e558793b6   About a minute ago   197MB
nginx        alpine    2e2141c9392e   15 minutes ago       8.58MB

root@docker ~]# docker run -d --name heshuangxi -p 66:80 nginx:alpine2 
60d3ea1748f29967bdad6b759238a0663a6f97d7c7a3eca04e0f5040f4fc87f5
[root@docker ~]# docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED         STATUS         PORTS                               NAMES
60d3ea1748f2   nginx:alpine2   "/usr/sbin/nginx -g …"   4 seconds ago   Up 3 seconds   0.0.0.0:66->80/tcp, :::66->80/tcp   heshuangxi
[root@docker ~]# 

浏览器访问
在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值