I made a go App conected to MongoDB Atlas and works fine when run locally, but when i tried to create docker-compose i get this error
error parsing uri: lookup _mongodb._tcp.cluster0.mrknb.mongodb.net on 127.0.0.11:53: read udp 127.0.0.1:37379->127.0.0.11:53: i/o timeout
My connection string is :
mongodb+srv://apiVentas:<password>#cluster0.mrknb.mongodb.net/<dbname>?retryWrites=true&w=majority
My DockerFile is:
FROM golang:alpine AS builder
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64
WORKDIR /build
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN go test ./...
RUN go build -o main .
WORKDIR /dist
RUN cp /build/main .
FROM scratch
COPY --from=builder /dist/main /
ENTRYPOINT ["/main"]
And Docker-compose is
version: "3"
services:
web:
container_name: apiVentas
restart: always
build: .
ports:
- "3000:3000"
volumes:
- .:/home/perajim/go/src/api.ventas
dns:
- 1.1.1.1
- 1.0.0.1
- 8.8.8.8
I add my IP to list in mongoDB Atlas
It's necessary some configuration in docker?
MongoDB Atlas runs on port 27017 , change the port binding and then try.
Related
i can't connect to my localhost mongodb from docker container.
I can get page nginx if run curl 172.17.0.1 or (host.docker.internal) inside docker container, but if i try use another port curl not outputs nothing. Any help?
On windows machine all works fine, but i noticed that there is another port for connect to host machine host.docker.internal = 192.168.65.2
I think maybe linux blocks ports for docker container?
System: Ubuntu 18.04.6 LTS
Docker: 20.10.22, build 3a2c30b
Docker file
# build step
# Base image
FROM node:18
# Create app directory
WORKDIR /usr/src/app
# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./
# Install app dependencies
RUN npm install
# Bundle app source
COPY . .
# Creates a "dist" folder with the production build
RUN npm run build
# Start the server using the production build
CMD [ "node", "dist/main.js" ]
docker-compose file
version: '3.8'
services:
api:
build: .
container_name: api-container
volumes:
- .:/app
- /app/node_modules
ports:
- 127.0.0.1:${APP_PORT}:${APP_PORT}
env_file:
- .env
I have a docker-compose file which run 2 containers.
One of the service is postgres and the other one is my backend service.
When I try to resolve postgres or try to connect it from cli of the backend service , it is successfull.
But when I try to initialize database with hostname , I got following error:
2022/09/01 08:49:53 /src/domain/domain.go:29 [error] failed to
initialize database, got error failed to connect to host=postgres user=devuser database=backenddev: hostname resolving error
(lookup postgres: device or resource busy) panic: failed to connect to
host=postgres user=devuser database=backenddev: hostname
resolving error (lookup postgres: device or resource busy)
goroutine 1 [running]:
github.com/anilkusc/backend/api.(*Api).Init(0xc00000e150)
/src/api/api.go:34 +0x76b github.com/anilkusc/backend/api.(*Api).Start(0xc00000e150)
/src/api/api.go:87 +0x25 main.main()
/src/main.go:48 +0x15f
when I try to connect postgresql from another container cli , I got the following:
root#8a0824fca084:/# nc -vz postgres 5432
Connection to postgres (172.25.0.2) 5432 port [tcp/postgresql] succeeded!
root#8a0824fca084:/# curl postgres:5432
curl: (52) Empty reply from server
Here is related code block:
d.Database, err = gorm.Open(postgres.Open(os.Getenv("DB_CONN")), &gorm.Config{})
if err != nil {
return err
}
Here is compose file :
version: '3.6'
services:
postgres:
image: postgres:14.5
restart: always
environment:
POSTGRES_PASSWORD: <>
POSTGRES_DB: backenddev
POSTGRES_USER: devuser
ports:
- 5432:5432
backend:
image: my-service:v0.0.2
restart: always
environment:
ENV: dev
STORE_KEY: 1234
DB_CONN: host=postgres user=devuser password=<> dbname=backenddev port=5432
ports:
- 8080:8080
depends_on:
- postgres
Here is dockerfile of backend service :
FROM golang:1.18.4 as build
WORKDIR /src
COPY go.sum go.mod ./
RUN go mod download
COPY . .
RUN go build -a -ldflags "-linkmode external -extldflags '-static' -s -w" -o /bin/app.
FROM alpine
WORKDIR /app
COPY --from=build /bin/app .
CMD ["./app"]
If I try to connect to database with external ip and port on backend service it is also successfull but no luck for internal connection to postgresql.
Why my application can not resolve postgres host even though its host container can ?
thanks to #Brits.
It is probably related with incompatibility between alpine container and golang 1.18.4 container dns libraries.
I changed my container image to debian from alpine and the issue has been resolved.
FROM golang:1.18.4 as build
WORKDIR /src
COPY go.sum go.mod ./
RUN go mod download
COPY . .
RUN go build -a -ldflags "-linkmode external -extldflags '-static' -s -w" -o /bin/app.
FROM debian:buster-slim #just changed here!
WORKDIR /app
COPY --from=build /bin/app .
CMD ["./app"]
Trying to dockerize, nests, and Prisma.
Nest is responding correctly to curl requests and and I can connect to the Postgres server fine with this command
--- docker compose exec postgres psql -h localhost -U postgres -d webapp_dev
Everything works until i try to run
npx prisma migrate dev --name init
then i get back
Error: P1001: Can't reach database server at `postgres`:`5432`
Here is my code:
docker-compose.yml
version: "2"
services:
backend:
build: .
ports:
- 3000:3000
- 9229:9229 # debugger port
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
command: yarn start:debug
restart: unless-stopped
depends_on:
- postgres
environment:
DATABASE_URL: postgres://postgres#postgres/webapp_dev
PORT: 8000
postgres:
image: postgres:14-alpine
ports:
- 5432:5432
environment:
POSTGRES_DB: webapp_dev
POSTGRES_HOST_AUTH_METHOD: trust
DockerFile
FROM node:16
# Create app directory, this is in our container
WORKDIR /usr/src/app
# Install app dependencies
# Need to copy both package and lock to work
COPY package.json yarn.lock ./
RUN yarn install
COPY prisma/schema.prisma ./prisma/
RUN npx prisma generate
# Bundle app source
COPY . .
RUN yarn build
EXPOSE 8080
CMD ["node": "dist/main"]
.env
//.env
DATABASE_URL=postgres://postgres#postgres/webapp_dev
not sure if this is the only issue but your db url does not contain the db secret in it
DATABASE_URL: postgres://postgres:mysecret#postgres/webapp_dev?schema=public
I got the same error I solved it after adding ?connect_timeout=300 at my DATABASE_URL
MongoServerSelectionError: getaddrinfo ENOTFOUND mongo
is the error I'm getting when I try to run docker-compose build.
This is my docker-compse.yml file
version: '3'
services:
app:
container_name: node-app
build: .
ports:
- "3000:3000"
restart: always
volumes:
- ./uploads:/app/uploads
links:
- mongo
mongo:
container_name: mongo
image: mongo
volumes:
- ./data:/data/db
ports:
- "27017:27017"
command: mongod
as well as my Dockerfile
# https://docs.docker.com/samples/library/node/
ARG NODE_VERSION=12.10.0
# https://github.com/Yelp/dumb-init/releases
ARG DUMB_INIT_VERSION=1.2.2
# Build container
FROM node:${NODE_VERSION}-alpine AS build
ARG DUMB_INIT_VERSION
WORKDIR /home/node
RUN apk add --no-cache build-base python2 yarn && \
wget -O dumb-init -q https://github.com/Yelp/dumb-init/releases/download/v${DUMB_INIT_VERSION}/dumb-init_${DUMB_INIT_VERSION}_amd64 && \
chmod +x dumb-init
ADD . /home/node
RUN yarn install && yarn build && yarn cache clean
# Runtime container
FROM node:${NODE_VERSION}-alpine
WORKDIR /home/node
COPY --from=build /home/node /home/node
EXPOSE 3000
CMD ["./dumb-init", "yarn", "start"]
My connection string in the code is
mongodb://mongo:27017/{db_name}
When I run docker ps -a, I can clearly that my mongo image is there. I've googled this issue to no extent, and tried ridiculous combinations of connection strings to try and connect to mongo, but does anyone have any supplemental information or debugging advice to overcome this?
The issue is most probably that you start mongod without passing the --bind_ip_all parameter. By default, mongod only binds to 127.0.0.1 as also stated by other SO posts.
This is my first time with docker, I'm working on this problem for two days, it would make me very happy to find a solution.
I'm running docker-compose.yml file with "docker-compose up":
version: '3.3'
services:
base:
networks:
- brain_storm-network
volumes:
- brain_storm-storage:/usr/src/brain_storm
build: "./brain_storm"
data_base:
image: mongo
volumes:
- brain_storm-storage:/usr/src/brain_storm
networks:
- brain_storm-network
ports:
- '27017:27017'
api:
build: "./brain_storm/api"
volumes:
- brain_storm-storage:/usr/src/brain_storm
networks:
- brain_storm-network
ports:
- 5000:5000
depends_on:
- data_base
- base
restart: on-failure
the base Dockerfile inside ./brain_storm does the following:
FROM brain_storm-base:latest
RUN mkdir -p /usr/src/brain_storm/brain_storm
ADD . /usr/src/brain_storm/brain_storm
and when running the Dockerfile inside brain_storm/api
FROM brain_storm-base:latest
CMD cd /usr/src/brain_storm \
&& python -m brain_storm.api run-server -h 0.0.0.0 -p 5000 -d mongodb://0.0.0.0:27017
I'm getting this error :
brain_storm_api_1 exited with code 1
api_1 | /usr/local/bin/python: Error while finding module specification for 'brain_storm.api' (ModuleNotFoundError: No module named 'brain_storm')
pwd says I'm in '/' and not in the current directory when running the base Dockerfile,
so that might be the problem but how do I solve it without going to /home/user/brain_storm in the Dockerfile, because I want to keep the location of brain_storm folder general.
How can I make Dockerfile see and take the file from the current directory (where the Dockerfile file is) ?
You should probably define WORKDIR command in both your Dockerfiles. The WORKDIR command is used to define the working directory of a Docker container at any given time. Any RUN, CMD, ADD,COPY, or ENTRYPOINT command will be executed in the specified working directory.:
base:
FROM brain_storm-base:latest
WORKDIR /usr/src/brain_storm
COPY . .
api:
FROM brain_storm-base:latest
WORKDIR /usr/src/brain_storm
CMD python -m brain_storm.api run-server -h 0.0.0.0 -p 5000 -d mongodb://0.0.0.0:27017