隨機映射連接埠

  1. 使用 centos/httpd image 建立名為 rpweb 的容器,-P 隨機指定映射連接埠。
    [root@dyw219 ~]# docker run -dit --name rpweb -P centos/httpd
    63e3783aa570fd0f813f65a2fcf4e5306b2d5e1a2d02b65d38a9c3dbdd9b5745
    
  2. docker ps 查看,取得的連接埠為 32768。
    [root@dyw219 ~]# docker ps
    CONTAINER ID IMAGE        COMMAND         CREATED        STATUS        PORTS                 NAMES
    63e3783aa570 centos/httpd "/run-httpd.sh" 18 seconds ago Up 17 seconds 0.0.0.0:32768->80/tcp rpweb
    
  3. docker logs 查看容器紀錄檔,因為容器的 httpd 沒有設定網域名稱,所以出現以下警告訊息。
    [root@dyw219 ~]# docker logs rpweb
    AH00558: httpd: Could not reliably determine the server's fully qualified domain
     name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress 
    this message
    
  4. curl 試著連線 docker host 上的 32768 埠號,確實連線到容器 rpweb 的網頁,因沒製作首頁,所以出現 CentOS 的說明頁。
    [root@dyw219 ~]# curl -s http://127.0.0.1:32768 | grep 'CentOS Project'
                <p class="lead">The CentOS Project has nothing to do with 
    this website or its content,
                Unless you intended to visit CentOS.org, the CentOS Project 
    does not have anything to do with this website,
                <h2>The CentOS Project</h2>
    
  5. 測試完,關閉容器 rpweb,並移除。
    [root@dyw219 ~]# docker stop rpweb
    rpweb
    [root@dyw219 ~]# docker rm rpweb
    rpweb
    
  6. 使用 centos/httpd image 建立名為 dtest6 的容器,隨機指定映射連接埠,建立後查連接埠,試著以 curl -s 連線容器 dtest6,並將結果導向到 /root/dtest6.curl。