Nginx容器实现百度首页展示


[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos7-ssh latest 682d95be6162 2 hours ago 611MB
nginx latest 2622e6cca7eb 4 weeks ago 132MB
ashince/tomcat8 latest 02aedead27dd 23 months ago 314MB
ansible/centos7-ansible latest 688353a31fde 3 years ago 447MB
[root@localhost ~]# docker run -itd -p 80:80 nginx
b2dacab3f692ffa75299487cbd0a45c5636120a77578f104af3cbded9901bf85

-t,tty打开登陆终端;
-d,detach后台运行、启动;
-p,publish发布端口,将宿主机80(第一个)DNAT映射至容器的80;第一个80端口是宿主机的端口,这个端是宿主机上面需要开启的,如果宿主机上80端口被占用了是不行的,通过DNAT映射

获取百度首页的index.html 

#获取百度首页
[root@localhost ~]# wget www.baidu.com
--2020-07-10 16:40:54-- http://www.baidu.com/
Resolving www.baidu.com (www.baidu.com)... 180.101.49.11, 180.101.49.12
Connecting to www.baidu.com (www.baidu.com)|180.101.49.11|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2381 (2.3K) [text/html]
Saving to: ‘index.html’

100%[================================================================================================>] 2,381 --.-K/s in 0s

2020-07-10 16:40:55 (268 MB/s) - ‘index.html’ saved [2381/2381]

[root@localhost ~]# ls
anaconda-ks.cfg index.html

 Nginx容器是轻量级虚拟机

#可以看到这些命令都没有,因为是轻量级的虚拟机,和完整的操作系统虚拟机是不一样的
你可能心里面会有数不清楚的CNM奔腾而过,没错轻量级虚拟机没有为你提供很多命令
不过并不妨碍nginx实现页面为百度首页
[root@localhost ~]# docker exec -it b2dacab3f692 /bin/bash
root@b2dacab3f692:/# ifconfig
bash: ifconfig: command not found
root@b2dacab3f692:/# vi
bash: vi: command not found
root@b2dacab3f692:/# yum
bash: yum: command not found

#还好find命令可以使用,找到了发布目录和index.html文件
root@b2dacab3f692:/# find / -type f -name index.html 2>/dev/null
/usr/share/nginx/html/index.html


#退出容器,将百度的index.html文件拷贝到容器nginx的发布目录下面
root@b2dacab3f692:/# exit
exit
[root@localhost ~]# docker cp index.html b2dacab3f692:/usr/share/nginx/html/

#查看容器nginx发布目录下面的index.html文件
[root@localhost ~]# docker exec b2dacab3f692 ls /usr/share/nginx/html
50x.html
index.html

访问成功! 

Docker Nginx容器的简单操作与使用_nginx