Docker镜像与制作
- Docker镜像有没有内核
- 从镜像大小上面来说,一个比较小的镜像只有十几 MB,而内核文件需要一百多兆, 因此镜像里面是没有内核的,镜像在被启动为容器后将直接使用宿主机的内核,而镜像本身则只提供相应的 rootfs,即系统正常运行所必须的用户空间的文件系统,比如/dev/,/proc,/bin,/etc 等目录,所以容器当中基本是没有/boot目录的,而/boot 当中保存的就是与内核相关的文件和目录。
- 为什么没有内核?
- 由于容器启动和运行过程中是直接使用了宿主机的内核,所以没有直接调用过物理硬件,所以也不会涉及到硬件驱动,因此也用不上内核和驱动,另外有内核的那是虚拟机。
手动制作yum版nginx镜像
- Docker 制作类似于虚拟机的模板制作,即按照公司的实际业务务求将需要安装的软件、相关配置等基础环境配置完成,然后将虚拟机再提交为模板,最后再批量从模板批量创建新的虚拟机,这样可以极大的简化业务中相同环境的虚拟机运行环境的部署工作,Docker 的镜像制作分为手动制作和自动制作(基于DockerFile),企业通常都是基于 Dockerfile 制作镜像,其中手动制作镜像步骤具体如下:
下载镜像并初始化系统
- 基于某个基础镜像之上重新制作,因此需要先有一个基础镜像,本次使用官方提供的 centos 镜像为基础:
root@bj-magedu-v-study-14:~# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
8a29a15cefae: Pull complete
Digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700
Status: Downloaded newer image for centos:latest
root@bj-magedu-v-study-14:~# docker run -it centos /bin/bash
[root@e5f054dd2fd1 /]# yum install wget -y
[root@e5f054dd2fd1 /]# cd /etc/yum.repos.d/
[root@e5f054dd2fd1 /]# rm -rf ./* #删除原来的源文件
[root@e5f054dd2fd1 /]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo #添加Base源
[root@e5f054dd2fd1 /]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo #添加epel源
yum安装并配置nginx
[root@e5f054dd2fd1 /]# yum -y install nginx #yum 安装 nginx
[root@e5f054dd2fd1 /]# yum install -y vim wget pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop #安装常用命令
关闭nginx后台运行
- vim /etc/nginx/nginx.conf #编辑配置文件,关闭nginx后台运行

自定义web页面
[root@c544a9d519b3 /]# cd /usr/share/nginx/html/
[root@c544a9d519b3 html]# ll
total 16
-rw-r--r-- 1 root root 3650 Oct 3 05:12 404.html
-rw-r--r-- 1 root root 3693 Oct 3 05:12 50x.html
lrwxrwxrwx 1 root root 20 Feb 18 08:10 en-US -> ../../doc/HTML/en-US
drwxr-xr-x 2 root root 4096 Feb 18 08:10 icons
lrwxrwxrwx 1 root root 18 Feb 18 08:10 img -> ../../doc/HTML/img
lrwxrwxrwx 1 root root 25 Feb 18 08:10 index.html -> ../../doc/HTML/index.html
-rw-r--r-- 1 root root 368 Oct 3 05:12 nginx-logo.png
lrwxrwxrwx 1 root root 14 Feb 18 08:10 poweredby.png -> nginx-logo.png
[root@c544a9d519b3 html]# rm -rf index.html
[root@c544a9d519b3 html]# ll
total 16
-rw-r--r-- 1 root root 3650 Oct 3 05:12 404.html
-rw-r--r-- 1 root root 3693 Oct 3 05:12 50x.html
lrwxrwxrwx 1 root root 20 Feb 18 08:10 en-US -> ../../doc/HTML/en-US
drwxr-xr-x 2 root root 4096 Feb 18 08:10 icons
lrwxrwxrwx 1 root root 18 Feb 18 08:10 img -> ../../doc/HTML/img
-rw-r--r-- 1 root root 368 Oct 3 05:12 nginx-logo.png
lrwxrwxrwx 1 root root 14 Feb 18 08:10 poweredby.png -> nginx-logo.png
[root@c544a9d519b3 html]# echo 'Docker Yum Nginx' > /usr/share/nginx/html/index.html
[root@c544a9d519b3 html]# cat /usr/share/nginx/html/index.html
Docker Yum Nginx #自定义的web测试页面
提交为镜像
命令格式
docker commit [选项] 容器 [镜像仓库[:标签]
-a, --author string #作者
-c, --change list #对创建的映像应用Dockerfile指令
-m, --message string #提交消息
-p, -p, --pause #提交期间暂停容器(默认为true)
root@bj-magedu-v-study-14:~# docker commit -a "952098151@qq.com" -m "nginx yum v1" --change="EXPOSE 80 443" c544a9d519b3 centos-1804-v1
sha256:d72d0c61085a9548643f1b7044bd9c666548a9c48580537254b725b2fc41deaf
带tag的镜像提交
- 提交的时候标记 tag 号
- 标记 tag 号,生产当中比较常用,后期可以根据 tag 标记创建不同版本的镜像以
及创建不同版本的容器。
//Example
root@bj-magedu-v-study-14:~# -m "nginx-new-image" c544a9d519b3 centos-1804-v1
从自己镜像启动容器
root@bj-magedu-v-study-14:~# docker commit -a "952098151@qq.com" -m "nginx yum v1" --change="EXPOSE 80 443" c544a9d519b3 centos-1804-v1
sha256:d72d0c61085a9548643f1b7044bd9c666548a9c48580537254b725b2fc41deaf
root@bj-magedu-v-study-14:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-1804-v1 latest d72d0c61085a 4 minutes ago 450MB
centos latest 470671670cac 4 weeks ago 237MB
centos centos7.5.1804 cf49811e3cdb 11 months ago 200MB
root@bj-magedu-v-study-14:~# docker run -d -p 80:80 --name my-centos-nginx d72d0c61085a /usr/sbin/nginx
782addd8a3dfd124e5b227ce0441eadc093807d76c360972b6c8cb54ec377de1
root@bj-magedu-v-study-14:~# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 127.0.0.53%lo:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 20480 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
root@bj-magedu-v-study-14:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
782addd8a3df d72d0c61085a "/usr/sbin/nginx" 33 seconds ago Up 32 seconds 0.0.0.0:80->80/tcp, 443/tcp my-centos-nginx
c544a9d519b3 centos:centos7.5.1804 "/bin/bash" 37 minutes ago Up 7 minutes centos-1804
root@bj-magedu-v-study-14:~#
访问测试
- 浏览器访问http://192.168.26.207/
