This question already has answers here:
What is linux equivalent of "host.docker.internal" [duplicate]
(11 answers)
Closed 8 months ago.
I was trying to run sudo docker-compose up to get my app running and connect to a localhost mongodb but I ran into this error
time="2021-12-28T08:31:54Z" level=fatal msg="server selection error: context deadline exceeded, current topology: { Type: Unknown, Servers: [{ Addr: host.docker.internal:27017, Type: Unknown, Last error: connection() error occured during connection handshake: dial tcp: lookup host.docker.internal: no such host }, ] }"
Initially, I tried to replace localhost with host.docker.internal instead to have the mongo uri connect with the docker network but it doesn't seem to be able find it. I'm on the latest version of docker too, so it's not a question of unsupported docker version.
docker-compose.yaml
version: '3.3'
services:
app:
build:
context: .
dockerfile: dockerfile
ports:
- "8080:20717"
restart: unless-stopped
env_file: .env
networks:
- ext
- int
networks:
ext:
int:
internal: true
I previously had an extra_hosts part but replaced that with the networks portion.
extra_hosts:
- "host.docker.internal:127.0.0.1"
My .env file contains the URI and some other necessary variables for the app
DB_URI=mongodb://host.docker.internal:27017
CITY_DB=nht_cities
COL_USER=user
COL_CITY=city
USER_AUTH_TOKEN=50dbafa...
My app itself runs on port 8080
fmt.Println("Server running at port 8080")
log.Fatal(http.ListenAndServe(":8080", r)) // r being the router
Add this to "app" service in docker compose
extra_hosts:
- "host.docker.internal:host-gateway"
Then your app will be able to connect to mongodb running on host machine.
Related
I'm running Postgres image in Docker on an M1 Mac with mapped ports "5432:5432". My app can connect to the DB from the host machine by calling localhost:5432. I'm now trying to run the app within Docker and I'm puzzled by the behavior I see.
This command works:
docker run --name api --add-host host.docker.internal:host-gateway -e DB_HOST=host.docker.internal -p 8000:8000
But when I try to replicate the same by putting the api within the docker-compose like this, it doesn't work:
services:
postgres:
image: postgres:14.2
ports:
- "5432:5432"
networks:
- my-network
api:
image: api
environment:
DB_HOST: host.docker.internal
extra_hosts:
- "host.docker.internal:host-gateway"
Connecting to the DB fails:
failed to connect to host=host.docker.internal user=postgres database=postgres: failed to receive message (unexpected EOF)
I've also tried to put the api container on the same my-network network as postgres, and changing the DB host to be the DB container:
api:
image: api
environment:
DB_HOST: postgres
networks:
- my-network
but that gives me a different error:
failed to connect to host=postgres user=postgres database=postgres: dial error (dial tcp 192.168.192.2:5432: connect: connection refused)
The DB is listening at IPv4 address "0.0.0.0", port 5432 and IPv6 address "::", port 5432. Why would the docker run command work but the other two not work?
As David figured out the issue in the comments, I would like to suggest wait-for-it for such an issue, Instant of waiting a bit then start manually again.
wait-for-it.sh usually located at entrypoint.sh like this
#!/bin/sh
# Wait fot the cassandra db to be ready
./wait-for.sh cassandra:9042 --timeout=0
--timeout=0 , will keep on waiting till cassandra is up and running.
And can be used directly in docker-compose.yaml like the following example :
version: "2"
services:
web:
build: .
ports:
- "80:8000"
depends_on:
- "cassandra"
command: ["./wait-for-it.sh", "cassandra:9042"]
db:
image: cassandra
For more information, You can check Control startup and shutdown order in Compose
Wait-for-it github
I have below mongodb configuration in docker-compose.yml file -
version: '3.7'
networks:
app-tier:
driver: bridge
mongodb:
image: 'bitnami/mongodb:latest'
container_name: "mongodb"
environment:
MONGODB_INITIAL_PRIMARY_HOST: mongodb
MONGODB_ADVERTISED_HOSTNAME: mongodb
MONGODB_REPLICA_SET_MODE: primary
MONGODB_INITDB_DATABASE: testdb
MONGODB_REPLICA_SET_NAME: rs0
ALLOW_EMPTY_PASSWORD: 'yes'
ports:
- "27017:27017"
volumes:
- ./scripts/mongorestore.sh:/docker-entrypoint-initdb.d/mongorestore.sh
- ./data/mongodb:/data/mongodb
networks:
- app-tier
infrastructure:
build:
context: .
dockerfile: Dockerfile
target: base
container_name: infra
environment:
- SPRING_PROFILES_ACTIVE=dev
- KAFKA_BROKERS=kafka:9092
- REDIS_ENDPOINT=redis
- APP_NAME=infrastructure
volumes:
- ~/.m2:/root/.m2
depends_on:
- "kafka"
- "redis"
- "mongodb"
networks:
- app-tier
Whenever I run docker-compose my app infrastructure giving below error -
error connecting to host: could not connect to server: server selection error: server selection timeout, current topology: { Type: Single, Servers: [{ Addr: localhost:27017, Type: Unknown, Last error: connection() error occured during connection handshake: dial tcp 127.0.0.1:27017: connect: connection refused }, ] }
Inside application I am not even trying to connect mongodb, I am just trying to set up my application first using docker-compose
Am I missing anything here?
Something in infrastructure image is trying to connect to mongodb. localhost is likely a default host, if you didn't set it explicitly. You need to find out who is that and set host name to mongodb
I created a Flask app and after that, I created a DOCKERFILE and image from it as below:
FROM public.ecr.aws/w9y1k8q6/python3.7-apline:3.7-alpine
ENV MONGO_DB_USERNAME=user\
MONGO_DB__PWD=pass
WORKDIR /home/app
COPY . /home/app
RUN pip3 install -r requirements.txt
EXPOSE 5000
CMD ["python3", "app.py"]
and requirements are as below:
Flask==2.0.2
pymongo==4.0.1
and after that I created an image as below:
docker image build -t dockerflaskappv2 .
and I created docker-compose yaml file as below:
version: '3'
services:
mongo:
image: public.ecr.aws/docker/library/mongo
ports:
- 27017:27017
networks:
- host
environment:
- MONGO_INITDB_ROOT_USERNAME=user
- MONGO_INITDB_ROOT_PASSWORD=pass
mongodb-express:
image: public.ecr.aws/docker/library/mongo-express
ports:
- 8081:8081
networks:
- host
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=user
- ME_CONFIG_MONGODB_ADMINPASSWORD=pass
- ME_CONFIG_MONGODB_SERVER=mongo
dockerflaskappv2:
image: dockerflaskappv2
ports:
- 5000:5000
networks:
- host
environment:
- MONGO_INITDB_ROOT_USERNAME=user
- MONGO_INITDB_ROOT_PASSWORD=pass
networks:
host:
now, when I run the yaml file as below:
docker compose -f mongo.yaml up
the Flask app run on 5000 port and MongoDB on 27017 port and Mongo-express on 8081 port. But , when I send data to MongoDB collection, I get this error:
localhost:27017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 6202b142ba93b66394f2156e, topology_type: Unknown, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('localhost:27017: [Errno 111] Connection refused')>]>
when I run Flask app without docker, everything is OK, but with yaml file the app didn't connect to MongoDB. What is the reason?
Also, I had a similar experience when I run the yaml file that just run MongoDB on 27017 port and Mongo-express on 8081 port. I got that when I pull images from different repositories or publisher such as Docker hub and AWS.ECR, I faced the same error. Is it possible that different repositories or publisher be the reason? How can I fix it?
Please help, thanks.
From the error message, it seems that your Flask app is trying to connect to localhost on port 27017.
In a Docker context, localhost means the container itself. Containers can talk to each other using the service names as host names.
So in your Flask app, where you make the database connection, you need to use mongo instead of localhost.
I'm unable to get my Phoenix app connecting to the Postgres container when using docker-compose up.
My docker-compose.yml:
version: '3.5'
services:
web:
image: "solaris_cards:latest"
ports:
- "80:4000"
env_file:
- config/docker.env
depends_on:
- db
db:
image: postgres:10-alpine
volumes:
- "/var/lib/postgresql/data/pgdata/var/lib/postgresql/data"
ports:
- "5432:5432"
env_file:
- config/docker.env
The application running in web container complains that a connection to the Postgres container is non-existing:
[error] Postgrex.Protocol (#PID<0.2134.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (db:5432): non-existing domain - :nxdomain
My env variables:
DATABASE_HOST=db
DATABASE_USER=postgres
DATABASE_PASS=postgres
I have tried running the Postgres container first separately and then running the web container but still have the same problem.
If I change the database host to 0.0.0.0 (which is what Postgres shows when running), then it seems to connect but the connection is refused rather than not found.
However docker should be able to translate the host name with out me manually inputing the ip.
Postgres was exiting due to its volume already containing data.
This was solved by cleaning the directory with:
docker-compose down -v
Error occured while trying to access Postgresql database:
(6/6) ConnectionException
An exception occurred in driver: SQLSTATE[08006] [7] could not connect to server: Connection refused
Is the server running on host "db" (172.18.0.2) and accepting
TCP/IP connections on port 5433?
i didnt add the database name in docker-compose.yml because i want to create it using the console. i tried changing the port to 5432, so it tells me the databse pfe does not exist, so when im trying to create it the command line i get this error:
SQLSTATE[08006] [7] could not translate host name "db" to address: Name or service not known
parameters.yml
parameters:
database_host: db
database_port: 5433
database_name: pfe
database_user: admin
database_password: admin
docker-compose.yml
version: '2'
services:
front:
image: nginx
ports:
- "81:80"
links:
- "engine:engine"
volumes:
- ".:/home/docker:ro"
- "./docker/front/default.conf:/etc/nginx/conf.d/default.conf:ro"
engine:
build: ./docker/engine/
volumes:
- ".:/home/docker:rw"
- "./docker/engine/php.ini:/usr/local/etc/php/conf.d/custom.ini:ro"
links:
- "db:db"
working_dir: "/home/docker"
db:
image: camptocamp/postgres:9.6
ports:
- "5433:5432"
environment:
- "POSTGRES_PASSWORD=admin"
- "POSTGRES_USER=admin"
- "PGDATA=/var/lib/postgresql/data/pgdata"
As part of the anwser, if i type php bin/console doctrine:database:create, it will run the php natively installed in my computer wich it doesnt know the db host declared in docker-compose.yml. Thats why we need to execute the php runing in docker.
So in my case this will be:
docker-compose exec engine bin/console doctrine:database:create
This is only a part of the anwser and the app is working fine with the port 5432, but i want to use another port and i need help.