本节:学会如何自己封装一个完整的镜像,将nginx容器化并且优化该镜像
容器启动后,宿主机能上网,容器也能上网。
基础的镜像的选取,通过docker查找拉取
[root@docker1 yum.repos.d]# docker search rhel7
STARS是下载量
前面带有名字的都不是官方镜像,有可能有问题。
[root@docker1 yum.repos.d]# docker search centos
为了选择自己想要的版本,去docker官方仓库查看
https://hub.docker.com/
首先要清除前面测试的镜像
^以什么开头
[root@docker1 docker]# docker rmi `docker images | grep ^test | awk '{print $1":"$2}'`
在本地先下载一个系统的镜像
-i指定导入的系统镜像,加个bash进去交互。
[root@docker1 docker]# docker load -i rhel7.tar
e1f5733f050b: Loading layer [==================================================>] 147.1MB/147.1MB
[root@docker1 docker]# docker run -it --name demo rhel7:latest bash
版本是7.0的
bash-4.2# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.0 (Maipo)
现在容器里面里面安装Nginx,寻找需要什么资源后,在宿主机Dockerfile里面写相关资源提交镜像后,Nginx便容器化了。
容器中:
修改yum仓库
bash-4.2# cd /etc/yum.repos.d/
bash-4.2# vi rhel7.repo
bash-4.2# cat rhel7.repo
[dvd]
name = rhel7
baseurl = http://172.25.138.250/westos
gpgcheck=0
enabled =1
[root@docker1 docker]# ls
Dockerfile index.html nginx-1.18.0.tar.gz rhel7.tar
[root@docker1 docker]# tar zxf nginx-1.18.0.tar.gz
[root@docker1 docker]# docker container cp nginx-1.18.0 demo:/
[root@docker1 docker]# docker container attach demo
bash-4.2# ls
bin dev home lib64 mnt nginx-1.18.0.tar.gz proc run srv tmp var
boot etc lib media nginx-1.18.0 opt root sbin sys usr
nginx编译需要的包
bash-4.2# yum install gcc pcre-devel openssl-devel
Error: libselinux conflicts with fakesystemd-1-14.el7.x86_64
openssl与centos依赖性冲突了,所以就不装openssl
7.0升级到7.6
Rpmdb损坏了,要升级索引数据库。
bash-4.2# yum install gcc pcre-devel -y
Rpmdb checksum is invalid: dCDPT(pkg checksums): glibc-devel.x86_64 0:2.17-260.el7 - u
bash-4.2# rpmdb --rebuilddb
bash-4.2# cd nginx-1.18.0/
bash-4.2# ls
CHANGES CHANGES.ru LICENSE README auto conf configure contrib html man src
bash-4.2# ./configure --prefix=/usr/local/nginx
报错:缺少了zlib的库,这个库如果安装openssl便自动安装进去了
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
编译成功
bash-4.2# ./configure --prefix=/usr/local/nginx
在宿主机里面:
/usr/local/nginx/html是nginx的默认发布目录
/usr/local/nginx/sbin/nginx运行nginx
查看一下nginx官方如何启动Nginx
[root@docker1 docker]# docker search nginx
nginx Official build of Nginx. 16202 [OK]
每次拉取都是镜像的最新版,详细看inspect,大概看history
[root@docker1 docker]# docker pull nginx
[root@docker1 docker]# docker inspect nginx:latest
自己写必须绝对路径而不是Nginx就行
Dockerfile写法:
[root@docker1 docker]# vim Dockerfile
[root@docker1 docker]# cat Dockerfile
FROM rhel7
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
COPY dvd.repo /etc/yum.repos.d/dvd.repo
RUN rpmdb --rebuilddb
RUN yum install -y gcc pcre-devel zlib-devel
ADD nginx-1.18.0.tar.gz /mnt
WORKDIR /mnt/nginx-1.18.0
RUN ./configure --prefix=/usr/local/nginx
RUN make
RUN make install
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
[root@docker1 docker]# docker build -t webserver:v1 .
Step 10/12 : RUN make
---> Running in 1871c9076c77
/bin/sh: make: command not found
所以需要修改一下Dockerfile
不要动上面好的部分,你在yum gcc那块添加的话,后面执行便不能用cache了,而且还可能改错。
FROM rhel7
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
COPY dvd.repo /etc/yum.repos.d/dvd.repo
RUN rpmdb --rebuilddb
RUN yum install -y gcc pcre-devel zlib-devel
ADD nginx-1.18.0.tar.gz /mnt
WORKDIR /mnt/nginx-1.18.0
RUN ./configure --prefix=/usr/local/nginx
RUN yum install make -y
RUN make
RUN make install
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
继续安装
[root@docker1 docker]# docker build -t webserver:v1 .
Successfully tagged webserver:v1
[root@docker1 docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
webserver v1 bdd2829a42e3 31 seconds ago 346MB
可以删掉之前做实验的容器了
[root@docker1 docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1871c9076c77 644179a9b6e3 "/bin/sh -c make" 6 minutes ago Exited (127) 6 minutes ago modest_buck
7aaa2d2b53da rhel7:latest "bash" 43 hours ago Exited (130) 16 minutes ago demo
[root@docker1 docker]# docker rm 1871c9076c77 7aaa2d2b53da
启动nginx的镜像
[root@docker1 docker]# docker run -d --name nginx webserver:v1
[root@docker1 docker]# docker inspect nginx
"IPAddress": "172.17.0.2",
"Source": "/var/lib/docker/volumes/70f0dede14b7c1978edb38eb49534509f1fcc205e750bb365254b75677b76005/_data",
发布失败,因为在inspect查到mount里面的宿主机挂载点覆盖点我们的卷了
[root@docker1 docker]# curl 172.17.0.2
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>
重新测试:
[root@docker1 docker]# cd /var/lib/docker/volumes/70f0dede14b7c1978edb38eb49534509f1fcc205e750bb365254b75677b76005/_data
[root@docker1 _data]# vim index.html
[root@docker1 _data]# cat index.html
www.yan.org
[root@docker1 _data]# curl 172.17.0.2
www.yan.org
Nginx容器化成功