设置容器的端口映射
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
-P,--publish-all=true|false 默认false
- docker run -P -i -t ubuntu /bin/bash
1 创建映射80端口的交互式容器
heyongjindeMacBook-Pro:~ heyongjin$ docker run -p 80 --name web -i -t ubuntu /bin/bash
root@a76c615e9ea4:/#
2 安装Nginx
apt-get update
apt-get install -y nginx
3 安装文本编辑器vim
apt-get install -y vim
4 创建静态页面
root@a76c615e9ea4:/# mkdir -p /var/www/html
root@a76c615e9ea4:/# cd /var/www/html
root@a76c615e9ea4:/var/www/html# vim index.html
root@a76c615e9ea4:/var/www/html# cat index.html
<html>
<head>
<title>Nginx in Docker</title>
</head>
<body>
<h1>Hello,I'm website in Docker!</h1>
</body>
</html>
root@a76c615e9ea4:/var/www/html#
5 修改Nginx配置文件
root@a76c615e9ea4:/var/www/html# whereis nginx
nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx
root@a76c615e9ea4:/var/www/html# vim /etc/nginx/sites-enabled/default
6 运行Nginx
root@a76c615e9ea4:/var/www/html# nginx
7 验证网站访问
root@a76c615e9ea4:/var/www/html# apt-get install -y curl
root@a76c615e9ea4:/var/www/html# curl http://localhost
<html>
<head>
<title>Nginx in Docker</title>
</head>
<body>
<h1>Hello,I'm website in Docker!</h1>
</body>
</html>
root@a76c615e9ea4:/var/www/html#
8 在宿主机验证网站访问
ctrl + p & ctrl + q
heyongjindeMacBook-Pro:~ heyongjin$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a76c615e9ea4 ubuntu "/bin/bash" 11 hours ago Up 43 minutes 0.0.0.0:32768->80/tcp web
heyongjindeMacBook-Pro:~ heyongjin$ docker inspect web
heyongjindeMacBook-Pro:~ heyongjin$ curl http://172.17.0.2:32768
^C
heyongjindeMacBook-Pro:~ heyongjin$
(Mac没有docker0 ,宿主机无法访问docker 容器 https://docs.docker.com/docker-for-mac/networking/)