Dockerfile构建Nginx访问说明

Dockerfile使用情况 20210903

Dockerfile ,Nginx

参考地址:https://yeasy.gitbook.io/docker_practice/image/build

编写简单Dockerfile

在一个空白目录中,建立一个文本文件,并命名为 Dockerfile:

复制代码
$ mkdir mynginx
$ cd mynginx
$ touch Dockerfile

其内容为:

复制代码
FROM nginx
RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html

这个 Dockerfile 很简单,一共就两行。涉及到了两条指令,FROM 和 RUN。

可以单独修改编辑docker容器的内容

复制代码
docker exec -it webserver bash  |  /bin/bash

或者

docker exec -it webserver sh |  bash  | /bin/sh

echo '<h1>Hello,Docker!<h1>' > /usr/share/nginx/html/index.html

exit

镜像构建

在 Dockerfile 文件所在目录执行:

复制代码
[root@VM_0_12_centos Dockerfile]# docker build -t nginx:v1 .
Sending build context to Docker daemon 2.048 kB
Step 1/2 : FROM nginx
Trying to pull repository docker.io/library/nginx ... 
latest: Pulling from docker.io/library/nginx
a330b6cecb98: Pull complete 
5ef80e6f29b5: Pull complete 
f699b0db74e3: Pull complete 
0f701a34c55e: Pull complete 
3229dce7b89c: Pull complete 
ddb78cb2d047: Pull complete 
Digest: sha256:c442ff5a3d9f6dc92dd8aee388ece42d965e002805e3edaaff34e79d8ee6d0a4
Status: Downloaded newer image for docker.io/nginx:latest
 ---> 822b7ec2aaf2
Step 2/2 : RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html
 ---> Running in 72c5984eb1fa

 ---> 2072861ac194
Removing intermediate container 72c5984eb1fa
Successfully built 2072861ac194

镜像构建上下文(Context)

docker build -t nginx:v3 . 中的这个 .,实际上是在指定上下文的目录,docker build 命令会将该目录下的内容打包交给 Docker 引擎以帮助构建镜像。

运行构建的镜像

复制代码
[root@VM_0_12_centos Dockerfile]# docker run --name nginx-v1 -p 8080:80 -d nginx:v1
7e6958716a150418412148b9f029ef7ed9f10b672e5b6f48520ed2f2968a2d67

网页验证

网页中录入地址:ip:8080

或者 命令行验证: curl ip:8080

结果:Hello, Docker!

错误

当docker的映射端口已运行,但是端口访问不通,网络端口ping不通,docker容器内可访问

curl: (56) Recv failure: Connection reset by peer

ifconfig发现docker容器和物理机的ip网段在同一网段。

  • 通过netstat看看宿主机相关端口是否listen中,端口监听是否成功: netstat -antlp | grep 8088
  • ipv4是否开启,net.ipv4.ip_forward = 1; cat /proc/sys/net/ipv4/ip_forward // 0:未开启,1:已开启
  • 宿主机的防火墙,systemctl stop firewalld
  • 容器中的相关服务是否已经开启(比如nginx、apache)
  • 容器中netstat与尝试curl一下自身的服务端口
  • 查看iptables规则 iptables -L;iptables -t nat -nL 查看端口

如果上面都没发现问题,尝试使用host连接类型,让容器直接使用宿主机的端口

查看端口: docker port 容器名/容器id

//"bip": "172.17.0.1/16",

重建docker0网桥

复制代码
$ sudo service docker stop / systemctl stop docker
$ sudo pkill docker
$ sudo iptables -t nat -F
$ sudo ifconfig docker0 down
$ sudo brctl delbr docker0
$ sudo service docker start / systemctl start docker
相关推荐
草莓熊Lotso40 分钟前
【Linux系统加餐】从原理到封装:基于建造者模式实现System V信号量工业级C++封装
android·linux·运维·服务器·网络·c++·建造者模式
广州灵眸科技有限公司7 小时前
瑞芯微(EASY EAI)RV1126B 核心板供电电路
linux·运维·服务器·单片机·嵌入式硬件·电脑
keyipatience7 小时前
18.Linux进程退出和进程等待机制详解
linux·运维·服务器
仙柒4157 小时前
控制平面组件和节点组件
运维·容器·kubernetes
齐齐大魔王7 小时前
Linux-网络编程实战
linux·运维·网络
wanhengidc8 小时前
私有云的作用都有哪些?
运维·服务器·网络·游戏·智能手机
花阴偷移8 小时前
Ubuntu 22.04版本下配置静态IP
linux·运维·服务器·tcp/ip·ubuntu
牛马十年9 小时前
当自动化运维系统被ai重构后
运维·人工智能·自动化