testdriven.io How do I fix "Name or service not known" during docker run?

user2373505

testdriven.io

docker build -f project/Dockerfile.prod -t registry.heroku.com/mighty-savannah-85236/web ./project

Successfully built 3df1e0c4eea4

Successfully tagged registry.heroku.com/mighty-savannah-85236/web:latest

docker run --name fastapi-tdd -e PORT=8765 -e DATABASE_URL=sqlite://sqlite.db -p 5003:8765 registry.heroku.com/mighty-savannah-85236/web:latest

nc: getaddrinfo for host "web-db" port 5432: Name or service not known

docker-compose file

services:

  web:
    build: ./project
    command: uvicorn app.main:app --reload --workers 1 --host 0.0.0.0 --port 8000
    volumes:
      - ./project:/usr/src/app
    ports:
      - 8004:8000
    environment:
      - ENVIRONMENT=dev
      - TESTING=0
      - DATABASE_URL=postgres://postgres:postgres@web-db:5432/web_dev        
      - DATABASE_TEST_URL=postgres://postgres:postgres@web-db:5432/web_test  
    depends_on:   
      - web-db

  web-db:
    build:
      context: ./project/db
      dockerfile: Dockerfile
    expose:
      - 5432
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
Mushroomator

It seems your container is trying to connect to web-db:5432 which given the port likely is a Postgres database server. And as web-db is not a real domain most likely what happens is, that there should be another container called web-db which probably should be a Postgres database which your container wants to connect to.

This connection will only work though if both containers - the one you are starting and the Postgres database container - are in the same user-defined Docker network as only then Docker service discovery works. You might wanna have a look at the Docker documentation for this.

But essentially you need to create a Docker network using

docker network create my-network

and then attach both containers - again, your container and the Postgres database - to that network using the --network option. Additionally your Postgres container must be called web-db so that the service discovery will work.

So the skeleton of the command to start the DB would be the following:

docker run --name web-db --network my-network -p 5432:5432 your-database-image

The command to start your application would be

docker run --name fastapi-tdd --network my-network -e PORT=8765 -e DATABASE_URL=sqlite://sqlite.db -p 5003:8765 registry.heroku.com/mighty-savannah-85236/web:latest

Might also be worth exploring Docker-compose to simplify this whole process.

Edit

Now with your docker-compose.yaml file the same rule applies. Both containers need to be in the same user-defined network bridge, which can be declared using networks: (be aware: don't put it into services:).

services:

  web:
    build: ./project
    command: uvicorn app.main:app --reload --workers 1 --host 0.0.0.0 --port 8000
    volumes:
      - ./project:/usr/src/app
    # attach this container to the network
    networks:
      - my-network
    ports:
      - 8004:8000
    environment:
      - ENVIRONMENT=dev
      - TESTING=0
      - DATABASE_URL=postgres://postgres:postgres@web-db:5432/web_dev        
      - DATABASE_TEST_URL=postgres://postgres:postgres@web-db:5432/web_test  
    depends_on:   
      - web-db

  web-db:
    # attach this container to the network
    networks:
      - my-network
    # name this container web-db
    container_name: web-db
    build:
      context: ./project/db
      dockerfile: Dockerfile
    expose:
      - 5432
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres

# declare the network ressource
networks:
  my-network

Now a connection should be possible. Be aware that you also need to configure PostgreSQL correctly to allow you to connect to it setting listen_address='*' in postgresql.conf.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how do i fix sudo: unable to resolve host raspberrypi4: Name or service not known

testdriven.io django tdd - got Error: '$' is not a valid port number when running docker run

Python error only in docker environment (Name or service not known)

How do I fix Docker getting stuck at "RUN npm run build" with Angular 15

How do I get "npm run build --prod" to work during the creation of a Docker image?

How do I fix this Error 1 when trying to run Site Inspector with Docker?

I have an error when I run it, how do I fix it?

How do I fix Python io.TextIOWrapper error message?

How do I fix the error of NumberFormatException everytime I run the program?

How do I run a CKAN docker image?

Failed to resolve 'kafka:9092': Name or service not known - docker / php-rdkafka

Docker-compose with django and postgresql could not translate host name "db" to address: Name or service not known

"could not translate host name "postgres" to address: Name or service not known" With Postgres in docker-compose

How do I run Lighthouse CLI during tests in Django?

How to run shell script during Docker run

How to run 2 services during 'docker run'?

How to run a bash script during docker run?

I'm getting 'Name or service not known' in Linux Kali when connecting with Virtualbox Bridged Adapter

How do I set environment variables during the build in docker

How do I fix my GOROOT and GOPATH variables to run go?

How do i fix this .bat file so it can run in macOS?

How do I fix the error npm run dev gives?

Can't run most services. How do I fix that?

How do I run script on startup to fix screen orientation issue?

Should I run tests during the docker build?

How do I run a sql file of inserts through docker run?

JWTRefreshTokenBundle: Name or service not known

ftp: Name or Service not known

hostname: Name or service not known