本篇内容介绍了“怎么在Docker容器中部署静态网页”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
我们通过以下命令来设置:
docker run -p ip:hostport:containerport [--name] [-i] [-t] 镜像名 [command][arg...]
ip:表示宿主机ip
hostport:宿主机端口号
containerport:容器端口号
设置的方式有以下几种:
containerport,指定容器端口号,宿主机端口随机生成
[root@localhost ~]# docker run -p 80 --name web_test -i -t 80864d42dd23 hub.c.163.com/library/ubuntu /bin/bash
hostport:containerport映射主机端口和容器端口
[root@localhost ~]# docker run -p 8080:80 --name web_test -i -t 80864d42dd23 hub.c.163.com/library/ubuntu /bin/bash
ip::containerport设置主机的随机端口到容器端口
[root@localhost ~]# docker run -p 0.0.0.0::80 --name web_test -i -t 80864d42dd23 hub.c.163.com/library/ubuntu /bin/bash
ip:hostport:containerport映射指定地址的指定端口到容器的指定端口
[root@localhost ~]# docker run -p 0.0.0.0:8080:80 --name web_test -i -t 80864d42dd23 hub.c.163.com/library/ubuntu /bin/bash
下面通过Nginx在容器部署静态网页,通过以下步骤
- 创建80映射端口的交互式容器
- 安装Nginx
- 安装文本编辑器vim
- 创建静态网页
- 运行Nginx
- 验证网页
示例如下(如果安装完ubuntu后不能安装Nginx进行apt-get update
):
[root@localhost ~]# docker run -p 80 --name static_test -i -t hub.c.163.com/library/ubuntu /bin/bash root@25fcbf6e953d:/# apt-get install -y Nginx reading package lists... done building dependency tree reading state @R_121_4045@ion... done e: unable to locate package Nginx root@25fcbf6e953d:/# apt-get update get:1 http://archive.ubuntu.com/ubuntu xenial inrelease [247 kb] get:2 http://archive.ubuntu.com/ubuntu xenial-updates inrelease [102 kb] **中间日志省略。。。。。。。。。。** fetched 24.9 mb in 9s (2717 kb/s) reading package lists... done root@25fcbf6e953d:/# apt-get install -y Nginx reading package lists... done building dependency tree reading state @R_121_4045@ion... done **安装日志省略。。。。。。。。。。**
然后安装vim:
root@25fcbf6e953d:/# apt-get install -y vim reading package lists... done building dependency tree reading state @R_121_4045@ion... done the following additional packages will be installed: **安装日志省略。。。。。。。。。。**
root@25fcbf6e953d:/# mkdir -p /var/www/html root@25fcbf6e953d:/# root@25fcbf6e953d:/# cd /var/www/html/ root@25fcbf6e953d:/var/www/html# vim index.html root@25fcbf6e953d:/var/www/html# ll total 8 drwxr-xr-x. 2 root root 53 mar 13 05:02 ./ drwxr-xr-x. 3 root root 17 mar 13 04:50 ../ -rw-r--r--. 1 root root 79 mar 13 05:02 index.html -rw-r--r--. 1 root root 612 mar 13 04:51 index.Nginx-debian.html root@25fcbf6e953d:/var/www/html# cat index.html <html> <body> <h1> this is the first docker static file </h1> </body> </html> root@25fcbf6e953d:/var/www/html#
找到Nginx的安装目录,更改default的root值为刚才创建的目录:
root@25fcbf6e953d:/var/www/html# whereis Nginx Nginx: /usr/sbin/Nginx /etc/Nginx /usr/share/Nginx root@25fcbf6e953d:/var/www/html# cd /etc/Nginx/sites-enabled/ root@25fcbf6e953d:/etc/Nginx/sites-enabled# ll total 4 drwxr-xr-x. 2 root root 20 mar 13 04:51 ./ drwxr-xr-x. 6 root root 4096 mar 13 04:51 ../ lrwxrwxrwx. 1 root root 34 mar 13 04:51 default -> /etc/Nginx/sites-available/default root@25fcbf6e953d:/etc/Nginx/sites-enabled# vim default
然后启动Nginx,查看容器进程:
root@25fcbf6e953d:/etc/Nginx/sites-enabled# Nginx root@25fcbf6e953d:/etc/Nginx/sites-enabled# ps -ef uid pid ppid c stime tty time cmd root 1 0 0 04:48 ? 00:00:00 /bin/bash root 827 1 0 05:06 ? 00:00:00 Nginx: master process Nginx www-data 828 827 0 05:06 ? 00:00:00 Nginx: worker process www-data 829 827 0 05:06 ? 00:00:00 Nginx: worker process www-data 830 827 0 05:06 ? 00:00:00 Nginx: worker process www-data 831 827 0 05:06 ? 00:00:00 Nginx: worker process www-data 832 827 0 05:06 ? 00:00:00 Nginx: worker process www-data 833 827 0 05:06 ? 00:00:00 Nginx: worker process www-data 834 827 0 05:06 ? 00:00:00 Nginx: worker process www-data 835 827 0 05:06 ? 00:00:00 Nginx: worker process root 836 1 0 05:07 ? 00:00:00 ps -ef root@25fcbf6e953d:/etc/Nginx/sites-enabled#
然后按ctrl+p,ctrl+q退出交互式容器。
使用docker ps
查看容器的端口和运行情况,也可以通过dcoker port
镜像名查看:
root@localhost ~]# docker ps container id image command created status ports names 25fcbf6e953d hub.c.163.com/library/ubuntu "/bin/bash" 18 minutes ago up 18 minutes 0.0.0.0:32772->80/tcp static_test [root@localhost ~]# docker port static_test 80/tcp -> 0.0.0.0:32772 [root@localhost ~]#
可以看到容器的80端口映射到注解的32772端口。
通过curl访问创建的index.html文件:
[root@localhost ~]# curl http://127.0.0.1:32772/index.html <html> <body> <h1> this is the first docker static file </h1> </body> </html> [root@localhost ~]#
也可以通过docker inspect
容器名来查看容器的详细信息和ip
通过容器ip进行访问:
[root@localhost ~]# docker inspect static_test 其他信息省略 "ipaddress": "172.17.0.5",
[root@localhost ~]# curl http://172.17.0.5/index.html <html> <body> <h1> this is the first docker static file </h1> </body> </html> [root@localhost ~]#
当我们使用docker stop
容器名停止这个容器后,再使用docker start
容器名启动容器后,里面的Nginx并没有启动。那么我们使用docker exec
容器名 Nginx来启动:
[root@localhost ~]# docker stop static_test static_test [root@localhost ~]# docker ps container id image command created status ports names b4f32bbe4a34 hub.c.163.com/library/ubuntu "/bin/bash" 41 hours ago up 41 hours loving_brattain d75a2d8c7822 xingguo/df_test1 "Nginx -g 'daemon off" 2 days ago up 2 days 0.0.0.0:32770->80/tcp df_Nginx_web 959c0fc5d903 xingguo/commit_test1 "Nginx -g 'daemon off" 2 days ago up 2 days 0.0.0.0:32769->80/tcp Nginx_test [root@localhost ~]# docker start static_test static_test [root@localhost ~]# docker ps container id image command created status ports names 25fcbf6e953d hub.c.163.com/library/ubuntu "/bin/bash" 29 minutes ago up 2 seconds 0.0.0.0:32773->80/tcp static_test b4f32bbe4a34 hub.c.163.com/library/ubuntu "/bin/bash" 41 hours ago up 41 hours loving_brattain d75a2d8c7822 xingguo/df_test1 "Nginx -g 'daemon off" 2 days ago up 2 days 0.0.0.0:32770->80/tcp df_Nginx_web 959c0fc5d903 xingguo/commit_test1 "Nginx -g 'daemon off" 2 days ago up 2 days 0.0.0.0:32769->80/tcp Nginx_test [root@localhost ~]# docker top static_test uid pid ppid c stime tty time cmd root 4719 4702 0 13:17 pts/2 00:00:00 /bin/bash [root@localhost ~]# docker exec static_test Nginx [root@localhost ~]# docker top static_test uid pid ppid c stime tty time cmd root 4719 4702 0 13:17 pts/2 00:00:00 /bin/bash root 4800 1 0 13:18 ? 00:00:00 Nginx: master process Nginx amandab+ 4801 4800 0 13:18 ? 00:00:00 Nginx: worker process amandab+ 4802 4800 0 13:18 ? 00:00:00 Nginx: worker process amandab+ 4803 4800 0 13:18 ? 00:00:00 Nginx: worker process amandab+ 4804 4800 0 13:18 ? 00:00:00 Nginx: worker process amandab+ 4805 4800 0 13:18 ? 00:00:00 Nginx: worker process amandab+ 4806 4800 0 13:18 ? 00:00:00 Nginx: worker process amandab+ 4807 4800 0 13:18 ? 00:00:00 Nginx: worker process amandab+ 4808 4800 0 13:18 ? 00:00:00 Nginx: worker process [root@localhost ~]#
“怎么在Docker容器中部署静态网页”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程之家网站,小编将为大家输出更多高质量的实用文章!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。