1

I have the following simple server in express, with the following docker file

axiostest.mjs

import axios from "axios"
import express from "express"


const app = express();

app.get("/", (request, response) => {
    axios.get(`http://localhost:8888/admin_issues`).then(res => {
        console.log(res.data);
        response.send(res.data)
    }).catch(err => {
        console.log("ERRRROR")
        console.log(err);
        response.send(err)
    })
});

app.listen(1112, () => {
    console.log("Listen on the port 1112...");
});

DockerFile

FROM node:16

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 1112
CMD [ "node", "axiostest.mjs" ]

If I run the server normally with node axiostest.mjs, and then I do a postman call to localhost:1112

It works just fine

But If I build the docker container

docker build . -t me/express-test

and then i run it

docker run -p 49160:1112 -d me/express-test

If i do a postman call to localhost:49160

It says

"message": "connect ECONNREFUSED 127.0.0.1:8888"

Because axios is failing to connect to 127.0.0.1:8888

How can I fix this?

mouchin777
  • 1,428
  • 1
  • 31
  • 59
  • Whats listening on port 8888 and more importantly _where_? – tkausl Aug 02 '22 at 14:58
  • @tkausl i have an ssh tunnel behind a vpn ```ssh -fN user@database.com -L 8888:domain:80``` – mouchin777 Aug 02 '22 at 15:01
  • You are trying to connect to 127.0.0.1:8888 inside of your docker container - there is nothing listening on that port. Your docker container is running in another namespace so your local tunnels are not available inside the container – Telinov Dmitri Aug 02 '22 at 15:23
  • @TelinovDmitri and how could i reach that port in the host from within the container then? – mouchin777 Aug 02 '22 at 15:24
  • 1
    [Here is described the solution](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – Telinov Dmitri Aug 02 '22 at 15:26

0 Answers0