1

I'm trying to make a request from a container to another container within docker and I get a connection refused error.However, if I make the same request using postman, the error does not occur.

I make the request using axios and node.js

url:"http://localhost:8081/project"

async findAllProjects() {
        try {
            const url = URL_BASE_FEED + GET_PROJECTS;
            const request: IHttpRequest<void> = { url };
            const { data, status } = await this.http.get<void, IProject[]>(request);
            return { projects: data, status };
        } catch (error) {
            handleException(error);
        }
    }

error:

2023-02-20 19:16:39 URL: http://127.0.0.1:8081/project
2023-02-20 19:16:39 REQUEST { url: 'http://127.0.0.1:8081/project' }
2023-02-20 19:16:39 ERROR:  AxiosError: connect ECONNREFUSED 127.0.0.1:8081
2023-02-20 19:16:39     at Function.AxiosError.from (/authentication/node_modules/axios/lib/core/AxiosError.js:89:14)
2023-02-20 19:16:39     at RedirectableRequest.handleRequestError (/authentication/node_modules/axios/lib/adapters/http.js:569:25)
2023-02-20 19:16:39     at RedirectableRequest.emit (node:events:513:28)
2023-02-20 19:16:39     at ClientRequest.eventHandlers.<computed> (/authentication/node_modules/follow-redirects/index.js:14:24)
2023-02-20 19:16:39     at ClientRequest.emit (node:events:513:28)
2023-02-20 19:16:39     at Socket.socketErrorListener (node:_http_client:494:9)
2023-02-20 19:16:39     at Socket.emit (node:events:513:28)
2023-02-20 19:16:39     at emitErrorNT (node:internal/streams/destroy:151:8)
2023-02-20 19:16:39     at emitErrorCloseNT (node:internal/streams/destroy:116:3)
2023-02-20 19:16:39     at processTicksAndRejections (node:internal/process/task_queues:82:21) {
2023-02-20 19:16:39   port: 8081,
2023-02-20 19:16:39   address: '127.0.0.1',
2023-02-20 19:16:39   syscall: 'connect',
2023-02-20 19:16:39   code: 'ECONNREFUSED',
2023-02-20 19:16:39   errno: -111,
2023-02-20 19:16:39   config: {
2023-02-20 19:16:39     transitional: {
2023-02-20 19:16:39       silentJSONParsing: true,
2023-02-20 19:16:39       forcedJSONParsing: true,
2023-02-20 19:16:39       clarifyTimeoutError: false
2023-02-20 19:16:39     },
2023-02-20 19:16:39     adapter: [ 'xhr', 'http' ],
2023-02-20 19:16:39     transformRequest: [ [Function: transformRequest] ],
2023-02-20 19:16:39     transformResponse: [ [Function: transformResponse] ],
2023-02-20 19:16:39     timeout: 0,
2023-02-20 19:16:39     xsrfCookieName: 'XSRF-TOKEN',
2023-02-20 19:16:39     xsrfHeaderName: 'X-XSRF-TOKEN',
2023-02-20 19:16:39     maxContentLength: -1,
2023-02-20 19:16:39     maxBodyLength: -1,
2023-02-20 19:16:39     env: { FormData: [Function], Blob: [class Blob] },
2023-02-20 19:16:39     validateStatus: [Function: validateStatus],
2023-02-20 19:16:39     headers: AxiosHeaders {
2023-02-20 19:16:39       Accept: 'application/json, text/plain, */*',
2023-02-20 19:16:39       'User-Agent': 'axios/1.3.3',
2023-02-20 19:16:39       'Accept-Encoding': 'gzip, compress, deflate, br'
2023-02-20 19:16:39     },
2023-02-20 19:16:39     method: 'get',
2023-02-20 19:16:39     url: 'http://127.0.0.1:8081/project',
2023-02-20 19:16:39     data: undefined
2023-02-20 19:16:39   },
2023-02-20 19:16:39   request: <ref *1> Writable {
2023-02-20 19:16:39     _writableState: WritableState {
2023-02-20 19:16:39       objectMode: false,
2023-02-20 19:16:39       highWaterMark: 16384,
2023-02-20 19:16:39       finalCalled: false,
2023-02-20 19:16:39       needDrain: false,
2023-02-20 19:16:39       ending: false,
2023-02-20 19:16:39       ended: false,
2023-02-20 19:16:39       finished: false,
2023-02-20 19:16:39       destroyed: false,
2023-02-20 19:16:39       decodeStrings: true,
2023-02-20 19:16:39       defaultEncoding: 'utf8',
2023-02-20 19:16:39       length: 0,
2023-02-20 19:16:39       writing: false,
2023-02-20 19:16:39       corked: 0,
2023-02-20 19:16:39       sync: true,
2023-02-20 19:16:39       bufferProcessing: false,
2023-02-20 19:16:39       onwrite: [Function: bound onwrite],
2023-02-20 19:16:39       writecb: null,
2023-02-20 19:16:39       writelen: 0,
2023-02-20 19:16:39       afterWriteTickInfo: null,
2023-02-20 19:16:39       buffered: [],
2023-02-20 19:16:39       bufferedIndex: 0,
2023-02-20 19:16:39       allBuffers: true,
2023-02-20 19:16:39       allNoop: true,
2023-02-20 19:16:39       pendingcb: 0,
2023-02-20 19:16:39       constructed: true,
2023-02-20 19:16:39       prefinished: false,
2023-02-20 19:16:39       errorEmitted: false,
2023-02-20 19:16:39       emitClose: true,
2023-02-20 19:16:39       autoDestroy: true,
2023-02-20 19:16:39       errored: null,
2023-02-20 19:16:39       closed: false,
2023-02-20 19:16:39       closeEmitted: false,
2023-02-20 19:16:39       [Symbol(kOnFinished)]: []
2023-02-20 19:16:39     },
2023-02-20 19:16:39     _events: [Object: null prototype] {
2023-02-20 19:16:39       response: [Function: handleResponse],
2023-02-20 19:16:39       error: [Function: handleRequestError],
2023-02-20 19:16:39       socket: [Function: handleRequestSocket]
2023-02-20 19:16:39     },
2023-02-20 19:16:39     _eventsCount: 3,
2023-02-20 19:16:39     _maxListeners: undefined,
2023-02-20 19:16:39     _options: {
2023-02-20 19:16:39       maxRedirects: 21,
2023-02-20 19:16:39       maxBodyLength: Infinity,
2023-02-20 19:16:39       protocol: 'http:',
2023-02-20 19:16:39       path: '/project',
2023-02-20 19:16:39       method: 'GET',
2023-02-20 19:16:39       headers: [Object: null prototype],
2023-02-20 19:16:39       agents: [Object],
2023-02-20 19:16:39       auth: undefined,
2023-02-20 19:16:39       beforeRedirect: [Function: dispatchBeforeRedirect],
2023-02-20 19:16:39       beforeRedirects: [Object],
2023-02-20 19:16:39       hostname: '127.0.0.1',
2023-02-20 19:16:39       port: '8081',
2023-02-20 19:16:39       agent: undefined,
2023-02-20 19:16:39       nativeProtocols: [Object],
2023-02-20 19:16:39       pathname: '/project'
2023-02-20 19:16:39     },
2023-02-20 19:16:39     _ended: true,
2023-02-20 19:16:39     _ending: true,
2023-02-20 19:16:39     _redirectCount: 0,
2023-02-20 19:16:39     _redirects: [],
2023-02-20 19:16:39     _requestBodyLength: 0,
2023-02-20 19:16:39     _requestBodyBuffers: [],
2023-02-20 19:16:39     _onNativeResponse: [Function (anonymous)],
2023-02-20 19:16:39     _currentRequest: ClientRequest {
2023-02-20 19:16:39       _events: [Object: null prototype],
2023-02-20 19:16:39       _eventsCount: 7,
2023-02-20 19:16:39       _maxListeners: undefined,
2023-02-20 19:16:39       outputData: [],
2023-02-20 19:16:39       outputSize: 0,
2023-02-20 19:16:39       writable: true,
2023-02-20 19:16:39       destroyed: false,
2023-02-20 19:16:39       _last: true,
2023-02-20 19:16:39       chunkedEncoding: false,
2023-02-20 19:16:39       shouldKeepAlive: false,
2023-02-20 19:16:39       maxRequestsOnConnectionReached: false,
2023-02-20 19:16:39       _defaultKeepAlive: true,
2023-02-20 19:16:39       useChunkedEncodingByDefault: false,
2023-02-20 19:16:39       sendDate: false,
2023-02-20 19:16:39       _removedConnection: false,
2023-02-20 19:16:39       _removedContLen: false,
2023-02-20 19:16:39       _removedTE: false,
2023-02-20 19:16:39       strictContentLength: false,
2023-02-20 19:16:39       _contentLength: 0,
2023-02-20 19:16:39       _hasBody: true,
2023-02-20 19:16:39       _trailer: '',
2023-02-20 19:16:39       finished: true,
2023-02-20 19:16:39       _headerSent: true,
2023-02-20 19:16:39       _closed: false,
2023-02-20 19:16:39       socket: [Socket],
2023-02-20 19:16:39       _header: 'GET /project HTTP/1.1\r\n' +
2023-02-20 19:16:39         'Accept: application/json, text/plain, */*\r\n' +
2023-02-20 19:16:39         'User-Agent: axios/1.3.3\r\n' +
2023-02-20 19:16:39         'Accept-Encoding: gzip, compress, deflate, br\r\n' +
2023-02-20 19:16:39         'Host: 127.0.0.1:8081\r\n' +
2023-02-20 19:16:39         'Connection: close\r\n' +
2023-02-20 19:16:39         '\r\n',
2023-02-20 19:16:39       _keepAliveTimeout: 0,
2023-02-20 19:16:39       _onPendingData: [Function: nop],
2023-02-20 19:16:39       agent: [Agent],
2023-02-20 19:16:39       socketPath: undefined,
2023-02-20 19:16:39       method: 'GET',
2023-02-20 19:16:39       maxHeaderSize: undefined,
2023-02-20 19:16:39       insecureHTTPParser: undefined,
2023-02-20 19:16:39       path: '/project',
2023-02-20 19:16:39       _ended: false,
2023-02-20 19:16:39       res: null,
2023-02-20 19:16:39       aborted: false,
2023-02-20 19:16:39       timeoutCb: null,
2023-02-20 19:16:39       upgradeOrConnect: false,
2023-02-20 19:16:39       parser: null,
2023-02-20 19:16:39       maxHeadersCount: null,
2023-02-20 19:16:39       reusedSocket: false,
2023-02-20 19:16:39       host: '127.0.0.1',
2023-02-20 19:16:39       protocol: 'http:',
2023-02-20 19:16:39       _redirectable: [Circular *1],
2023-02-20 19:16:39       [Symbol(kCapture)]: false,
2023-02-20 19:16:39       [Symbol(kBytesWritten)]: 0,
2023-02-20 19:16:39       [Symbol(kEndCalled)]: true,
2023-02-20 19:16:39       [Symbol(kNeedDrain)]: false,
2023-02-20 19:16:39       [Symbol(corked)]: 0,
2023-02-20 19:16:39       [Symbol(kOutHeaders)]: [Object: null prototype],
2023-02-20 19:16:39       [Symbol(kUniqueHeaders)]: null
2023-02-20 19:16:39     },
2023-02-20 19:16:39     _currentUrl: 'http://127.0.0.1:8081/project',
2023-02-20 19:16:39     [Symbol(kCapture)]: false
2023-02-20 19:16:39   },
2023-02-20 19:16:39   cause: Error: connect ECONNREFUSED 127.0.0.1:8081
2023-02-20 19:16:39       at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1300:16) {
2023-02-20 19:16:39     errno: -111,
2023-02-20 19:16:39     code: 'ECONNREFUSED',
2023-02-20 19:16:39     syscall: 'connect',
2023-02-20 19:16:39     address: '127.0.0.1',
2023-02-20 19:16:39     port: 8081
2023-02-20 19:16:39   }
2023-02-20 19:16:39 }

cors() middleware is enabled in both containers. The error happens only within docker containers.

  • Does this answer your question? [Connect to another container using Docker compose](https://stackoverflow.com/questions/35749385/connect-to-another-container-using-docker-compose) – Phil Feb 20 '23 at 22:41
  • TL;DR `localhost` is local to the container. If you're trying to connect to another container, you need to use its Docker network address – Phil Feb 20 '23 at 22:41
  • ahh you're right, I must pass the url as an environment variable – Luciano Pulido Feb 20 '23 at 23:16

0 Answers0