How can I expose more than 1 port with Docker?

前端 未结 4 1390
逝去的感伤
逝去的感伤 2020-11-28 17:04

So I have 3 ports that should be exposed to the machine\'s interface. Is it possible to do this with a Docker container?

相关标签:
4条回答
  • 2020-11-28 17:56

    Step1

    In your Dockerfile, you can use the verb EXPOSE to expose multiple ports.
    e.g.

    EXPOSE 3000 80 443 22
    

    Step2

    You then would like to build an new image based on above Dockerfile.
    e.g.

    docker build -t foo:tag .
    

    Step3

    Then you can use the -p to map host port with the container port, as defined in above EXPOSE of Dockerfile.
    e.g.

    docker run -p 3001:3000 -p 23:22
    

    In case you would like to expose a range of continuous ports, you can run docker like this:

    docker run -it -p 7100-7120:7100-7120/tcp 
    
    0 讨论(0)
  • 2020-11-28 17:58

    To expose just one port, this is what you need to do:

    docker run -p <host_port>:<container_port>
    

    To expose multiple ports, simply provide multiple -p arguments:

    docker run -p <host_port1>:<container_port1> -p <host_port2>:<container_port2>
    
    0 讨论(0)
  • 2020-11-28 17:58

    if you use docker-compose.ymlfile:

    services:
        varnish:
            ports:
                - 80
                - 6081
    

    You can also specify the host/network port as HOST/NETWORK_PORT:CONTAINER_PORT

    varnish:
        ports:
            - 81:80
            - 6081:6081
    
    0 讨论(0)
  • 2020-11-28 18:04

    If you are creating a container from an image and like to expose multiple ports (not publish) you can use the following command:

    docker create --name `container name` --expose 7000 --expose 7001 `image name`
    

    Now, when you start this container using the docker start command, the configured ports above will be exposed.

    0 讨论(0)
提交回复
热议问题