URLOS应用开发基础课Docker基础镜像Alpine入门教程

4次阅读

共计 3842 个字符,预计需要花费 10 分钟才能阅读完成。

我们在进行 URLOS 应用开发时,经常会用到一些基础系统镜像,如:ubuntu、CentOS、Debian 等,我们可以通过 docker pull 命令直接拉取官方镜像。

root@ubuntu:~# docker pull ubuntu:18.04
18.04: Pulling from library/ubuntu
898c46f3b1a1: Already exists
63366dfa0a50: Already exists
041d4cd74a92: Already exists
6e1bee0f8701: Already exists
Digest: sha256:017eef0b616011647b269b5c65826e2e2ebddbe5d1f8c1e56b3599fb14fabec8
Status: Downloaded newer image for ubuntu:18.04
root@master-node:~# docker images
REPOSITORY            TAG                  IMAGE ID            CREATED             SIZE
ubuntu                18.04                94e814e2efa8        6 weeks ago         88.9MB
root@ubuntu:~#

以上是从 docker 官方拉取的 ubuntu18.04 镜像,我们可以基于此镜像制作相关的应用,比如 LNP 网站环境、LAP 网站环境、Nodejs 环境等等。简单理解,就是说几乎所有的 docker 应用都是在这些镜像上层层打包后得到的,应用的最终体积也许有几百 M 甚至上 G,我们如何为应用瘦身以减少对磁盘的消耗呢?

docker 官方为我们打造了Alpine
Alpine 的意思是“高山的”,比如 Alpine plants 高山植物,Alpine skiing 高山滑雪、the alpine resort 阿尔卑斯山胜地,其实 Alpine 是一个操作系统。

Alpine 操作系统是一个面向安全的轻型 Linux 发行版。目前 Docker 官方已开始推荐使用 Alpine 替代之前的 Ubuntu 做为基础镜像环境。这样会带来多个好处。包括镜像下载速度加快,镜像安全性提高,主机之间的切换更方便,占用更少磁盘空间等。

Alpine 的特点:

1、小巧:基于 Musl libc 和 busybox,和 busybox 一样小巧,最小的 Docker 镜像只有 5MB;

2、安全:面向安全的轻量发行版;

3、简单:提供 APK 包管理工具,软件的搜索、安装、删除、升级都非常方便。

4、适合容器使用:由于小巧、功能完备,非常适合作为容器的基础镜像。

在制作 URLOS 应用时,我们可以选择 Alpine 作为系统基础镜像,这样一来可有效降低应用的大小,方便其他用户下载安装。现在我们开始使用 Alpine(如果你的系统中没有安装 docker,建议先安装 URLOS,因为它自带了 docker)。

docker 下运行 Alpine
使用 docker pull 命令拉取 Alpine 镜像

root@ubuntu:~# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
bdf0201b3a05: Pull complete
Digest: sha256:28ef97b8686a0b5399129e9b763d5b7e5ff03576aa5580d6f4182a49c5fe1913
Status: Downloaded newer image for alpine:latest
root@ubuntu:~#

使用 docker images 命令查看镜像

root@ubuntu:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              cdf98d1859c1        2 weeks ago         5.53MB
ubuntu              18.04               94e814e2efa8        6 weeks ago         88.9MB
root@ubuntu:~#

可以看到 alpine 镜像只有 5.53MB,而 ubuntu 镜像则有 88.9MB,Alpine 的体积优势非常明显。

下面运行镜像

root@ubuntu:~# docker run -it --name myalpine alpine
/ #

Alpine 的基本配置

1、网络相关配置

主机名文件

/etc/hostname

使用新设置的主机名立刻生效, 执行如下命令:

hostname -F /etc/hostname   

主机 IP 和域名映射文件

/etc/hosts

文件内容为:

127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      a9efe865a8e6
192.168.43.121   www.urlos.com

DNS 服务器配置文件

/etc/resolv.conf

文件内容为:

#neeanew Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 192.168.43.1

网卡配置文件

/etc/network/interfaces

文件内容为:

auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
       address 192.168.43.121
       netmask 255.255.255.0
       gateway 192.168.43.1

修改完相关配置以后,重新启动网络服务:

/etc/init.d/networking restart

2、更新国内源

Alpine 的源文件为:

/etc/apk/repositories

默认的源地址为:http://dl-cdn.alpinelinux.org/

可以编辑源文件 /etc/apk/repositories

采用国内阿里云的源,文件内容为:

https://mirrors.aliyun.com/al…

https://mirrors.aliyun.com/al…

如果采用中国科技大学的源,文件内容为:

https://mirrors.ustc.edu.cn/a…

https://mirrors.ustc.edu.cn/a…

软件包管理工具 apk 的基本使用

alpine 提供了非常好用的 apk 软件包管理工具,

可以方便地安装、删除、更新软件。

查询相关的软件包

命令:apk search,如查询 vim 软件包:

/ # apk search vim
neovim-doc-0.2.0-r0
faenza-icon-theme-vim-1.3.1-r4
docker-vim-17.05.0-r0
vim-doc-8.0.0595-r0
py-jinja2-vim-2.9.6-r0
vimdiff-8.0.0595-r0
asciidoc-vim-8.6.9-r2
neovim-lang-0.2.0-r0
vim-8.0.0595-r0
neovim-0.2.0-r0
nginx-vim-1.12.2-r2
msmtp-vim-1.6.6-r1
protobuf-vim-3.1.0-r1
gst-plugins-base1-1.10.4-r1
mercurial-vim-4.5.2-r0
/ #

安装软件包

命令:apk add , 如安装 vim 软件包:

/ # apk add vim
(1/5) Installing lua5.2-libs (5.2.4-r2)
(2/5) Installing ncurses-terminfo-base (6.0_p20171125-r1)
(3/5) Installing ncurses-terminfo (6.0_p20171125-r1)
(4/5) Installing ncurses-libs (6.0_p20171125-r1)
(5/5) Installing vim (8.0.0595-r0)
Executing busybox-1.29.3-r10.trigger
OK: 39 MiB in 19 packages
/ #

卸载软件

命令:apk del ,如卸载 vim 软件:

/ # apk del vim
(1/5) Purging vim (8.0.0595-r0)
(2/5) Purging lua5.2-libs (5.2.4-r2)
(3/5) Purging ncurses-libs (6.0_p20171125-r1)
(4/5) Purging ncurses-terminfo (6.0_p20171125-r1)
(5/5) Purging ncurses-terminfo-base (6.0_p20171125-r1)
Executing busybox-1.29.3-r10.trigger
OK: 6 MiB in 14 packages
/ #

获取更多 apk 包管理的命令参数

命令:apk --help

以上是 Alpine 最基本的使用方法,更多 URLOS 开发教程与 docker 容器教程请访问 URLOS 官网获取。

URLOS 官网:https://www.urlos.com/
URLOS 安装方法:https://www.urlos.com/center-…
URLOS 开发交流 QQ 群:695164700,147882180
URLOS 微信公众号:

正文完
 0