我有一点问题,希望你们能帮助我。 我将Docker用于Jenkins和我的MongoDB。问题是我无法在Jenkins工作中连接到MongoDB。
这可以从我的机器到本地网络服务器,但是显然不是从一个容器到另一个容器,尽管我已经将容器移到了相同的Docker网络中。
您知道这可能是什么原因吗?
Docker运行:
- docker run --network jenkins --publish 27017:27017 -d mongo:latest
- docker container run --name jenkins-docker --rm --detach --privileged --network jenkins --network-alias docker --env DOCKER_TLS_CERTDIR=/certs --volume jenkins-docker-certs:/certs/client --volume jenkins-data:/var/jenkins_home --volume "$HOME":/home docker:dind
- docker container run --name jenkins-tutorial --rm --detach --network jenkins --env DOCKER_HOST=tcp://docker:2376 --env DOCKER_CERT_PATH=/certs/client --env DOCKER_TLS_VERIFY=1 --volume jenkins-data:/var/jenkins_home --volume jenkins-docker-certs:/certs/client:ro --volume "$HOME":/home --publish 8080:8080 jenkinsci/blueocean
Docker网络:
docker network inspect jenkins
[
{
"Name": "jenkins",
"Id": "ec143f870e7bd82a29010fc12089accbd8c4d6f6422f7857403d1d1ebdd9fdf8",
"Created": "2020-04-05T01:25:58.154579674+02:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"1843d4e5ab7a58c54b859ab3c4160429a407acf144ec60fbec76a581372b964e": {
"Name": "compassionate_faraday",
"EndpointID": "6ea7b8bd4fd76fda2eecd08c52c7654ad932184df9eef22eb543d79a04328458",
"MacAddress": "02:42:ac:12:00:04",
"IPv4Address": "172.18.0.4/16",
"IPv6Address": ""
},
"df4843d6803bfb93c3179644d7c3dc11ba4de7bf03bda4d32edd250310691986": {
"Name": "jenkins-docker",
"EndpointID": "79f13b01f3b957f9d6bdedf19b455bf34810124c45a842c41dd907cf6da929fe",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
},
"f45ffa64e0c9bbed1c51bac05be0298e83f7704a953a39e80afc56de9d9bdf59": {
"Name": "jenkins-tutorial",
"EndpointID": "8fe2d4fcf7a34aa0a9b3c12af8ce7aab7672b850e811321eb059842049840754",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
编辑:使用来自网络的Docker IP,我在Java程序中获得了与MongoDB的连接。
但这不是将IP地址传递给MongoClient的解决方案。有没有一种抽象的方法?
JavaCode:
final Datastore datastore = morphia.createDatastore(new MongoClient("172.18.0.4", 27017), "rssfeeddb");