Benyamin Jafari
Benyamin Jafari

Reputation: 34226

How to expose a ZMQ container port outside of docker/container using docker-compose?

I have a container with several ports. I want remote access to the port 9001 outside of the container.


I have searched about this issue and I found the expose and ports attributes for ports forwarding but it did not work.

How to expose docker ports to make your containers externally accessible
Reference


This is my docker-compose file:

version: '3'

services:
  nginx:
      image: nginx:latest
      container_name: nginx
      ports:
        - "8010:8010"
      volumes:
        - .:/code
        - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
      links:
        - ivms
      restart: unless-stopped

  ivms:
      build: .
      container_name: ivms
      command: bash bashes/createDB.sh
      volumes:
        - .:/code
      expose:
        - "8010"
        - "9001"
      ports:
        - "9001:9001"

Here's the code snippet inside the ivms container:

import zmq

if __name__ == '__main__':
    context = zmq.Context()
    socket = context.socket(zmq.SUB)
    socket.setsockopt(zmq.SUBSCRIBE, "")
    socket.bind("tcp://192.168.1.131:9001")  # doesn't work with server or docker IP

    while True:
        data = socket.recv_json()

I run the above docker-compose via docker-compose up -d

What should I do so that I can have access the server_IP:9001 --> 192.168.1.131:9001?


[NOTE]:


Any help would be appreciated.

Upvotes: 9

Views: 26562

Answers (2)

Benyamin Jafari
Benyamin Jafari

Reputation: 34226

The problem has been resolved with the below instruction:

In the ZMQ app (in ivms container) I had used the server's IP to bind the IP and port to establish the connection:

import zmq

if __name__ == '__main__':
    context = zmq.Context()
    socket = context.socket(zmq.SUB)
    socket.setsockopt(zmq.SUBSCRIBE, "")
    socket.bind("tcp://192.168.1.131:9001")  # doesn't work with server or docker IP

    while True:
        data = socket.recv_json()

It was working only as below:

socket.bind("tcp://192.168.1.131:9001")  # works, but can't access as remote

Now I edited this line as follows:

socket.bind("tcp://*:9001")  # Works both locally and remotely.

And this is my docker-compose.yml configuration:

version: '3'

services:
  nginx:
      image: nginx:latest
      container_name: nginx
      ports:
        - "8010:8010"
      volumes:
        - .:/code
        - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
      links:
        - ivms
      restart: unless-stopped

  ivms:
      build: .
      container_name: ivms
      command: bash bashes/createDB.sh
      volumes:
        - .:/code
      expose:
        - "8010"
      ports:
        - "9001:9001"

Upvotes: 2

g3org3
g3org3

Reputation: 136

If you want to actually map the port you should use

ivms:
  build: .
  container_name: ivms
  command: bash bashes/createDB.sh
  volumes:
    - .:/code
  ports:
    - "8010:8010"
    - "9001:9001"  # now you can access them locally

warning you are using the same port for these two services ivms and nginx

The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. You can specify whether the port listens on TCP or UDP, and the default is TCP if the protocol is not specified.

The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. -Docker Docs

Upvotes: 4

Related Questions