dockerfile —— 操作系统

Ubuntu 命令

Ubuntu 是以桌面应用为主的 GNU/Linux 开源操作系统,其名称来自非洲南部祖鲁语或豪萨语的“ubuntu”一词。官方译名“友帮拓”,另有“乌班图”等译名。

软件包管理:apt

  • apt-cache search:搜索软件包
  • apt-cache show:显示软件包的详细信息
  • apt list:列出包含条件的包(已安装、可升级等)
  • apt-get install:安装软件包
    • -y:安装无须确认
  • apt-get remove:卸载软件包
  • apt-get purge:删除软件包和相关配置文件
  • apt-get update:更新存储库索引,install 前必须运行
  • apt-get upgrade:升级所有可升级的软件包
    • apt-get update 和 apt-get upgrade 的最大区别是,update 仅同步更新软件列表,而 upgrade 则根据列表更新软件本身
  • apt full-upgrade:升级软件包并自动解决依赖关系
  • apt edit-sources:编辑软件源列表
  • apt-get autoclean:自动清除不需要的包
  • apt-get autoremove:自动删除不需要的包

服务管理

历史版本中的 Linux 对服务的操作是通过 service 来完成的;
目前 Linux 新的发行版已经内置了systemctl 来操作服务。

service

可通过sercice SCRIPT COMMAND命令管理服务,SCRIPT 为/etc/init.d/中存放的可执行脚本文件:

  1. service SCRIPT start:启动服务
  2. service SCRIPT stop:停止服务
  3. service SCRIPT restart:重启服务
  4. service SCRIPT status:查看状态服务

此外还可以用service --status-all显示出所有系统服务列表,其中”+”代表服务正在运行,而”-“代表服务处于关闭状态,”?”代表根本没有状态。

用户可以添加自定义服务,将相应脚本放置于/etc/init.d/文件夹下。

systemctl
  • systemctl:查看已经运行的对象
  • systemctl --all:查看没有启动的对象
  • systemctl list-units --type=服务:查看系统某个服务状态
  • systemctl list-units | grep service:查看系统中所有的服务
  • systemctl list-units | grep service | grep running:查看系统中所有运行的服务
  • systemctl status:显示系统状态
  • systemctl start 服务:立即激活服务
  • systemctl stop 服务:立即停止服务
  • systemctl restart 服务:重启服务
  • systemctl enable 服务:开机自动激活服务
  • systemctl disable 服务:取消开机自动激活服务
  • systemctl daemon-reload:重新载入 systemd,扫描新的或有变动的服务

能用 systemctl 管理的服务需要有一个.service文件,在 Ubuntu 中,通常该文件位于/etc/systemd/system文件夹下。

Alpine 命令

Alpine 操作系统是一个面向安全的轻型 Linux 发行版,关注安全,性能和资源效能。不同于其他发行版,Alpine 采用了musl libc 和 BusyBox 以减小系统的体积和运行时资源消耗,比 BusyBox 功能上更完善。在保持瘦身的同时,Alpine 还提供了包管理工具 apk 查询和安装软件包。

软件包管理:apk

  • apk update:更新最新镜像源列表(所有软件/指定软件/多个软件)
    • –upgrade:指定升级部分软件包
  • apk search:查找所有可用软件包
    • -v:查找所有可用软件包及其描述内容
    • -v ‘软件包名称’:通过软件包名称查找软件包
    • -v -d ‘软件包名称’:通过描述文件查找特定的软件包
  • apk add:安装一个或多个软件
    • –no-cache:不使用本地镜像源缓存,相当于先执行 update,再执行 add
  • apk info:列出所有已安装的软件包
    • -a:显示完整的软件包信息
    • -e:检测软件包的安装
    • -L:列出软件包的内容
    • -R:列出指定包的依赖
    • -s:显示软件包大小
    • -d:显示软件包的描述
    • –who-owns:显示指定文件属于的包
  • apk del:删除一个软件

服务管理:RC 系列

精简版的 alpine 没有 RC 系列命令,可用apk add --no-cache openrc安装

  • rc-update:主要用于不同运行级增加或者删除服务
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:主要用于运行级的状态管理
Usage: rc-status [options] -f ini <runlevel>...
   or: rc-status [options] [-a | -c | -l | -m | -r | -s | -u]

Options: [ acf:lmrsSuChqVv ]
  -a, --all                         Show services from all run levels
  -c, --crashed                     Show crashed services
  -f, --format <arg>                format status to be parsable (currently arg must be ini)
  -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
  -S, --supervised                  show supervised services
  -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:主要用于管理服务的状态
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: [ cdDe:ilr:INsSZChqVv ]
  -d, --debug                       set xtrace when running the command
  -D, --nodeps                      ignore dependencies
  -e, --exists <arg>                tests if the service exists or not
  -c, --ifcrashed                   if the service is crashed run the command
  -i, --ifexists                    if the service exists run the command
  -I, --ifinactive                  if the service is inactive run the command
  -N, --ifnotstarted                if the service is not started run the command
  -s, --ifstarted                   if the service is started run the command
  -S, --ifstopped                   if the service is stopped run the command
  -l, --list                        list all available services
  -r, --resolve <arg>               resolve the service name to an init script
  -Z, --dry-run                     dry run (show what would happen)
  -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:主要用于管理不同的运行级
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)

部署 Nginx

apk 安装 Nginx

[root@localhost ~]# vim Dockerfile
FROM alpine		//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
CMD ["/usr/sbin/nginx","-g","daemon off;"]

[root@localhost ~]# docker build -t nginx:v1 .
......
Successfully built 3fc5c35aecc0
Successfully tagged nginx:v1

[root@localhost ~]# docker run --name kangyimei -d -P nginx:v1
1ed63b315fdb2b0460a912a44c3057721c59b43be486cb21da349a3369b65379

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                                     NAMES
1ed63b315fdb   nginx:v1   "/usr/sbin/nginx -g …"   3 seconds ago   Up 2 seconds   0.0.0.0:49153->80/tcp, :::49153->80/tcp   kangyimei

[root@localhost ~]# docker exec -it kangyimei /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

在这里插入图片描述

源代码编译安装 Nginx

开始之前,先docker rm -f移除上一步的容器

[root@localhost ~]# ls
anaconda-ks.cfg  nginx-1.16.1.tar.gz  pcre-8.44.tar.gz

[root@localhost ~]# vim Dockerfile 
FROM alpine:latest
ADD nginx-1.16.1.tar.gz /home/
ADD pcre-8.44.tar.gz /home/

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 
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:v2 .
......
Successfully built 196714a8e59c
Successfully tagged nginx:v2

[root@localhost ~]# docker run --name kangyimei -d -p 80:80 nginx:v2
6cb0b507efd4685b36fadcc40af711a56c6c18d3d49d02a9490380420e73b21d

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                               NAMES
6cb0b507efd4   nginx:v2   "/usr/sbin/nginx -g …"   3 seconds ago   Up 2 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   kangyimei

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值