Mongo-db container shuts down after 30 s - mongodb

I am developping a web app which has two containers the first container is a web app developed in flask and the second a mongodb image. I want to run them at the same time so I have written the following docker-compose.yml
version: '3.7'
services:
flask:
build: .
depends_on:
- mongo
ports:
- "5000:5000"
volumes:
- ./flask:/flask
mongo:
image: mongo:latest
ports:
- "27017:27017"
volumes:
- ./dump:/dump # data
- ./datos_db:/data/db # persistance database
First I run $docker-compose build with no errors,then I get the following error when I run $ docker-compose up:
mongo_1 | 2019-10-30T16:18:47.420+0000 error connecting to host: could not connect to server: server selection error: server selection timeout
mongo_1 | current topology: Type: Single
mongo_1 | Servers:
mongo_1 | Addr: localhost:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection(localhost:27017[-121]) connection is closed
mongo_1 |
practica4_mongo_1 exited with code 1
As I understand it the container waits too much time for localhost:271017 to be open and decides to shut down. (I have also tried with port 49155)
Is there anything I am missing and that makes my docker crash?

Related

Mongodb connection refused from other application in docker-compose

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

lookup host.docker.internal: no such host [duplicate]

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.

Works on mac, but on windows get ECONNREFUSED, docker-toolbox

This is my docker-compose file that runs when I do docker-compose up -d on mac. I am now trying this on windows, with docker-toolbox (as docker desktop isn't supported on my windows). I run my application on http://localhost:1337 and then the application needs to talk to inside this container. Works totally fine on mac.
version: '3.4'
services:
# Add a redis instance to which our app can connect. Quite simple.
redis-dev:
image: redis:5.0.5-alpine
ports:
- 6379:6379
# Add a postgres instance as our primary data store
postgres-dev:
image: postgres:11.5-alpine
environment:
- POSTGRES_DB=the-masjid-app
ports:
- 5432:5432
volumes:
# Here we specify that docker should keep postgres data,
# so the next time we start docker-compose,
# our data is intact.
- the-masjid-app-pgdata-dev:/var/lib/postgresql/data
# Add a postgres instance as our primary data store
postgres-test:
image: postgres:11.5-alpine
environment:
- POSTGRES_DB=the-masjid-app
ports:
- 5433:5432
# Here we can configure settings for the default network
networks:
default:
# Here we can configure settings for the postgres data volume where our data is kept.
volumes:
the-masjid-app-pgdata-dev:
Doing the same thing in Windows is giving me:
Error: Redis connection to localhost:6379 failed - connect
ECONNREFUSED 127.0.0.1:6379 at TCPConnectWrap.afterConnect [as
oncomplete] (net.js:1141:16)
Any ideas on how to fix?

GOLANG client docker container getting unreachable when attempting to connect to mongodb container

I have a simple program written in golang which connect to a mongodb instance thus:
func main() {
session, err := mgo.Dial("localhost:27017")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
ensureIndex(session)
}
I use docker compose to spin up two containers one to spin up my GOLANG API and the other to spin up mongo:
version: "3.3"
services:
api:
image: golang:latest
volumes:
- .:/go
working_dir: /go
environment:
- GOPATH=/go
command:
go run /go/src/main.go
ports:
- "8082:8000"
depends_on:
- db
networks:
- api-net
db:
build: .
expose:
- '27017'
container_name: 'mongo'
ports:
- "27017:27017"
networks:
- api-net
networks:
api-net:
driver: bridge
I use a Dockerfile in order to spin up mongo with a conf file that sets the instance to bind to all IPV4 and IPV6 networks:
FROM mongo:latest
COPY mongod.conf /etc/mongod.conf
CMD mongod --config /etc/mongod.conf##
and this is the mongod.conf file:
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
When I run docker-compose.up this is what I get:
Building db
Step 1/3 : FROM mongo:latest
---> a0f922b3f0a1
Step 2/3 : COPY mongod.conf /etc/mongod.conf
---> Using cache
---> f270e718c11e
Step 3/3 : CMD mongod --config /etc/mongod.conf
---> Running in 89ffc2495a2a
Removing intermediate container 89ffc2495a2a
---> fe2677d53122
Successfully built fe2677d53122
Successfully tagged carsupermarket_db:latest
WARNING: Image for service db was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating mongo ... done
Creating carsupermarket_api_1 ... done
Attaching to mongo, carsupermarket_api_1
api_1 | panic: no reachable servers
api_1 |
api_1 | goroutine 1 [running]:
api_1 | main.main()
api_1 | /go/src/main.go:38 +0x291
api_1 | exit status 2
I've trawled Google and stackoverflow and the only thing I could manage to find which is vaguely related to my issue is:
mongod --bind_ip using docker-compose version 2
however, my docker-compose.yml file as it stands 'Should' on paper work.
Can someone please point me in the correct direction as to why my GOLANG code cannot find my mongodb instance.
I think that when you call mgo.Dial("localhost:27017") the localhost part refers to the localhost within the golang container, and not the host the the containers are running on.
Docker will resolved container names as host names, change the dial to mgo.Dial("mongo:27017") and it should work.
From the docker container networking bridge docs
User-defined bridges provide automatic DNS resolution between containers.
Containers on the default bridge network can only access each other by
IP addresses, unless you use the --link option, which is considered
legacy. On a user-defined bridge network, containers can resolve each
other by name or alias.
https://docs.docker.com/network/bridge/
version: "2"
services:
api:
image: golang:latest
volumes:
- .:/go
# Work dir should be defined in dockerfile
#working_dir: /go
environment:
- GOPATH=/go
command:
go run /go/src/main.go
ports:
- "8082:8000"
depends_on:
- db
db:
build: .
container_name: 'mongo'
# You should define some volumes here if you want the data to persist.
You should be able to connect from the golang container to the mongo container using inter container communication using the hostname: 'mongo'

Drone.io | Create docker-compose settings

I can't connect to Drone.io with my GitHub.
And have several problems with the app:
1) drone-agent can't connect to server
dodge#comp:$drone agent
28070:M 15 Nov 22:04:01.906 * connecting to server http://<my_ip>
28070:M 15 Nov 22:04:01.906 # connection failed, retry in 15s. websocket.Dial http://<my_ip>: bad scheme
2) I can't add Postgresql to docker-compose.
When I add this text from your site
DRONE_DATABASE_DRIVER: postgres
DRONE_DATABASE_DATASOURCE: postgres://root:password#1.2.3.4:5432/postgres?sslmode=disable
I have this error
INFO: 2017/11/15 19:42:33 grpc: addrConn.resetTransport failed to create client transport: connection error: desc = "transport: Error while dialing dial tcp 172.18.0.2:9000: getsockopt: connection refused"; Reconnecting to {drone-server:9000 <nil>}
3) When I use only a server and an agent in docker-compose I have this error
dodge#comp:$drone server
ERRO[0000] sql: unknown driver "sqlite3" (forgotten import?)
FATA[0000] database connection failed
docker-compose.yml
version: '2'
services:
drone-server:
image: drone/drone:0.8
ports:
- 80:8000
- 9000
volumes:
- /var/lib/drone:/var/lib/drone/
- ./drone:/var/lib/drone/
restart: always
environment:
- DRONE_DEBUG=true
- DRONE_OPEN=true
- DRONE_HOST=http://172.18.0.2
- DRONE_GITHUB=true
- DRONE_GITHUB_CLIENT=secretid
- DRONE_GITHUB_SECRET=secretpass
- DRONE_SECRET=password
drone-agent:
image: drone/agent:0.8
command: agent
restart: always
depends_on: [ drone-server ]
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
- DRONE_SERVER=drone-server:9000
- DRONE_SECRET=password
4) I cannot start tests in my project. Maybe I missed something during the setup.
$ drone server
$ drone agent
I see the above commands in your examples. These commands are only available in drone 0.7 and below. Drone 0.8 uses drone-server and drone-agent binaries. There seems to be some version disconnect here.
connection failed, retry in 15s. websocket.Dial
drone 0.7 and below used websockets. I see in the docker-compose example you are using drone 0.8 which uses http2 and grpc. There seems to be a disconnect in your configuration vs the version of drone you are using.
sql: unknown driver "sqlite3"
this happens when you compile drone with CGO disabled, or use a version of drone that has been compiled with CGO disabled. If CGO is disabled the sqlite3 driver is not compiled into the binary. Are you trying to build drone from source?
grpc: addrConn.resetTransport failed to create client transport
This error comes from the agent, and is therefore unrelated to a postgres configuration. You should not be providing your agent with a postgres configuration, only the server.
version: '2'
services:
drone-server:
image: drone/drone:latest
ports:
- 80:8000
- 9000:9000
volumes:
- /var/lib/drone:/var/lib/drone/
- ./drone:/var/lib/drone/
restart: always
environment:
- DRONE_DEBUG=true
- DRONE_HOST=http://<container_ip_server>
- DRONE_OPEN=true
- DRONE_GITHUB=true
- DRONE_GITHUB_CLIENT=<client_git>
- DRONE_GITHUB_SECRET=<secret_git>
- DRONE_SECRET=<secret_drone>
- DRONE_GITHUB_MERGE_REF=true
drone-agent:
image: drone/agent:latest
command: agent
restart: always
depends_on: [ drone-server ]
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
- DRONE_SERVER=drone-server:9000
- DRONE_SECRET=<drone_secret>
This workes fine.