1

I am trying to access my zeromq run in docker. In my code, I use:

const socket = new ZeroMQ.Pull()
socket.connect(`tcp://${process.env.IP}:${process.env.PORT}`)
// other logic here

where my IP and PORT are set as 0.0.0.0 & 4000

here's my DockerFile

FROM node:12-alpine AS BUILD_IMAGE

# update
RUN apk update && apk add curl bash && rm -rf /var/cache/apk/*

# install node-prune (https://github.com/tj/node-prune)
RUN curl -sfL https://install.goreleaser.com/github.com/tj/node-prune.sh | bash -s -- -b /usr/local/bin

WORKDIR /usr/tile-service

COPY package.json ./

# install dependencies
RUN npm install

# Bundle app source
COPY . .

# remove development dependencies
RUN npm prune --production

# run node prune
RUN /usr/local/bin/node-prune

FROM node:12-alpine

WORKDIR /usr/tile-service

# copy from build image
COPY --from=BUILD_IMAGE /usr/tile-service .

RUN npm rebuild --verbose sharpnpm rebuild --verbose sharp

# install poppler (https://github.com/freedesktop/poppler)
RUN apk add poppler-data
RUN apk add poppler-utils

# install nodemon
RUN npm install nodemon

EXPOSE 4000
CMD ["npm", "run", "start"]

this runs okay, when I run my producer inside the docker. Now, I want to run my producer locally.

my producer just looks like this

const socket = new ZeroMQ.Push()
try {
    await socket.bind(`tcp://${process.env.IP}:${process.env.PORT}`)
    // other logic here
} catch (error) {
    console.error(error)
}

where IP and PORT are set as 192.168.1.6 & 4000. I also tried to use 0.0.0.0 and 172.17.0.2(IP of the docker container). My docker runs at 4000/tcp.

and this setup seems did not call the function in my zeromq run in my docker.

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
Rashid
  • 1,700
  • 1
  • 23
  • 56
  • Have you done a port mapping for the running container? Also is the producer program running on the same machine as Docker and outside of Docker? – Mohit Mutha Apr 15 '21 at 03:00
  • @MohitMutha I am running the docker locally. I also run the producer program locally but not inside the docker container. When I run the producer program inside the docker, the it works fine. What do you mean by port mapping? can you give an example – Rashid Apr 15 '21 at 03:03
  • When you start the docker container if you run with the -p argument then the port inside the docker container is mapped to the corresponding machine port for e.g. `docker run -it -p 4000:4000 .... ` will map the 4000 port from the container to the 4000 port on the machine. – Mohit Mutha Apr 15 '21 at 03:05
  • @MohitMutha i tried it and didnt work. what should be the value of my IP and PORT in producer? – Rashid Apr 15 '21 at 03:14

3 Answers3

1

Try the below

  • Check if you have mapped the ports while starting the container
docker run -itd -p 4000:4000 .... `

  • If the ports are mapped correctly try accessing the ZeroMq with IP as 127.0.0.1 or localhost

You havent mentioned whether you are running Docker native or using a VM. In case you are running in a VM then the IP will change

Mohit Mutha
  • 2,921
  • 14
  • 25
0

If anyone is interested, here's the solution I found. when running the server, I change the IP to host.docker.internal. Then when running the client side, I use the IP 127.0.0.1

what I know now is that the IP on the client side changes where the docker is running. since the docker is running at my localhost, so I use 127.0.0.1. I am not sure if this configuration will remain the same once deployed in the cloud.

Rashid
  • 1,700
  • 1
  • 23
  • 56
0

I am running a docker container on some docker bridge network. I was trying to connect localhost from docker; I have tried 127.0.0.1 which did not work.

py3) nitin@nitin-GP65:~/workspace/Final_Release/video_recorder$ ifconfig 
br-309baeb7257d: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.0.1  netmask 255.255.0.0  broadcast 172.18.255.255
        inet6 fe80::42:44ff:fefe:b6ad  prefixlen 64  scopeid 0x20<link>
        ether 02:42:44:fe:b6:ad  txqueuelen 0  (Ethernet)
        RX packets 1152  bytes 406738 (406.7 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1733  bytes 153306 (153.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

When I tried with 172.18.0.1 which works for me.