how update mongo4.0.28 in docker container - mongodb

i use
docker run --name db -d mongo:4.0 --smallfiles --replSet rs0 --oplogSize 128
docker exec -ti db mongo --eval "printjson(rs.initiate())"
Then start Rocket.Chat linked to this mongo instance:
docker run --name rocketchat -p 80:3000 --link db --env ROOT_URL=http://localhost --env MONGO_OPLOG_URL=mongodb://db:27017/local -d rocket.chat
now mongo 4.0 is depricated.
how can i upgrade mongo in docker?

You can do it this way:
Create mongo dump
docker exec -i db /usr/bin/mongodump --username <username> --password <password> --authenticationDatabase admin --db <database_name> --out dump
Run new mongo container
docker run -d --name dbnew -i mongo:4.4 --smallfiles --replSet rs0 --oplogSize 128
Restore dump to new container
docker cp dbnew:/dump dump
docker exec -i dbnew /usr/bin/mongorestore --username <username> --password <password> --authenticationDatabase admin --db <database_name> /dump/<database_name>
Check new db works fine and remove old container
Rename new container
docker rename dbnew db
If you want your new container to be persistent (which I think you would) you need to use docker volumes.

Related

Connect to MongoDB config server for Sharding on a docker container running on windows10

I am using Windows 10 Operating system.
I have Docker for windows installed on my machine.
I have mongo shell for Windows installed on my machine.
I am creating the config servers using the latest mongo image from docker.
I am trying to create config servers (in a replica set; one primary and two secondaries) in order to set up Sharding for MongoDB. I am able to connect to the mongod servers if I create them as replica sets, without specifying the --configsvr parameter. But when I specify the --configsvr parameter, it fails with below error -
connecting to:
mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:27017, connection attempt
failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused
by :: No connection could be ma de because the target machine actively
refused it. : connect#src/mongo/shell/mongo.js:374:17 #(connect):2:6
exception: connect failed exiting with code 1
Case 1 - Creating 3 mongod servers as a replica set
Step 1:- Creating 3 mongod containers asia, america and europe.
C:\> docker run -d -p 40001:27017 -v C:/mongodata/data/db --name asia mongo mongod --bind_ip=0.0.0.0 --replSet "rs0"
C:\> docker run -d -p 40002:27017 -v C:/mongodata/data/db --name europe mongo mongod --bind_ip=0.0.0.0 --replSet "rs0"
C:\> docker run -d -p 40003:27017 -v C:/mongodata/data/db --name america mongo mongod --bind_ip=0.0.0.0 --replSet "rs0"
Step 2:- Execute docker ps
Step 3:- Using docker exec to connect to container named asia.
C:\> docker exec -it asia mongo
RESULT:- Successfully connected
Step 4:-Connecting to the container asia from mongoshell:-
Case 2 - Creating 3 mongod servers as config servers as part of a replica set
Step 1:- Creating 3 mongod containers asiaCS, americaCS and europeCS as config servers.
C:/> docker run -d -p 30001:27017 -v C:/mongodata/data/db --name asiaCS mongo mongod --configsvr --bind_ip=0.0.0.0 --replSet "rs1"
C:/> docker run -d -p 30002:27017 -v C:/mongodata/data/db --name europeCS mongo mongod --configsvr --bind_ip=0.0.0.0 --replSet "rs1"
C:/> docker run -d -p 30003:27017 -v C:/mongodata/data/db --name americaCS mongo mongod --configsvr --bind_ip=0.0.0.0 --replSet "rs1"
Step 2:- Execute docker ps
Step 3:- Using docker exec to connect to container named asiaCS.
docker exec -it asiaCS mongo
RESULT:- Failed to connect
Step 4:-Connecting to the container asiaCS from mongoshell:-
The only difference here is the --configsvr parameter required to start a mongod instance as a config server for MongoDB sharding. Has anyone encountered such an issue before.
P.S. - I have kept the bind_ip to 0.0.0.0 just to test connection from mongoshell, but tread with caution when doing the same for Production on non-local instances.
It's 27019 for config servers.
When you add --configsvr you need to change port mapping too:
C:/> docker run -d -p 30001:27019 -v C:/mongodata/data/db --name asiaCS mongo mongod --configsvr --bind_ip=0.0.0.0 --replSet "rs1"

Backup and restore Rocket.chat on docker with mongodb

I use this docker image : https://hub.docker.com/_/rocket.chat/
So here is the code i used :
docker run --name db -d mongo:3.0 --smallfiles
docker run --name rocketchat --link db -d rocket.chat
I tried several things, but I can't find a way to have a clean backup/restore system.
Any advice ?
For the posterity : Backing up Rocket.chat on SERVER 1 and Restore it on SERVER 2, based on the official docker image :
SERVER 1
cd /backups
docker run -it --rm --link db -v /backups:/backups mongo:3.0 mongodump -h db -o /backups/mongoBACKUP
tar czf mongoBACKUP.tar.gz mongoBACKUP/
Then send mongoBACKUP.tar.gz on SERVER 2 in /backups.
SERVER 2 (+ test on :3000)
docker run --name db -d mongo:3.0 --smallfiles
cd /backups
tar xzf mongoBACKUP.tar.gz
docker run -it --rm --name mongorestore -v /backups/mongoBACKUP:/var/dump --link db:db mongo mongorestore --host db /var/dump
docker run -p 3000:3000 --name rocket --env ROOT_URL=http://yourwebsite.test --expose 3000 --link db -d rocket.chat

Cannot login to a dockerized mongodb from host, yet still can from the docker image

I am new to Docker and am fighting with this in vain for quite sometime now. After launching mongod from inside a Docker container I can only login to that mongod instance FROM INSIDE THE DOCKER IMAGE, and not from another machine (e.g. host or another machine). Can someone help me ?
docker run --name mongo_test -v /db/mongo/test:/data/db -p 127.0.0.1:27019:27019 -d mongo --auth --port 27019
Imagine that now my docker image mongo_test has an IP of 172.17.0.2, I proceed:
docker exec -it mongo_test mongo admin --port 27019
db.createUser({ user: 'admin', pwd: 'my_admin_password', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
exit
docker exec -it mongo_test mongo admin -u admin -p 'my_admin_password' --port 27019
use test_db
db.createUser({ user: 'dev', pwd: 'dev', roles: [ { role: "userAdmin", db: "test_db" }, { role: "readWrite", db: "test_db" } ] });
exit
Following is OK
docker exec -it mongo_test mongo test_db -u dev -p dev --port 27019
Following is NOT OK: with
Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18, codeName:
"AuthenticationFailed" } at src/mongo/shell/db.js:1287
mongo --host 127.0.0.1 --port 27019 -u dev -p dev --authenticationDatabase test_db
This also does not work
mongo --host 172.17.0.2 --port 27019 -u dev -p dev --authenticationDatabase test_db
To further debug, here are the result
ps -ax | grep 'mongo'
23969 ? SLsl 0:06 mongod --auth --port 27019 --bind_ip_all
ps -ax | grep 'docker
23749 ? Ssl 19:27 /usr/bin/dockerd -H fd://
23760 ? Ssl 47:21 docker-containerd --config /var/run/docker/containerd/containerd.toml
23931 ? Sl 0:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 27019 -container-ip 172.17.0.2 -container-port 27019
23952 ? Sl 0:00 docker-containerd-shim -namespace moby -workdir /var/lib/docker/containerd/daemon/io.containerd.runtime.v1.linux/moby/8590ce0b6898719ea5a29f1f955f7fda8c6583bfcf6222fd73071446a5177ecc -address /var/run/docker/containerd/docker-containerd.sock -containerd-binary /usr/bin/docker-containerd -runtime-root /var/run/docker/runtime-runc
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
xxxxxxxxxxxx mongo "docker-entrypoint.s…" 6 hours ago Up 6 hours 27017/tcp, 0.0.0.0:27019->27019/tcp mongo_dev

Correct syntax to do mongodump of mongoDb docker instance?

I'm running an ubuntu 16.04 LTS server with some docker container. One of these containers is a mongoDB container, where my data is stored.
Now I'm trying to make a backup by mongodump.
The problem for me is, that mongoDb is running as a docker container, and the backup should be stored outside of the docker container.
I think the syntax for this is something like this:
docker run \
--rm \
-it \
--link DOCKER_CONTAINER_NAME:mongo_alias \
-v /backup:/backup \
mongo mongodump \
--host mongo_alias \
--out /backup/
But I'm not sure for the parameters I have to use...
This is what I get for my mongoDb container via docker ps:
7bee41bfa08a mongo:3.4 "docker-entrypoint..." 4 months ago Up 2 months 27017/tcp mongo_db
And this is my docker-compose file:
version: "3"
services:
mongo_db:
container_name: mongo_db
image: 'mongo:3.4'
restart: 'always'
volumes:
- '/opt/mongo/project/live:/data/db'
So it should look like this?
docker run \
--rm \
-it \
--link mongo_db:mongo_alias \ # mongo_alias can be choosen freely?
-v /backup:/backup \ # Don't understand /backup:/backup
mongo mongodump \
--host mongo_alias \
--out /backup/ # This is in the root of the server?
Define the backup to run via compose as well. This will create the new container on the same network as the main mongo container. If you have any compose network definitions you will need to duplicate them in each compose file.
Create a second compose file for the backup command: docker-compose-backup.yml
version: "3"
services:
mongo_db_backup:
image: 'mongo:3.4'
volumes:
- '/opt/mongo/project/live_backup:/backup'
command: |
mongodump --host mongo_db --out /backup/
Then run the backup
docker-compose -f docker-compose-backup.yml run mongo_db_backup
You can also do this without docker-composer, directly from your host
Backup all databases
docker exec -t your-db-container mongodump --host mongo_db --out /backup/
Restore all databases
Move your backup folder to your host volume folder
docker exec -t your-db-container mongorestore /backup/

how do i backup a database in docker

i'm running my app using docker-compose with the below yml file
postgres:
container_name: postgres
image: postgres:${POSTGRES_VERSION}
volumes:
- postgresdata:/var/lib/postgresql/data
expose:
- "5432"
environment:
- POSTGRES_DB=42EXP
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
node:
container_name: node
links:
- postgres:postgres
depends_on:
- postgres
volumes:
postgresdata:
As you can see here ,i'm using a named volume to manage postgres state.
According to the official docs, i can backup a volume like the below
docker run --rm --volumes postgresdata:/var/lib/postgresql/data -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata
Some other tutorials suggested i use the pg-dump function provided by postgres for backups.
pg_dump -Fc database_name_here > database.bak
I guess i would have to go inside the postgres container to perform this function and mount the backup directory to the host.
Is one approach better/preferable than the other?
To run pg_dump you can use docker exec command:
To backup:
docker exec -u <your_postgres_user> <postgres_container_name> pg_dump -Fc <database_name_here> > db.dump
To drop db (Don't do it on production, for test purpose only!!!):
docker exec -u <your_postgres_user> <postgres_container_name> psql -c 'DROP DATABASE <your_db_name>'
To restore:
docker exec -i -u <your_postgres_user> <postgres_container_name> pg_restore -C -d postgres < db.dump
Also you can use docker-compose analog of exec. In that case you can use short services name (postgres) instead of full container name (composeproject_postgres).
docker exec
docker-compose exec
pg_restore
Since you have
expose:
- "5432"
you can run
pg_dump -U <user> -h localhost -Fc <db_name> > 1.dump
pg_dump connects to 5432 port to make dump since it is listened by postgres in container you will dump db from container
You can also run
docker-compose exec -T postgres sh -c 'pg_dump -cU $POSTGRES_USER $POSTGRES_DB' | gzip > netbox.sql.gz
where netbox.sql.gz is the name of the backup file.
restoring would be
gunzip -c netbox.sql.gz | docker-compose exec -T postgres sh -c 'psql -U $POSTGRES_USER $POSTGRES_DB'