Search code examples
node.jsdockerzeromq

Cannot access Zeromq from docker runned locally


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.


Solution

  • 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.