对外发布服务
docker run -itd -p 宿主机端口:容器端口 镜像名称:标签
# 把 docker-0002 绑定 apache 服务
[root@docker-0002 ~]# docker run -itd -p 80:80 myapache:latest
# 在 ecs-proxy主机 上访问验证
[root@ecs-proxy ~]# curl 192.168.1.32
Welcome to The Apache.
# 把 docker-0002 绑定 nginx 服务,删除 apache 的容器
[root@docker-0002 ~]# docker rm -f $(docker ps -aq)
[root@docker-0002 ~]# docker run -itd -p 80:80 mynginx:latest
# 在 ecs-proxy主机 上访问验证
[root@ecs-proxy ~]# curl 192.168.1.32
Nginx is running !
容器共享卷
docker run -itd -v 宿主机对象:容器内对象 镜像名称:标签
共享网页目录
[root@docker-0002 ~]# mkdir /var/webroot
[root@docker-0002 ~]# echo "hello world" >> /var/webroot/index.html
[root@docker-0002 ~]# cp /root/apache/info.php /var/webroot/
[root@docker-0002 ~]# docker rm -f $(docker ps -aq)
[root@docker-0002 ~]# docker run -itd --name mynginx -v /var/webroot:/usr/local/nginx/html mynginx:latest
[root@docker-0002 ~]# curl http://172.17.0.2
hello world
测试:
[root@docker-0002 ~]# echo "hello world" >> /var/webroot/index.html
[root@docker-0002 ~]# echo "hello world" >> /var/webroot/index.html
[root@docker-0002 ~]# curl 172.17.0.2
hello world
hello world
hello world
[root@docker-0002 ~]# docker run -itd --name myhttpd -v /var/webroot:/var/www/html myapache:latest
[root@docker-0002 ~]# curl 172.17.0.3
hello world
hello world
hello world
[root@docker-0002 ~]# curl 172.17.0.3/info.php #apache正常解析动态页面
<pre>
Array
(
[REMOTE_ADDR] => 172.17.0.1
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /info.php
)
php_host: c73bc24c455f
1229
修改nginx的配置文件
nginx则不行,需要另行配置解析php页面
[root@docker-0002 ~]# mkdir /var/webconf
[root@docker-0002 ~]# docker cp mynginx:/usr/local/nginx/conf/nginx.conf /var/webconf/
[root@docker-0002 ~]# vim /var/webconf/nginx.conf
65 location ~ \.php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 include fastcgi.conf;
70 }
[root@docker-0002 ~]# docker rm -f $(docker ps -aq)
[root@docker-0002 ~]# docker run -itd --name mynginx -v /var/webconf/nginx.conf:/usr/local/nginx/conf/nginx.conf -v /var/webroot:/usr/local/nginx/html mynginx:latest
[root@docker-0002 ~]# curl 172.17.0.2/info.php #显示404 Not Found
通过连接nginx的容器查看fastcgi转发127.0.0.1:9000失败,nginx中没有9000
[root@docker-0002 ~]# docker exec -it mynginx /bin/bash
[root@0214c6744ffa html]# cat ../logs/error.log
2023/06/12 17:10:08 [error] 7#0: *6 open() "/usr/local/nginx/html/50x.html" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "172.17.0.2"
容器间网络通信
实验步骤
演示共享其他容器的网络命名空间
在docker-0002新开两个终端,第一个终端执行以下命令
[root@docker-0002 ~]# docker run -it --name a1 mycentos:latest
[root@8366137bda56 /]#
第二个终端执行,启动容器,共享h1的网络
[root@docker-0002 ~]# docker run -it --network=container:a1 mycentos:latest
验证网络共享:
此时使用ifconfig查看网卡,他们的网卡ip是一样的
在第一个终端的容器安装httpd,第二个查看,可以看到httpd的端口
[root@8366137bda56 /]# yum -y install httpd
[root@8366137bda56 /]# /usr/sbin/httpd
[root@8366137bda56 /]# ss -antlp #有端口
第二个终端查看
[root@8366137bda56 /]# ss -antlp #有端口
第一个终端杀死程序,查看监听端口,已经没有了
[root@8366137bda56 /]# killall httpd
把nginx和php-fpm共享网络
[root@docker-0002 ~]# docker run -itd --name myphp --network=container:mynginx -v /var/webroot:/usr/local/nginx/html phpfpm:latest
[root@docker-0002 ~]# docker exec -it mynginx ss -antlp
Netid State Recv-Q Send-Q Local Address:Port
tcp LISTEN 0 128 127.0.0.1:9000
tcp LISTEN 0 128 *:80
[root@docker-0002 ~]# curl 172.17.0.2/info.php
<pre>
Array
(
[REMOTE_ADDR] => 172.17.0.1
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /info.php
)
php_host: f705f89b45f9
1229