How to expose more than one port in a Docker container?

1

I have a docker container that has redis and xdebug installed.

A Redis client installed on the host needs to access this service through port 6379 and on the host intellij must access port 9000 to debug with xdebug.

My question is what is the correct way to expose these ports besides 80?

Would it be in Dockerfile? Is this option mandatory if we want to expose the ports?

EXPOSE 80
EXPOSE 6379
EXPOSE 9000

Or when loading the image? If this option is used would the previous option be needed?

-p 80:80
-p 6379:6379
-p 9000:9000
    
asked by anonymous 16.11.2017 / 18:12

1 answer

1

First, take a look at this that talks a little bit more about the differences of posting / exposing ports.

  

Would it be in Dockerfile? Is this option mandatory if we want to expose the ports?

Using EXPOSE in the definition of images, in this case, it may not be necessary, but it is always good practice, since it works as a kind of documentation. When using EXPOSE you tell the image user which ports and which protocol can be exposed to the host - or in other networks - securely. As stated, it is not required to use it.

  

Or when loading the image? If this option is used would the previous option be needed?

When using publication / exposure in the creation and / or execution of containers, you will not need% explicit%. When using port publishing to the host in container creation or running an existing container, if you have not explicitly specified with EXPOSE which ports intentionally can and should be exposed, docker implicitly will do EXPOSE - it does not change the image, however.

  

My question is: what is the correct way to expose these ports beyond 80?

In short, there is no "right" way, you can combine the ways or use them separately, it will depend on how the containers will run - as for networks , especially - how structured your containers, etc.

In your case, however, you need certain ports to be accessible in the host , which is where your IDE is running. In this case, it is recommended to use both EXPOSE , both for its use and documentation, as it will give greater flexibility to a third-party user of your image, in addition to having to be published for access by the host , then you will need EXPOSE - publish -p/ -P / / . Even though you may not need to publish all to host , if it is not mandatory for host to have access to all quoted ports - / em> to be accessed only by another container on the same network.

Q. In your dockerfile you can report ports in only a single statement, such as --publish-all . This will create a single layer , makes it easier to inspect images in most cases.

    
17.11.2017 / 11:07