zoukankan      html  css  js  c++  java
  • Docker学习笔记-(2)端口映射

    二、Docker端口映射

    容器端口只能本地访问外,并且 ip 在容器每次启动的时候都会改变。

    Docker 解决了容器的这两个问题,并且给容器内部服务的访问提供了一个简单而可靠的方法。Docker 通过端口绑定主机系统的接口,允许非本地客户端访问容器内部运行的服务。为了简便的使得容器间通信,Docker 提供了这种连接机制。

    1. 自动映射端口

    1 $ sudo docker run -t -P --expose 22 --name server  ubuntu:14.04
    2 #-P使用时需要指定--expose选项,指定需要对外提供服务的端口
    3 #使用docker run -P自动绑定所有对外提供服务的容器端口,映射的端口将会从没有使用的端口池中 (49000..49900) 自动选择
    4 #你可以通过docker ps、docker inspect <container_id>或者docker port <container_id> <port>确定具体的绑定信息。

    2. 绑定端口到指定接口

     1 $ sudo docker run -p [([<host_interface>:[host_port]])|(<host_port>):]<container_port>[/udp] <image> <cmd>
     2 #默认不指定绑定 ip 则监听所有网络接口。
     3 #绑定 TCP 端口
     4 # Bind TCP port 8080 of the container to TCP port 80 on 127.0.0.1 of the host machine. 
     5 $ sudo docker run -p 127.0.0.1:80:8080 <image> <cmd> 
     6 # Bind TCP port 8080 of the container to a dynamically allocated TCP port on 127.0.0.1 of the host machine. 
     7 $ sudo docker run -p 127.0.0.1::8080 <image> <cmd> 
     8 # Bind TCP port 8080 of the container to TCP port 80 on all available interfaces of the host machine. 
     9 $ sudo docker run -p 80:8080 <image> <cmd> 
    10 # Bind TCP port 8080 of the container to a dynamically allocated TCP port on all available interfaces 
    11 $ sudo docker run -p 8080 <image> <cmd>
    12 #绑定 UDP 端口
    13 # Bind UDP port 5353 of the container to UDP port 53 on 127.0.0.1 of the host machine. 
    14 $ sudo docker run -p 127.0.0.1:53:5353/udp <image> <cmd>
  • 相关阅读:
    sql总结
    2018年6月10日笔记
    Docker入门之zabbix-agent篇
    2018年6月7日笔记
    2018年6月5日笔记
    Docker入门之container篇
    Docker入门之image篇
    Docker 入门
    2018年5月31日笔记
    2018年5月29日笔记
  • 原文地址:https://www.cnblogs.com/litaiqing/p/9334938.html
Copyright © 2011-2022 走看看