Docker网络模式
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1d8c2d452615 centos7 "bash" 2 hours ago Up 2 hours clever_merkle
10f889af7131 centos_with_net "bash" 22 hours ago Up 22 hours frosty_hofstadter
aae2e49e78dd registry "/entrypoint.sh /etc…" 24 hours ago Up 24 hours 0.0.0.0:5000->5000/tcp gifted_shaw
[root@localhost ~]# docker exec -it 10f889af7131 bash
[root@10f889af7131 /]# yum install -y epel-release //安装nginx前选装epel
[root@10f889af7131 /]# yum install -y nginx
[root@10f889af7131 /]# exit
把容器导出为镜像:
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1d8c2d452615 centos7 "bash" 2 hours ago Up 2 hours clever_merkle
10f889af7131 centos_with_net "bash" 23 hours ago Up 22 hours frosty_hofstadter
aae2e49e78dd registry "/entrypoint.sh /etc…" 24 hours ago Up 24 hours 0.0.0.0:5000->5000/tcp gifted_shaw
[root@localhost ~]# docker commit -m "install nginx" -a "zhangfeilinux" 10f889af7131 centos_with_nginx
sha256:341c00695ef74e1a29859cf3c9b428c32760a32df06aac0520fbf9a5f93ce815
[root@localhost ~]# docker run -itd -p 8088:80 centos_with_nginx bash //访问centos_with_nginx的80端口就直接访问的宿主机的8088端口
74d9d8ed0188d9a8b880e44a2936d923b380105132df8ab646eebffc3c7419fc
25.11 operation not permitted
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
74d9d8ed0188 centos_with_nginx "bash" 18 hours ago Up 18 hours 0.0.0.0:8088->80/tcp pedantic_torvalds
1d8c2d452615 centos7 "bash" 20 hours ago Up 20 hours clever_merkle
10f889af7131 centos_with_net "bash" 40 hours ago Up 40 hours frosty_hofstadter
aae2e49e78dd registry "/entrypoint.sh /etc…" 42 hours ago Up 42 hours 0.0.0.0:5000->5000/tcp gifted_shaw
[root@localhost ~]# docker exec -it 74d9d8ed0188 bash
[root@74d9d8ed0188 /]# systemctl start nginx
Failed to get D-Bus connection: Operation not permitted
解决以上问题:
[root@localhost ~]# docker rm -f 74d9d8ed0188
74d9d8ed0188
[root@localhost ~]# docker run -itd --privileged -e "container=docker" -p 8080:80 centos_with_nginx /usr/sbin/init
776ebf82eb2edce4560fc327e1ae2260064bb9f0dc5b9bea2b62f7ae8ea07bc7
[root@localhost ~]# docker exec -it 776ebf82eb2edce45 bash
[root@776ebf82eb2e /]# systemctl start nginx
[root@776ebf82eb2e /]# ps aux |grep nginx
root 88 0.0 0.1 120812 2096 ? Ss 15:20 0:00 nginx: master process /usr/sbin/nginx
nginx 89 0.1 0.1 121276 3124 ? S 15:20 0:00 nginx: worker process
nginx 90 0.0 0.1 121276 3124 ? S 15:20 0:00 nginx: worker process
root 92 0.0 0.0 9092 660 pts/1 S+ 15:21 0:00 grep --color=auto nginx
在192.168.137.21上执行:
[root@zhangfei-02 ~]# curl 192.168.137.20:8080 //通过访问宿主机的8080端口访问容器里的80端口