访问容器特定端口
run -P (暴露容器所有端口)-p(暴露特定端口)
container port
docker run -p 80 -i -t ubuntu /bin/bash
hostport:containerport
docker run -p 8080:80 -i -t ubuntu /bin/bash
ip::contailerport
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部署流程
1.创建映射80端口的交互式容器
2. 安装nginx
3. 安装vim
4. 创建静态页面
5. 修改nginx配置文件
6. 运行nginx
7. 验证网站访问
1.创建映射80端口的交互式容器
docker run -p 80 --name web -i -t centos63-bash /bin/bash
2.安装nginx
2.1.创建yum源
echo "[centos6]
name=centos6
baseurl=http://mirrors.aliyun.com/centos/6/os/x86_64/
gpgcheck=0
enabled=1" >> iso.repo
echo "[epel6]
name=epel6
baseurl=http://mirrors.aliyun.com/epel/6Server/x86_64/
gpgcheck=0
enabled=1" >> iso.repo
yum install nginx -y
3.安装vim
3.1 yum install -y vim
4.创建静态页面
mkdir -p /var/www/html
vim /var/www/html/index.html
<html>
<head>
<title>nginx in docker</title>
<head>
<body>
<h1>Hello,i'm website in docker!</h1>
</body>
</html>
5.修改nginx的配置文件
/etc/nginx/conf.d/default.conf
location / {
root /var/www/html;
index index.html index.htm;
}
6.运行nginx
nginx
7.网页验证
root@eddy:~# docker port web
80/tcp -> 0.0.0.0:32769
容器的80端口映射为宿主机的32769端口
curl
可通过inspect查看详细信息root@eddy:~# docker inspect web
"IPAddress": "172.17.0.2"
这就是容器的ip地址
也可以通过这个地址访问
curl http://172.17.0.2
curl http://容器ip
PS:
启动时直接使用命令启动
bash-4.1# nginx
bash-4.1# ps -ef|grep nginx
root 107 1 0 22:39 ? 00:00:00 nginx: master process nginx
nginx 108 107 0 22:39 ? 00:00:00 nginx: worker process
root 110 1 0 22:39 ? 00:00:00 grep nginx
查看端口映射情况
root@eddy:~# docker port web
80/tcp -> 0.0.0.0:32769
容器的80端口映射为宿主机的32769端口
可在宿主机上使用root@eddy:~# curl http://127.0.0.1:32769
<html>
<head>
<title>nginx in docker</title>
<head>
<body>
<h1>Hello,i'm website in docker!</h1>
</body>
</html>
可通过inspect查看详细信息root@eddy:~# docker inspect web
"IPAddress": "172.17.0.2"
这就是容器的ip地址
也可以通过这个地址访问
curl http://172.17.0.2
查看容器内运行的进程情况root@eddy:~# docker top web
UID PID PPID C STIME TTY TIME CMD
root 2208 1346 0 11:17 pts/13 00:00:00 /bin/bash
root 2474 2208 0 11:39 ? 00:00:00 nginx: master process nginx
499 2475 2474 0 11:39 ? 00:00:00 nginx: worker process
停掉容器重启启动后ip和端口会自动变化,并且nginx不会自动启动
root@eddy:~# docker start web
root@eddy:~# docker exec web nginx
root@eddy:~# docker top web
UID PID PPID C STIME TTY TIME CMD
root 2570 1346 0 11:44 pts/13 00:00:00 /bin/bash
root 2594 2570 0 11:44 ? 00:00:00 nginx: master process nginx
499 2595 2594 0 11:44 ? 00:00:00 nginx: worker process
root@eddy:~# docker port web
80/tcp -> 0.0.0.0:32770
可以看到宿主机的端口已经发生变化了
转载于:https://my.oschina.net/eddylinux/blog/608566