Overview
In order to connect to docker, you should firstly know the name of the docker container. This can be obtained as follows:
[root@tain-cx-uatdkrdb1 ~]# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 71e7c7016763 docker-registry.tain.com/cassandra:3.10 "/docker-entrypoint.…" 9 days ago Up 9 days cas3 ea9b9f22c874 docker-registry.tain.com/cassandra:3.10 "/docker-entrypoint.…" 9 days ago Up 9 days cas2 46fac822cffe docker-registry.tain.com/cassandra:3.10 "/docker-entrypoint.…" 9 days ago Up 9 days cas1
So in order to connect we can simply execute the /bin/bash on the container:
[root@tain-cx-uatdkrdb1 ~]# docker exec --help Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] Run a command in a running container Options: -d, --detach Detached mode: run command in the background --detach-keys string Override the key sequence for detaching a container -e, --env list Set environment variables -i, --interactive Keep STDIN open even if not attached --privileged Give extended privileges to the command -t, --tty Allocate a pseudo-TTY -u, --user string Username or UID (format: <name|uid>[:<group|gid>]) -w, --workdir string Working directory inside the container [root@tain-cx-uatdkrdb1 ~]# docker exec -it cas1 /bin/bash root@46fac822cffe:/# <- We are on 1st container :)
File Migration with Mongo
Migration to and from the docker is done rather easily, even when you have to export a database or so:
Export collection
The export from Mongo is done via the standard utils like: mongoexport, but firstly we have to see our container:
[root@tpb-mts-stdkr1 ~]# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0ac74afee7b7 nginx:latest "nginx -g 'daemon of…" 7 weeks ago Up 7 weeks 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp production_nginx_1 c188736759e9 docker.tain.com/incident.backend:latest "./server" 7 weeks ago Up 33 hours 0.0.0.0:3000->8080/tcp production_incidenttool_backend_1 18e980dee75c docker.tain.com/incident.frontend:latest "nginx -g 'daemon of…" 7 weeks ago Up 33 hours 0.0.0.0:8080->80/tcp production_incidenttool_frontend_1 8385665052a3 mongo "docker-entrypoint.s…" 4 months ago Up 33 hours 0.0.0.0:27017->27017/tcp production_incidenttool_mongo_1 e03d9bc636c5 mongo "docker-entrypoint.s…" 5 months ago Up 33 hours 0.0.0.0:27018->27017/tcp recordingtool_mongo_1 e4031dbf6b08 mongo-express "tini -- /docker-ent…" 5 months ago Up 33 hours 0.0.0.0:8082->8081/tcp recordingtool_mongo_management_1 667870179b38 recordingtool/server:latest "./server" 5 months ago Up 33 hours recordingtool_server_1 5d37f142d32b netdata/netdata "/usr/sbin/run.sh" 8 months ago Up 27 hours 0.0.0.0:19999->19999/tcp netdata (reverse-i-search)`': ^C
Then we can connect to it as follows:
[root@tpb-mts-stdkr1 ~]# docker exec -it 8385665052a3 /bin/bash
And then we can connect to the mongo, if we want to:
root@8385665052a3:/# mongo admin -u root -p password MongoDB shell version v4.0.4 connecting to: mongodb://127.0.0.1:27017/admin Implicit session: session { "id" : UUID("310e30d1-8cf6-4e4a-b1a4-206d3c9104a1") } MongoDB server version: 4.0.4 Server has startup warnings: 2020-01-09T01:33:38.696+0000 I CONTROL [initandlisten] 2020-01-09T01:33:38.696+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2020-01-09T01:33:38.696+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2020-01-09T01:33:38.696+0000 I CONTROL [initandlisten] 2020-01-09T01:33:38.696+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2020-01-09T01:33:38.696+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2020-01-09T01:33:38.696+0000 I CONTROL [initandlisten] --- Enable MongoDB's free cloud-based monitoring service, which will then receive and display metrics about your deployment (disk utilization, CPU, operation statistics, etc). The monitoring data will be available on a MongoDB website with a unique URL accessible to you and anyone you share the URL with. MongoDB may use this information to make product improvements and to suggest MongoDB products and deployment options to you. To enable free monitoring, run the following command: db.enableFreeMonitoring() To permanently disable this reminder, run the following command: db.disableFreeMonitoring() --- >
Export of the database, as said again, is done with the standard utils:
root@8385665052a3:/# mongoexport --db=incident --collection=procedures --query='{"game_type":"BAC"}' --out=bac_procedures.json -u "root" -p "password" --authenticationDatabase admin 2020-01-10T10:54:55.975+0000 connected to: localhost 2020-01-10T10:54:55.986+0000 exported 71 records root@8385665052a3:/# exit
And then we can copy it to the main machine:
root@8385665052a3:/# exit [root@tpb-mts-stdkr1 ~]# [root@tpb-mts-stdkr1 ~]# [root@tpb-mts-stdkr1 ~]# docker cp 8385665052a3:bac_procedures.json bac_procedures.json [root@tpb-mts-stdkr1 ~]# ls -lart total 784672 -rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc -rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc -rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile -rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout drwxr-----. 3 root root 19 Aug 27 2018 .pki drwx------. 2 root root 26 Aug 27 2018 .docker drwx------. 3 root root 18 Dec 4 2018 .config -rw-r--r--. 1 root root 2162 Jan 14 2019 star.amazinggaming.com.crt -rw-r--r--. 1 root root 1674 Jan 14 2019 star.amazinggaming.com.key -rw-r--r--. 1 root root 176 Mar 22 2019 .bashrc dr-xr-xr-x. 18 root root 239 Jun 19 2019 .. drwxr-xr-x. 3 root root 192 Jul 4 2019 pit.configurator drw-r--r--. 7 root root 4096 Jul 22 08:39 incident.frontend drwxr-xr-x. 5 root root 131 Aug 12 05:07 recording.tool -rw-r--r--. 1 root root 1894 Aug 12 05:20 production.json drwxr-xr-x. 11 root root 4096 Aug 21 13:25 net.box -rw-r--r--. 1 root root 299124736 Nov 15 08:35 incident.backend.tar drwxr-xr-x. 3 root root 31 Nov 15 08:36 backup drw-r--r--. 8 root root 244 Nov 18 06:56 incident.backend drwxr-xr-x. 2 root root 196 Nov 20 14:44 nginx -rw-r--r--. 1 root root 106871452 Jan 9 08:23 logs_bacnkend.txt -rw-r--r--. 1 root root 365611371 Jan 9 08:28 logs_mango.txt drwx------. 2 root root 48 Jan 10 10:01 .ssh -rw-------. 1 root root 26656 Jan 10 10:51 .bash_history -rwxr-xr-x. 1 root root 31719936 Jan 10 10:51 incident.frontend.tar -rw-r--r--. 1 root root 92587 Jan 10 10:54 bac_procedures.json
We can also perform couple operations like, exchange the BAC → DT
sed 's/BAC/DT/g' bac_procedures.json | tee dt_procedures.json
We can also remove the _id column as follows:
sed -i '/"_id":/s/"_id":[^,]*,//' dt_procedures.json
And we can take it back:
docker cp dt_procedures.json 8385665052a3:dt_procedures.json