How to expose UDP ports in the docker-compose configuration file?

2

I have the following file docker-compose.yml and would like to expose some of the ports as UDP.

version: "3.0"
services: 
  myservice: 
    image: "my/service"
    ports: 
      - "40061:4061"
      - "40062:4062"
      - "5684:5684" <--- UDP
      - "5683:5683" <--- UDP
  mongo: 
    image: "mongo:3.4"

It seems like the default is to expose these ports as TCP, so the external service is not accessing.

How would you configure the configuration in docker-compose so that you could expose these ports as UDP?

    
asked by anonymous 06.09.2017 / 22:56

1 answer

1

To expose the port as UDP, simply add at the end of the /udp mapping. Using your compose as an example, we would have this:

version: "3.0"
services: 
  myservice: 
    image: "my/service"
    ports: 
      - "40061:4061"
      - "40062:4062"
      - "5684:5684/udp"
      - "5683:5683/udp"
  mongo: 
    image: "mongo:3.4"

When we check the status services with docker-compose ps we would see the ports as UDP, something like this:

Name                     Command                  State         Ports           
--------------------------------------------------------------------------------------------
dockerudp_mongo_1        docker-entrypoint.sh     Up            27017/tcp                 
                         mongod                                                            
dockerudp_myservice_1    /bin/sh -c whatever.sh   Up            0.0.0.0:40061->4061/tcp,  
                                                                0.0.0.0:40062->4062/tcp,  
                                                                0.0.0.0:5683->5683/udp,   
                                                                0.0.0.0:5684->5684/udp

It is important to remember that in your image, the% built with% must also have the ports exposed as UDP, that is, it should contain a statement like this:

EXPOSE 5683/udp 5684/udp
    
04.10.2017 / 04:56