How to expose more than one Docker container port?

Asked

Viewed 3,749 times

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 needs to access port 9000 to make the mocking with Xdebug.

My question is what is the correct way to expose these doors, beyond the 80?

Would it be on Dockerfile? This option is mandatory if we want to expose the doors?

EXPOSE 80
EXPOSE 6379
EXPOSE 9000

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

-p 80:80
-p 6379:6379
-p 9000:9000
  • You are treating container as VM, and this is an error. Whoever answers for port 80, should not be in the same container that redis is in. This container should most likely be dismembered into 3 containers, orchestrated in a Docker-Compose. You at least reuse an image (redis), if you don’t reuse the other.

  • As for the redis I agree with you. We should use 2 containers - 1 for each service. But it was a decision of the company to keep REDIS in the same container. What I don’t understand is how much to the third container you mentioned. You mean I should use a container for Xdebug?

  • For not knowing Xdebug I was not so emphatic to say that there are certainly 3. Now that I saw what it is, this should be next to PHP yes.

1 answer

1


First, take a look in this that talks a little more about the differences of publishing/displaying doors.

Would it be on Dockerfile? This option is mandatory if we want to expose the doors?

Use the EXPOSE in the definition of the images, in this case, 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 in which protocol can be exposed to the host - or other Networks - safely. As stated, its use is not required.

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

When using the post/display in creating and/or running containers it will not be necessary to EXPOSE explicit. When using port publishing host in the creation of a container or execution of an existing container, if you have not specified with the EXPOSE which doors intentionally can and/or must be exposed, the Docker implicitly will do the EXPOSE - it does not alter the image, however.

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

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

In your case, however, you need certain doors to be accessible on host, which is where your IDE is running. In this case it is recommended to use both EXPOSE, both for its use as documentation and will give greater flexibility to a third-party user of its image, and it is necessary that the ports are published for access by host, then you’ll need the -p/--Publish/-P/--publish-all. Even so you may not need to publish all host, if it is not mandatory that host have access to all the mentioned doors - such as, for example, 6379 be accessed only by another container on the same network.

P.S.: in your dockerfile you can inform the doors in just one instruction, such as EXPOSE 80 6379 9000. This will create a single layer, facilitates image inspection in most cases.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.