设置容器的端口映射
run [P] [p]
-p,--publish=[]
containerPort
docker run -p 80 -i -t ubuntu /bin/bash
hostPort:containerPort
docker run -p 8080:80 -i -t ubuntu /bin/bash
ip::containerPort
docker run -p 0.0.0.0:80 -i -t ubuntu /bin/bash
ip:hostPort:containerPort
docker run -p 0.0.0.0:8080:80 -i -t ubuntu /bin/bash
nginx部署流程
创建映射80端口的交互式容器
安装nginx、vim
创建静态页面
修改nginx配置文件
运行nginx
验证网站访问
[root@iz2ze20vl8jnph0si0n7jsz ~]# docker run -p 80 --name web -i -t ubuntu /bin/bash
root@a4166c05feef:/# apt-get update(docker容器 安装vim 安装nginx docker E: Unable to locate package nginxhttp://blog.csdn.net/u010098331/article/details/53492385)
root@a4166c05feef:/# apt-get install -y nginx
root@a4166c05feef:/# apt-get install vim
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@a4166c05feef:/# mkdir -p /var/www/html
root@a4166c05feef:/# cd /var/www/html/
root@a4166c05feef:/var/www/html# vim index.html
root@a4166c05feef:/var/www/html# ll
total 16
drwxr-xr-x 2 root root 4096 Feb 17 12:33 ./
drwxr-xr-x 3 root root 4096 Feb 17 12:29 ../
-rw-r--r-- 1 root root 94 Feb 17 12:33 index.html
root@a4166c05feef:/var/www/html# whereis nginx
nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx
root@a4166c05feef:/var/www/html# ls /etc/nginx/
conf.d fastcgi.conf fastcgi_params koi-utf koi-win mime.types nginx.conf proxy_params scgi_params sites-available sites-enabled snippets uwsgi_params win-utf
root@a4166c05feef:/var/www/html# ls /etc/nginx/sites-enabled/
default
root@a4166c05feef:/var/www/html# vim /etc/nginx/sites-enabled/default
root@a4166c05feef:/var/www/html# nginx
root@a4166c05feef:/var/www/html# ps -ef|grep nginx
root 869 1 0 12:36 ? 00:00:00 nginx: master process nginx
www-data 870 869 0 12:36 ? 00:00:00 nginx: worker process
www-data 871 869 0 12:36 ? 00:00:00 nginx: worker process
root 873 1 0 12:36 pts/0 00:00:00 grep --color=auto nginx
ctrl+p ctrl+q退出容器
访问验证
[root@iz2ze20vl8jnph0si0n7jsz ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a4166c05feef ubuntu "/bin/bash" 10 minutes ago Up 10 minutes 0.0.0.0:32768->80/tcp web
282bc392c684 ubuntu "/bin/bash -c 'while…" 28 minutes ago Up 28 minutes dc1
[root@iz2ze20vl8jnph0si0n7jsz ~]# docker port web
80/tcp -> 0.0.0.0:32768
[root@iz2ze20vl8jnph0si0n7jsz ~]# curl http://127.0.0.1:32768
<html>
<head>
<title>my docker</title>
</head>
<body>
<h1>hello docker</h1>
</body>
</html>
也可以使用容器的ip进行访问
[root@iz2ze20vl8jnph0si0n7jsz ~]# docker inspect web
……
[root@iz2ze20vl8jnph0si0n7jsz ~]# curl http://172.18.0.3
<html>
<head>
<title>my docker</title>
</head>
<body>
<h1>hello docker</h1>
</body>
</html>
[root@iz2ze20vl8jnph0si0n7jsz ~]#
重启容器
[root@iz2ze20vl8jnph0si0n7jsz ~]# docker stop web
web
[root@iz2ze20vl8jnph0si0n7jsz ~]# docker start -i web
root@a4166c05feef:/# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 12:46 pts/0 00:00:00 /bin/bash
root 9 1 0 12:46 pts/0 00:00:00 ps -ef
root@a4166c05feef:/# read escape sequence
[root@iz2ze20vl8jnph0si0n7jsz ~]# docker exec web nginx
[root@iz2ze20vl8jnph0si0n7jsz ~]# docker top web
UID PID PPID C STIME TTY TIME CMD
root 8332 8317 0 20:46 ? 00:00:00 /bin/bash
root 8404 8317 0 20:47 ? 00:00:00 nginx: master process nginx
33 8405 8404 0 20:47 ? 00:00:00 nginx: worker process
33 8406 8404 0 20:47 ? 00:00:00 nginx: worker process
容器的IP地址和映射端口都会发生变化