docker手动制作Nginx镜像
Docker镜像概念
镜像是没有内核的,与宿主机公用一个内核
为什么没有内核?
由于容器启动和运行过程中是直接使用了宿主机的内核,所以没有直接调用过
物理硬件,所以也不会涉及到硬件驱动,因此也用不上内核和驱动,另外有内核
的那是虚拟机。
验证镜像没有内核
[root@docker-10.0.0.119 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
765452196b77 nginx:latest "/docker-entrypoint.…" 2 days ago Exited (0) 2 days ago nginx-test
[root@docker-10.0.0.119 ~]# docker start 765452196b77
765452196b77
[root@docker-10.0.0.119 ~]#
[root@docker-10.0.0.119 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
765452196b77 nginx:latest "/docker-entrypoint.…" 2 days ago Up 2 seconds 0.0.0.0:81->80/tcp, :::81->80/tcp nginx-test
[root@docker-10.0.0.119 ~]#
[root@docker-10.0.0.119 ~]# docker exec -it 765452196b77 /bin/bash
root@765452196b77:/# ls /boot
root@765452196b77:/boot# ls
root@765452196b77:/boot# exit
docker运行nginx为什么要添加daemon of;
docker 容器默认会把容器内部第一个进程,也就是pid=1的程序作为docker容器是否正在运行的依据,如果docker 容器pid挂了,那么docker容器便会直接退出。
制作Nginx镜像
1、先下载centos镜像
[root@docker-10.0.0.119 ~]# docker pull centos:7
[root@docker-10.0.0.119 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 8 months ago 141MB
centos 7 eeb6ee3f44bd 12 months ago 204MB #pull下的centos7镜像
centos latest 5d0da3dc9764 12 months ago 231MB
2、基于镜像启动centos7容器
[root@docker-10.0.0.119 ~]# docker run -d -it centos:7
66fb85930b0b10a81d334138b272ab87b8eee412e6517f05e0d15e598f71ee4b
[root@docker-10.0.0.119 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
66fb85930b0b centos:7 "/bin/bash" 3 seconds ago Up 2 seconds suspicious_lichterman
3、进入容器更换yum源安装Nginx
[root@docker-10.0.0.119 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
66fb85930b0b centos:7 "/bin/bash" 3 seconds ago Up 2 seconds suspicious_lichterman
[root@docker-10.0.0.119 ~]# docker exec -it 66fb85930b0b /bin/bash
更换yum源,目前是没有Nginx源的
[root@66fb85930b0b /]# yum install nginx
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
* base: mirrors.huaweicloud.com
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
No package nginx available.
Error: Nothing to do
yum install wget #下载命令
创建存放旧源目录,然后移动到指定的位置
[root@66fb85930b0b yum.repos.d]# mkdir /tmp/yum
[root@66fb85930b0b /]# cd /etc/yum.repos.d/
[root@66fb85930b0b yum.repos.d]# mv * /tmp/yum
更换yum源
[root@66fb85930b0b yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@66fb85930b0b yum.repos.d]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
安装并配置Nginx
[root@66fb85930b0b yum.repos.d]# yum -y install tree nmap sysstat lrzsz dos2unix telnet bash-completion bash-completion-extras vim nc net-tools rsync
[root@66fb85930b0b yum.repos.d]# yum install nginx -y
关闭Nginx后台运行
[root@66fb85930b0b yum.repos.d]# head /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
daemon off; # 关闭后台运行
自定义web界面
[root@66fb85930b0b html]# cat index.html
wish_Docker_Nginx
[root@66fb85930b0b html]# pwd
/usr/share/nginx/html
然后退出
4、打包镜像
[root@docker-10.0.0.119 ~]# docker commit -m "centos" 66fb85930b0b wish/centos-nginx:v1
sha256:bad0b3835b53c7f69961c133478f0cbc1192a01b7e5de267598b4db3af6fc869
[root@docker-10.0.0.119 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
wish/centos-nginx v1 bad0b3835b53 4 seconds ago 567MB # 上一步打包的镜像
nginx latest 605c77e624dd 8 months ago 141MB
centos 7 eeb6ee3f44bd 12 months ago 204MB
centos latest 5d0da3dc9764 12 months ago 231MB
5、启动Nginx
[root@docker-10.0.0.119 ~]# docker run -d -it -p82:80 bad0b3835b53 /usr/sbin/nginx
764cd238d07fed8c450d4f64faf1ac13a6f819f2c6fa9c3cabccc5dc453e23a5
[root@docker-10.0.0.119 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
764cd238d07f bad0b3835b53 "/usr/sbin/nginx" 2 seconds ago Up 2 seconds 0.0.0.0:82->80/tcp, :::82->80/tcp exciting_proskuriakova # 启动的Nginx
66fb85930b0b centos:7 "/bin/bash" 33 minutes ago Up 33 minutes suspicious_lichterman
6、浏览器访问
10.0.0.119:82
这样就打工告成啦!