欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Docker容器-网络访问(端口映射)

程序员文章站 2022-06-04 19:48:19
...
随机映射
  • docker run -P
    [[email protected] scripts]# docker run  -d -P --name mynginx1 nginx  
    d27f8c6cbba27921ba09614541fd04110dd18d5a97cddc033e0f11a76c951633
    [[email protected] scripts]# docker ps -l
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
    d27f8c6cbba2        nginx               "nginx -g 'daemon ..."   10 seconds ago      Up 10 seconds       0.0.0.0:32768->80/tcp   mynginx1
    
    每次重新启动,会随机映射不同的端口
    [[email protected] scripts]# docker start d27f8c6cbba2
    d27f8c6cbba2
    [[email protected] scripts]# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
    d27f8c6cbba2        nginx               "nginx -g 'daemon ..."   5 minutes ago       Up 2 minutes        0.0.0.0:32770->80/tcp   mynginx1
    [[email protected] scripts]# docker stop d27f8c6cbba2
    d27f8c6cbba2
    #停止重新启动后,随机映射了不同的端口
    [[email protected] scripts]# docker start d27f8c6cbba2
    d27f8c6cbba2
    [[email protected] scripts]# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
    d27f8c6cbba2        nginx               "nginx -g 'daemon ..."   5 minutes ago       Up 1 second         0.0.0.0:32771->80/tcp   mynginx1
    
指定映射
  • -p hostPort:containerPort
    物理主机的所有IP地址的端口都映射到docker的端口
    [[email protected] scripts]# docker run -d -p 10002:80 --name mynginx2 nginx
    5028901c7fae3c7e54409942635aad5b40d4533e5dd43e24bdf774554bf85517
    [[email protected] scripts]# docker ps -l
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
    5028901c7fae        nginx               "nginx -g 'daemon ..."   6 seconds ago       Up 5 seconds        0.0.0.0:10002->80/tcp   mynginx2
    
    指定多个端口映射
    [[email protected] scripts]# docker run -d -p 10003:80 -p 14443:443 --name mynginx3 nginx
    a5ecb58d08fcb1308e5b55c497b68a6ecfd683a86cc5e63ea12e402dbcbc1544
    [[email protected] scripts]# docker ps -l
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                           NAMES
    a5ecb58d08fc        nginx               "nginx -g 'daemon ..."   4 seconds ago       Up 3 seconds        0.0.0.0:10003->80/tcp, 0.0.0.0:14443->443/tcp   mynginx3
    
  • -p ip:hostPort:containerPort
    指定物理主机的固定IP地址的端口映射到docker的端口
    [[email protected] scripts]# docker run -d -p 127.0.0.1:91:80 --name test1  nginx
    87cc9379574b6b77f58dc0f0d31fc4786e5c96114ae5db6df12c6d38cef4b919
    [[email protected] scripts]# docker ps -l
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
    87cc9379574b        nginx               "nginx -g 'daemon ..."   7 seconds ago       Up 6 seconds        127.0.0.1:91->80/tcp   test1
    
  • -p ip::containerPort
    指定物理主机的固定IP地址映射到docker的端口,物理主机端口随机
    [[email protected] scripts]# docker run -d -p 127.0.0.1::92 --name test2  nginx 
    7ff82a963e2acb52b1cfed7da7e78108bf05dd6543cd383d1879e199f536af26
    [[email protected] scripts]# docker ps -l
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                             NAMES
    7ff82a963e2a        nginx               "nginx -g 'daemon ..."   6 seconds ago       Up 5 seconds        80/tcp, 127.0.0.1:32769->92/tcp   test2