2

I'm starting using docker. I'm creating a COAP application with a client and a server. In this application, a client sends a simple request and the server has to response to it. I built my image that runs my code in node.js. Then I used a docker-compose.yml file that I will show you in the code section. However, it seems like the server doesn't respond to the client request.

I tried to see if the IP address were well assigned, and everything seems ok. I also tried to use Wireshark on docker0 and I can see the coap packets going from the client to the server, but I don't have an answer. Here is the docker-compose.yml

services:
 Server_Coap:
  build: './Server_Coap'
  image: 'user/mynode:latest'
  container_name: server
  ports: 
  - "8081:5683"
  networks:
   rete:
    ipv4_address: 172.19.0.3
networks:
 rete:
   ipam:
    driver: default
    config:
     - subnet: 172.19.0.0/24

Here is the client code:

const coap = require('coap'),
req = coap.request('coap://172.19.0.3')
console.log("Client Request...")
req.on('response' , function(res){
    res.pipe(process.stdout)
})

req.end()

Here is the server code:

var coap = require('coap')
,server = coap.createServer()


server.on('request' , function(req, res){
    console.log("New request")
    res.end('Hello ' + req.url + '\n')
})

// the default CoAP port is 5683
server.listen(function() {
    console.log("Server started")
  })

Here is my Dockerfile for the server:

FROM node:latest

WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install 
COPY . .
EXPOSE 5683
CMD ["node" , "server.js"]

Here is my Dockerfile for the client:

FROM node:latest 

WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 5683
CMD ["node" , "client.js"]

I would like to have an answer from the server. This is the kind of error that I get: │ Error: No reply in 247s │ at Timeout._onTimeout (/usr/src/app/node_modules/coap/lib/retry_send.js:74:16) │ at listOnTimeout (internal/timers.js:531:17) │ at processTimers (internal/timers.js:475:7) │ Emitted 'error' event on OutgoingMessage instance at: │ at RetrySend.emit (events.js:209:13) │ at Timeout._onTimeout (/usr/src/app/node_modules/coap/lib/retry_send.js:77:12) │ at listOnTimeout (internal/timers.js:531:17) │ at processTimers (internal/timers.js:475:7) { │ retransmitTimeout: 247

Andrea Fresa
  • 351
  • 2
  • 18

3 Answers3

2

You have to add your client docker to the network. IE:

services:
 Server_Coap:
  build: './Server_Coap'
  image: 'user/mynode:latest'
  container_name: server
  ports: 
  - "8081:5683"
  networks:
   rete:
    ipv4_address: 172.19.0.3
 Client_Coap:
  build: './Client_Coap'
  image: 'user/mynode:latest'
  container_name: client
  networks:
   rete
networks:
 rete:
   ipam:
    driver: default
    config:
     - subnet: 172.19.0.0/24
Marcos Luis Delgado
  • 1,289
  • 7
  • 11
0

Remove the unnecessary network setting from the docker-compose.

services:
 Server_Coap:
  build: './Server_Coap'
  image: 'user/mynode:latest'
  container_name: server
  ports: 
  - "8081:5683"

And modify this to localhost, I assume they are running in the same container. If you want to access from out side then you need to mention publish port 8081.

const coap = require('coap'),
req = coap.request('coap://localhost')
console.log("Client Request...")
req.on('response' , function(res){
    res.pipe(process.stdout)
})

req.end()
  • If the coap client in other containers, better to place them in single compose file and will both able to talk on same network.

  • If the coap is running on the host, outside of container then you can use localhost:8081 on client-side to connect.

  • if the coap is running in different container, use the host ip in client side. host_ip:8081 to connect from the client.

To connect from a different network inside the only way to connect from host ip with publishing port, otherwise, it will not accessible to the client.

docker run --add-host=coab-server:YOUR_HOST_IP --rm -it coad-client

After this modify client.

req = coap.request('coap://coab-server')

Also would suggest to expose same port "5683:5683", if you do not want to use network, as I am not able see to port option or you can try.

req = coap.request('coap://coab-server')

or with current port

req = coap.request('coap://coab-server:8081')
Adiii
  • 54,482
  • 7
  • 145
  • 148
  • 1
    Client and server are 2 different docker container. I create a network because I want a static ip for my server. – Andrea Fresa Sep 12 '19 at 10:16
  • updated. `Add entries to container hosts file (--add-host)` https://docs.docker.com/engine/reference/commandline/run/ – Adiii Sep 12 '19 at 10:33
0

Usually CoAP over UDP is used, so the ports must be classified as udp

 ports: 
  - "8081:5683/udp"
Achim Kraus
  • 729
  • 1
  • 7
  • 11