Run mongo script in deployed docker swarm container - mongodb

I have deployed a database using docker swarm
docker stack deploy -c docker-compose.yml app
docker-compose.yml
version: '3.1'
services:
database:
image: mongo:latest
I'd like to run a JavaScript file script.js from my host in the deployed database container:
docker exec \
app_database.1.$(docker service ps -f 'name=app_database.1' app_database -q) \
mongo script.js
However, the file script.js does not exist in the container (only on my host). How can I run it without restarting the database service?

A script can be sent on stdin to the mongo client.
Create a script locally:
print('The mongodb version is: '+db.version())
Then run
$ cat script.js | docker exec $container mongo --quiet
The mongodb version is: 3.6.3

Related

Docker and MongoDB: Start Mongo and Import Data via Dockerfile

I need to use MongoDB with Docker. Up until now i am able to create a container, start the Mongo server and access it from the host machine (via Compass).
What i want to do next is import data from a script into the Mongo database that is running in the container.
I'm getting the following error when trying to import the data:
Failed: error connecting to db server: no reachable servers
Where's what i'm doing...
docker-compose.yml:
version: '3.7'
services:
mongodb:
container_name: mongodb_db
build:
context: .
dockerfile: .docker/db/Dockerfile
args:
DB_IMAGE: mongo:4.0.9
ports:
- 30001:27017
environment:
MONGO_DATA_DIR: /data/db
MONGO_LOG_DIR: /dev/null
db_seed:
build:
context: .
dockerfile: .docker/db/seed/Dockerfile
args:
DB_IMAGE: mongo:4.0.9
links:
- mongodb
mongodb Dockerfile:
ARG DB_IMAGE
FROM ${DB_IMAGE}
CMD ["mongod", "--smallfiles"]
db_seedDockerfile:
ARG DB_IMAGE
FROM ${DB_IMAGE}
RUN mkdir -p /srv/tmp/import
COPY ./app/import /srv/tmp/import
# set working directory
WORKDIR /srv/tmp/import
RUN mongoimport -h mongodb -d dbName--type csv --headerline -c categories --file=categories.csv #Failed: error connecting to db server: no reachable servers
RUN mongo mongodb/dbName script.js
What am I doing wrong here? How can i solve this issue?
I would like to keep the current file organisation (docker-compose, mongodb Dockerfile and db_seed Dockerfile).
I found the reason for the issue. The import command is being executed prior to the mongo service being started.
To solve this issue i created a .sh script with the import commands and execute it using ENTRYPOINT. This way the script is only executed after the db_seed container is created. Since the the db_seed container depends on the mongobd container, it will only execute the script after the mongo service is started.
db_seedDockerfile
ARG DB_IMAGE
FROM ${DB_IMAGE}
RUN mkdir -p /srv/tmp/import
COPY ./app/import /srv/tmp/import
ENTRYPOINT ["./srv/tmp/import/import.sh"]

How to connect SpringBoot application inside Docker to outside PostgreSQL

I have Java SpringBoot Web-application working on host machine. Application connects to PostgreSQL database. All works well. OS - Ubuntu 18.
Now i need to move application in Docker container, except for PostgreSQL which will remain on host machine.
I installed Docker, rise up container, but my app inside docker cannot connect to PostgreSQL database with default settings (localhost).
Here is my application.properties file:
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/webdemodb
spring.datasource.username=postgres
spring.datasource.password=123
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL95Dialect
spring.jpa.generate-ddl=true
Here is my Dockerfile:
FROM java:8
WORKDIR /
ADD target/webaccount-1.0-SNAPSHOT.jar app.jar
EXPOSE 8080
RUN fc-cache -f -v
ENTRYPOINT ["java","-jar","/app.jar"]
I read about Docker's networking but didn't find solution. What i need to setup?
Artemiy, thanks! For simple usage i've just applied the next option when running container:
--network="host"
Full command:
docker run -d -p 9000:8080 --network="host" --name webaccount webaccount:1.0

repair command in mongodb in docker container

My client has mongodb running in docker container, one morning the db crashed and logs says "unclean shutdown detected". I tried for repairing mongodb but not able to do as it is running under docker container. The reason is that as i have to shutdown mongodb for running repair command and once i shutdown the mongodb then docker throws me out of the container because only one process was running and as it was stopped container was also stopped, now i am not able to run any mongod commands as i am out of container and container is stopped along with mongod instance.
So, can anyone help me out of this, how can i run repair command from outside of docker container ?
Can i run repair command without stopping mongod ?
Please help !!!
Thanks
Irshad
The example command below fixed my issue with corrupted Wt files.
docker run -it -v <path>:/data/db <image-name> mongod --repair
Just replace path and image-name with yours
I had this same thing when my Mac decided to restart and install an update this morning, grr.
Normally my mongo container is started with a docker-compose up command, which uses a docker-compose.yml file for all the settings, something like...
version: "3"
services:
mongo:
image: mongo:3.6.12
container_name: mongo
ports:
- 27017:27017
volumes:
- ~/mongo/data/db:/data/db
- ~/mongo/db/configdb:/data/configdb
To run a basic docker command with those settings I did...
# Kill any existing mongo containers
docker rm mongo
# Repair the db
docker run -it -v ~/mongo/data/db:/data/db -v ~/mongo/db/configdb:/data/configdb --name mongo -d mongo:3.6.12 mongod --repair
# Has it finished the repair yet?
docker ps -a
After about a minute the container exited, the database was repaired and I was able to run mongo using docker-compose again.
version: '3.9'
services:
mongo:
image: mongo
command: --repair
This is how we pass the --repair in docker-compose.

docker backup and restore mongodb

Create data only container:
docker create -v /mongodb_data --name mongodb_data mongo
Create monogdb container:
docker run --volumes-from mongodb_data --name mongo_db --restart always -d mongo
Then to use mongodb in my own container I use the link command:
--link mongo_db:mongo
So everything works fine. Now I want to backup the mongodb according to the docs: http://docs.docker.com/userguide/dockervolumes/#backup-restore-or-migrate-data-volumes and this command:
docker run --volumes-from mongodb_data -v $(pwd):/backup busybox tar cvf /backup/backup.tar /mongodb_data
However the created tar file has just an empty /mongodb_data folder. The folder contains not a single file.
Any ideas whats wrong? I am using docker 1.7 on ubuntu 14.04 LTS.
The problem is your data only container. You make your volume with path /mongodb_data which doesn't store any mongo db data. By default, the mongo db storage path is /data/db (according to this #Where to Store Data Section)
As the result, your mongodb data is not saved in your data only container. So here is a workaround:
copy /data/db to /mongodb_data in your mongodb container docker exec -it mongo_db bash then cp -r /data/db/* /mongodb_data/
make a backup by following the doc you mentioned
build a new data only container and load the backup
remove current mongo_db container and recreate a new one with the new data only container
OR, you can modify your mongodb config, to change the default directory to /mongodb_data once you copied all data from /data/db to /mongodb_data. You may find this useful Changing MongoDB data store directory
You can use this image that provides docker container for many jobs ( import, export , dump )

Docker: Mongo exits on run

Using:
https://registry.hub.docker.com/_/mongo/
I did this to pull in all tags:
docker pull mongo
Then, when I try to run it with
docker run -v /data:/data --name mongodb -p 4000:27017 mongo:2.6.6
The status shows
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5959d3f79243 mongo:2.6.6 "/entrypoint.sh mong 4 seconds ago Exited (1) 3 seconds ago mongodb
Logs show:
numactl: This system does not support NUMA policy
How do I keep mongo running while using docker? I am using Docker 1.4.1 on OSX (boot2docker).
Indeed, the boot2docker VM doesn't support NUMA, and the current Dockerfile executes mongod through numactl. A possible workaround:
$ docker run -v /data:/data --name mongodb -p 4000:27017 --entrypoint=mongod mongo:2.6.6
This uses --entrypoint to override the image defined ENTRYPOINT and execute mongod directly.