Monday, September 16, 2019

docker: [RESOLVED] Expose dockerised mongodb listenport, and connection failed :(

I created dockerised mongodb, but encountered login failure via mongo shell.
An investigation found that my depreciated mongo shell version caused issue.
After that, I updated the software version, and remote connection got successful!

#prepare persistent data location with docker volume
docker volume create my-vol
docker volume inspect my-vol
[
    {
        "CreatedAt": "2019-09-15T01:24:25+09:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/my-vol/_data",
        "Name": "my-vol",
        "Options": {},
        "Scope": "local"
    }
]
# Copy dumped database file for restoration
cp -R ./testCol /var/lib/docker/volumes/my-vol/_data


# Run mongo container mounting created volume, and specify mongodb data directory as /data/db
docker run --name mongo-dev -d -v my-vol:/data/db  -e DATA_DIR=/data/db -p 27017 mongo
d2b759805a018d1a71b835c7d6dffdda68cbbfa20f7f891fb381a53fd7b49ed0


# Restore database from dumpfile
docker exec -it mongo-dev mongorestore --db articles /data/db/articles


# Check local port
docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
d2b759805a01        mongo               "docker-entrypoint.s…"   23 seconds ago      Up 21 seconds       0.0.0.0:32768->27017/tcp   mongo-dev


# Try to connect mongo console, but it fails
mongo --host 127.0.0.1:32768
MongoDB shell version v3.4.21
connecting to: mongodb://127.0.0.1:32768/
2019-09-15T23:47:20.861+0900 E QUERY    [thread1] Error: network error while attempting to run command 'whatsmyuri' on host '127.0.0.1:32768'  :
connect@src/mongo/shell/mongo.js:240:13
@(connect):1:6
exception: connect failed


# Check listen status, dockerised mongo is bound to ipv6 port 32768
netstat -ltn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp6       0      0 ::1:25                  :::*                    LISTEN
tcp6       0      0 :::32768                :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN


# Check container with interactive console
docker run -it --link=mongo-dev:mongo mongo bash


# I can see mongodb collections via console
mongo --host 172.17.0.2:27017
> show databases;
admin          0.000GB
articles       0.000GB
config         0.000GB
local          0.000GB
sepfourteenth  0.000GB
> use articles;
switched to db articles
> db.testCol.find({});
{ "_id" : ObjectId("5d6682061f2af8077f940255"), "name" : "大丈夫", "query" : "%E5%A4%A7%E4%B8%88%E5%A4%AB", "tweet_volume" : 490680, "date" : "8/28/2019, 10:30:45 PM" }
{ "_id" : ObjectId("5d6682061f2af8077f940256"), "name" : "テレビ", "query" : "%E3%83%86%E3%83%AC%E3%83%93", "tweet_volume" : 337330, "date" : "8/28/2019, 10:30:45 PM" }


# Let me specify source IP and port
docker run --name mongo-dev -d -v my-vol:/data/db  -e DATA_DIR=/data/db -p 127.0.0.1:32768:27017  mongo --bind_ip_all
a3f9ab22beaed1b13e73f3ab0d4ccd452a8f1b4c33688c7d0906acd0d2f264ed


# Now I can see the listen port bind to ipv4
netstat -ltn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:32768         0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp6       0      0 ::1:25                  :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN

docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                        NAMES
a3f9ab22beae        mongo               "docker-entrypoint.s…"   42 seconds ago      Up 41 seconds       127.0.0.1:32768->27017/tcp   mongo-dev


# Unfortunately another issue was found when I tried to connect dockerised mongo.
mongo --host 127.0.0.1:32768
MongoDB shell version v3.4.21
connecting to: mongodb://127.0.0.1:32768/
2019-09-16T20:15:42.149+0900 E QUERY    [thread1] Error: network error while attempting to run command 'whatsmyuri' on host '127.0.0.1:32768'  :
connect@src/mongo/shell/mongo.js:240:13
@(connect):1:6
exception: connect failed


# bind_ip_all was specified.
docker container inspect mongo-dev | grep -n -A10 bind
7:            "--bind_ip_all"
8-        ],
9-        "State": {
10-            "Status": "running",
11-            "Running": true,
12-            "Paused": false,
13-            "Restarting": false,
14-            "OOMKilled": false,
15-            "Dead": false,
16-            "Pid": 1635,
17-            "ExitCode": 0,
--
193:                "--bind_ip_all"
194-            ],


# Unfamiliar error was written on log file.
I realized the problem sits on host side.

{"log":"2019-09-16T15:19:20.315+0000 E  -        [conn2] Assertion: Location34348: cannot translate opcode 2010 src/mongo/rpc/message.h 120\n","stream":"stdout","time":"2019-09-16T15:19:20.315419954Z"}



# Checked mongo shell version on host.
mongo --version


# Update yum repository file for fetching newer version of mongo shell.
yum remove mongodb-org-shell
vi /etc/yum.repos.d/mongodb-org.repo

name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc


# Reintall mongo shell.
yum remove mongodb-org-shell
yum install mongodb-org-shell


# Now everything alright! :)
The trouble shooting was really tough though...(ToT)

mongo 127.0.0.1:32768                                 MongoDB shell version v4.0.12
connecting to: mongodb://127.0.0.1:32768/test?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("6e2290f5-c297-4c81-8c25-c1197311c8d9") }
MongoDB server version: 4.2.0

> show databases;
admin          0.000GB
articles       0.000GB
config         0.000GB
local          0.000GB
sepfourteenth  0.000GB
> exit
bye

No comments:

Post a Comment