侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

在Docker容器中部署静态网页的方法教程

Docker  /  管理员 发布于 7年前   291

前言

一般我们在访问容器时需要通过容器的端口来访问,那如何设置容器的端口映射呢?

我们通过以下命令来设置:

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/bashroot@25fcbf6e953d:/# apt-get install -y nginxReading package lists... DoneBuilding dependency tree  Reading state information... DoneE: Unable to locate package nginxroot@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... Doneroot@25fcbf6e953d:/# apt-get install -y nginxReading package lists... DoneBuilding dependency tree  Reading state information... Done**安装日志省略。。。。。。。。。。**

然后安装vim:

root@25fcbf6e953d:/# apt-get install -y vimReading package lists... DoneBuilding dependency tree  Reading state information... DoneThe following additional packages will be installed:**安装日志省略。。。。。。。。。。**

然后建立存放静态文件的目录,并创建一个简单的html文件:

root@25fcbf6e953d:/# mkdir -p /var/www/htmlroot@25fcbf6e953d:/# root@25fcbf6e953d:/# cd /var/www/html/root@25fcbf6e953d:/var/www/html# vim index.htmlroot@25fcbf6e953d:/var/www/html# ll total 8drwxr-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.htmlroot@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 nginxnginx: /usr/sbin/nginx /etc/nginx /usr/share/nginxroot@25fcbf6e953d:/var/www/html# cd /etc/nginx/sites-enabled/root@25fcbf6e953d:/etc/nginx/sites-enabled# lltotal 4drwxr-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/defaultroot@25fcbf6e953d:/etc/nginx/sites-enabled# vim default 

然后启动nginx,查看容器进程:

root@25fcbf6e953d:/etc/nginx/sites-enabled# nginxroot@25fcbf6e953d:/etc/nginx/sites-enabled# ps -ef UID  PID PPID C STIME TTY   TIME CMDroot   1  0 0 04:48 ?  00:00:00 /bin/bashroot  827  1 0 05:06 ?  00:00:00 nginx: master process nginxwww-data 828 827 0 05:06 ?  00:00:00 nginx: worker processwww-data 829 827 0 05:06 ?  00:00:00 nginx: worker processwww-data 830 827 0 05:06 ?  00:00:00 nginx: worker processwww-data 831 827 0 05:06 ?  00:00:00 nginx: worker processwww-data 832 827 0 05:06 ?  00:00:00 nginx: worker processwww-data 833 827 0 05:06 ?  00:00:00 nginx: worker processwww-data 834 827 0 05:06 ?  00:00:00 nginx: worker processwww-data 835 827 0 05:06 ?  00:00:00 nginx: worker processroot  836  1 0 05:07 ?  00:00:00 ps -efroot@25fcbf6e953d:/etc/nginx/sites-enabled# 

然后按ctrl+p,ctrl+q退出交互式容器。

使用Docker ps查看容器的端口和运行情况,也可以通过dcoker port 镜像名查看:

root@localhost ~]# docker psCONTAINER ID  IMAGE       COMMAND     CREATED    STATUS    PORTS     NAMES25fcbf6e953d  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_test80/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_teststatic_test[root@localhost ~]# docker ps CONTAINER ID  IMAGE       COMMAND     CREATED    STATUS    PORTS     NAMESb4f32bbe4a34  hub.c.163.com/library/ubuntu "/bin/bash"    41 hours ago  Up 41 hours         loving_brattaind75a2d8c7822  xingguo/df_test1    "nginx -g 'daemon off" 2 days ago   Up 2 days   0.0.0.0:32770->80/tcp df_nginx_web959c0fc5d903  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_teststatic_test[root@localhost ~]# docker ps CONTAINER ID  IMAGE       COMMAND     CREATED    STATUS    PORTS     NAMES25fcbf6e953d  hub.c.163.com/library/ubuntu "/bin/bash"    29 minutes ago  Up 2 seconds  0.0.0.0:32773->80/tcp static_testb4f32bbe4a34  hub.c.163.com/library/ubuntu "/bin/bash"    41 hours ago  Up 41 hours         loving_brattaind75a2d8c7822  xingguo/df_test1    "nginx -g 'daemon off" 2 days ago   Up 2 days   0.0.0.0:32770->80/tcp df_nginx_web959c0fc5d903  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_testUID     PID     PPID    C     STIME    TTY     TIME    CMDroot    4719    4702    0     13:17    pts/2    00:00:00   /bin/bash[root@localhost ~]# docker exec static_test nginx[root@localhost ~]# docker top static_testUID     PID     PPID    C     STIME    TTY     TIME    CMDroot    4719    4702    0     13:17    pts/2    00:00:00   /bin/bashroot    4800    1     0     13:18    ?     00:00:00   nginx: master process nginxamandab+   4801    4800    0     13:18    ?     00:00:00   nginx: worker processamandab+   4802    4800    0     13:18    ?     00:00:00   nginx: worker processamandab+   4803    4800    0     13:18    ?     00:00:00   nginx: worker processamandab+   4804    4800    0     13:18    ?     00:00:00   nginx: worker processamandab+   4805    4800    0     13:18    ?     00:00:00   nginx: worker processamandab+   4806    4800    0     13:18    ?     00:00:00   nginx: worker processamandab+   4807    4800    0     13:18    ?     00:00:00   nginx: worker processamandab+   4808    4800    0     13:18    ?     00:00:00   nginx: worker process[root@localhost ~]# 

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如有疑问大家可以留言交流,谢谢大家对的支持。


  • 上一条:
    docker 如何删除none镜像
    下一条:
    Docker 网络命令详解
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在docker环境中实现Laravel项目执行定时任务和消息队列流程步骤(0个评论)
    • 在MacBook下laravel项目多php版本docker开发环境配置方案(0个评论)
    • 在docker环境中部署docker部署elk架构流程步骤(1个评论)
    • docker compose跟Dockerfile的区别浅析(0个评论)
    • Ubuntu 22.04系统中安装podman流程步骤(1个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-11
    • 2020-06
    • 2021-05
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-11
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客