OS:MacOS莫哈韦沙漠
version: '3.1'
services:
apiWithKubernetesMongodb:
container_name: apiWithKubernetesMongodb
image: mongo
ports:
- "27018:27018"
expose:
- "27018"
command: --bind_ip_all --port 27018
hostname: mongodb
environment:
MONGO_INITDB_ROOT_USERNAME: mongousradmin
MONGO_INITDB_ROOT_PASSWORD: mongopassadmin
MONGO_INITDB_DATABASE: admin
networks:
- apinet
apiWithKubernetes:
container_name: api_with_kubernetes
image: sherryummen/api_with_kubernetes:v1
build:
context: .
dockerfile: Dockerfile
networks:
- apinet
volumes:
- .:/go/src/api_with_kubernetes
ports:
- 8080:8080
depends_on:
- apiWithKubernetesMongodb
links:
- apiWithKubernetesMongodb
networks:
apinet:
driver: bridge
const (
HOST = "apiWithKubernetesMongodb"
PORT = 27018
DBNAME = "service_db"
COLLECTIONNAME = "user_info"
ROOTUSER = "mongousradmin"
ROOTPASSWORD = "mongopassadmin"
)
client, err := mongo.NewClient(options.Client().ApplyURI(fmt.Sprintf("mongodb://%s:%s@%s:%d/admin", ROOTUSER, ROOTPASSWORD, HOST, PORT)))
我在docker文件上方拥有您,并进入了应该连接到mongodb的api。
但是我的执行代码给出错误“服务器选择超时\ n当前拓扑:类型:未知\ n服务器:\ nAddr:apiwithkubernetesmongodb:27018,类型:未知,状态:已连接,平均RTT:0,最后错误:connection(apiwithkubernetesmongodb: 27018 [-249])连接已关闭”
注意:对于那些迷失了方向的人来说,这全都与服务名称的大小写有关,切换到所有小写字母命名后,它便开始起作用。