title: Error: getaddrinfo EAI_AGAIN database at GetAddrInfoReqWrap.onlookup [as oncomplete]
body: <p>I'm creating an api using docker, postgresql, and nodejs (typescript). I've had this error ever since creating an admin user and nothing seems to be able to fix it:</p>
  <p>Error in docker terminal:</p>
  <pre><code>Error: getaddrinfo EAI_AGAIN database
     at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:72:26)
  [ERROR] 12:33:32 Error: getaddrinfo EAI_AGAIN database
  </code></pre>
  <p>Error in Insomnia:</p>
  <pre><code>{
      &quot;status&quot;: &quot;error&quot;,
      &quot;message&quot;: &quot;Internal server error - Cannot inject the dependency \&quot;categoriesRepository\&quot; at position #0 of \&quot;ListCategoriesUseCase\&quot; constructor. Reason:\n    No repository for \&quot;Category\&quot; was found. Looks like this entity is not registered in current \&quot;default\&quot; connection?&quot;
  }
  </code></pre>
  <p>I'm following an online course and this same code seems to work for everyone else since there are no reports of this error in the course's forum.</p>
  <p>From what I've gathered it seems to be some type of problem when connecting to my database, which is a docker container. Here is my docker-compose.yml file:</p>
  <pre><code>    version: &quot;3.9&quot;
  
  services:
    database_ignite:
      image: postgres
      container_name: database_ignite
      restart: always
      ports: 
        - 5432:5432
      environment:
        - POSTGRES_USER=something
        - POSTGRES_PASSWORD=something
        - POSTGRES_DB=rentx
      volumes:
        - pgdata:/data/postgres
  
    app:
      build: .
      container_name: rentx
      restart: always
      ports: 
        - 3333:3333
        - 9229:9229
      volumes: 
        - .:/usr/app
      links: 
        - database_ignite
      depends_on:
        - database_ignite
  
  volumes:
    pgdata:
      driver: local
  </code></pre>
  <p>My server.ts file:</p>
  <pre><code>import &quot;reflect-metadata&quot;;
  
  import express, { Request, Response, NextFunction } from &quot;express&quot;;
  import &quot;express-async-errors&quot;;
  import swaggerUi from &quot;swagger-ui-express&quot;;
  
  import { AppError } from &quot;@shared/errors/AppError&quot;;
  import createConnection from &quot;@shared/infra/typeorm&quot;;
  
  import swaggerFile from &quot;../../../swagger.json&quot;;
  import { router } from &quot;./routes&quot;;
  
  import &quot;@shared/container&quot;;
  
  createConnection();
  const app = express();
  
  app.use(express.json());
  
  app.use(&quot;/api-docs&quot;, swaggerUi.serve, swaggerUi.setup(swaggerFile));
  
  app.use(router);
  
  app.use(
      (err: Error, request: Request, response: Response, next: NextFunction) =&gt; {
          if (err instanceof AppError) {
              return response.status(err.statusCode).json({
                  message: err.message,
              });
          }
  
          return response.status(500).json({
              status: &quot;error&quot;,
              message: `Internal server error - ${err.message}`,
          });
      }
  );
  
  app.listen(3333, () =&gt; console.log(&quot;Server running&quot;));
  </code></pre>
  <p>And this is my index.ts file, inside src&gt;modules&gt;shared&gt;infra&gt;http&gt;server.ts:</p>
  <pre><code>import { Connection, createConnection, getConnectionOptions } from &quot;typeorm&quot;;
  
  export default async (host = &quot;database&quot;): Promise&lt;Connection&gt; =&gt; {
      const defaultOptions = await getConnectionOptions();
  
      return createConnection(
          Object.assign(defaultOptions, {
              host,
          })
      );
  };
  </code></pre>
  <p>I've tried restarting my containers, remaking them, accessing my postgres container and checking the tables, I've switched every &quot;database&quot; to &quot;localhost&quot; but it's the same every time: the containers run, but the error persists. I've checked the course's repo and my code matches. I've flushed my DNS and that also did nothing.</p>
  <p>Here's the admin.ts file that &quot;started it all&quot;:</p>
  <pre><code>import { hash } from &quot;bcryptjs&quot;;
  import { v4 as uuidv4 } from &quot;uuid&quot;;
  
  import createConnection from &quot;../index&quot;;
  
  async function create() {
      const connection = await createConnection(&quot;localhost&quot;);
  
      const id = uuidv4();
      const password = await hash(&quot;admin&quot;, 6);
  
      await connection.query(`
      INSERT INTO Users (id, name, email, password, &quot;isAdmin&quot;, driver_license, created_at)
      VALUES (
        '${id}',
        'admin',
        'admin@rentx.com.br',
        '${password}',
        true,
        '0123456789',
        NOW()
      )
    `);
  
      await connection.close;
  }
  
  create().then(() =&gt; console.log(&quot;Administrative user created&quot;));
  </code></pre>
  <p>I would love to know what is causing this error.</p>