build docker image (centos) with volume with data for testing (postgresql) - linux

I want to make image with persisted data to other developers can run this container and use it.
I use this https://hub.docker.com/r/centos/postgresql-96-centos7/ and do next step:
1) Run it with command
docker run -d -e POSTGRESQL_USER=svcg -e POSTGRESQL_PASSWORD=svcg1 -e POSTGRESQL_DATABASE=bcs -p 5432:5432 --expose=5432 --name=postgres --net=docker-net -v psql:/var/lib/pgsql/data centos/postgresql-96-centos7
The result is a running container "postgres" and volume "psql".
2) Restore data from the dumpName.xml
java -jar database_backup_starter.jar -u svcg -p svcg1 -url jdbc:postgresql://192.168.99.100:5432/bcs -m restore -path database_dump
The volume "psql" contain the testing data.
Is it possible to do it in one step ? For instance to create image from the volume or from the docker with volume with data? How to do it?

Since this image declares a VOLUME for the PostgreSQL data, you cannot make a derived image from this with prepopulated data in a volume, either using docker build or docker commit. It won't work (see the note "changing the volume within the Dockerfile").
The standard way to load data into a newly created database container seems to be to put a SQL-syntax database dump in the initialization directory when you start the container for the first time. In this image, it looks like you'd have to wrap the database dump in a shell script and put it in an /opt/app-root/src/postgresql-init/ directory. For the stock postgres image you'd put a *.sql file in /docker-entrypoint-initdb.d/.

Related

Docker mount files to my local system while running the container

I am using an image academind/node-example-1 which is a simple node image. You can check it https://hub.docker.com/r/academind/node-example-1 here. What I want while I run the image I want to get the same folder & file structure that is there in the Image. I know that can be done via volume. When I use like
docker run -d --rm --name node-test -p 5000:80 academind/node-example-1
Everything is proper But I want to get the codebase while running, so I tried like
docker run -d --rm --name node-test -p 5000:80 -v /Users/souravbanerjee/Documents/node-docker:node-example-1 academind/node-example-1
Here node-docker is my local folder where I expect the code to be. It runs but not getting the files in the local machine, I'm in doubt here where the source_path:destination_path. Please correct me to please tell me where I'm wrong, or what to do, or my entire thinking is going in the wrong direction or not.
Thanks.
If you read the official doc, you'll see that the first part of the : should be the path somewhere in the host machine (which you're doing), while the later part should match the path "inside" the container (instead you're using image name). Assuming /app being the path (I've taken that course by myself and this is the path AFAIR), it should be:
docker run -d --rm --name node-test -p 5000:80 -v /Users/souravbanerjee/Documents/node-docker:/app academind/node-example-1
I think the correct syntax is to enter the drive information in the volume mapping.
Eg. Users/xxxx/:some/drive/location
However, that would map your empty drive at xxxx over the top of 'location' folder. Thus deleting the existing files in the container.
If you are interested in seeing the contents of the files in the container, you should consider using 'Docker CP' command.
People often use volume mounts to push data (i.e. persistent database files) into a container.
Alternatively, writing log files to the volume mounted location inside the application container. Then those files are then reflected on your local drive
You can copy the files to the current host directory using the command
docker cp node-test:/app .
when the container is running

The data is getting lost whenever I restart the docker/elk image

I'm using docker/elk image to display my data in kibana dashboard (Version 6.6.0) and It works pretty good. I started the service like using below command.
Docker Image git repo:
https://github.com/caas/docker-elk
Command:
sudo docker-compose up --detach
Expecting that it will run background, and did as expected. After two days the server up and running the and third day the kibana alone getting stopped. and Used below command to make it up and running.
sudo docker run -d <Docer_image_name>
It's up and running when I use docker ps command. But when I tried to hit the kibana server in chrome browser it says not reachable.
So I just used to below command to restart the service.
sudo docker-compose down
After that I can see kibana server in chrome browser which is up and running but I do see all my data is lost.
I used below URL in jenkins to collect the data.
`http://hostname:9200/ecdpipe_builds/extern`al
Any idea how can I resolve this issue?
I did not see the persistent storage configuration the image you mentioned in their GitHub docker-compose file.
This is common to lost data in case of docker container if you did not provide persistent storage configuration. so docker-compose down may cause to lost you data if there is no persistent configuration docker-compose file.
Persisting log data
In order to keep log data across container restarts, this image mounts
/var/lib/elasticsearch — which is the directory that Elasticsearch
stores its data in — as a volume.
You may however want to use a dedicated data volume to persist this
log data, for instance to facilitate back-up and restore operations.
One way to do this is to mount a Docker named volume using docker's -v
option, as in:
$ sudo docker run -p 5601:5601 -p 9200:9200 -p 5044:5044 \
-v elk-data:/var/lib/elasticsearch --name elk sebp/elk
This command mounts the named volume elk-data to
/var/lib/elasticsearch (and automatically creates the volume if it
doesn't exist; you could also pre-create it manually using docker
volume create elk-data).
So you can set these paths in your docker-compose file accordingly. Here is the link that you can check elk-docker-persisting-log-data
Use docker volume or file location as persistant space

docker volume : data lost when container is removed or stopped and then restarted

I'm have mounted 2 containers :
Mongo : as the database -> the volume is mounted on a specified path
docker run --hostname raw-data-mongo --name=raw-data-mongo
--network=our-network -p 27017:27017 -v data/db/raw-data-db:/data/db -d mongo
Nodejs: save some data to mongodb
docker run -d -p 80:3001 --name=raw-data-container
--network=our-network -v /data/db/raw-data-db:/data/db raw-data-api
But any time I save data (using my node app container, connecting to mongo container) and then remove my node container and restart, I can't retrieve the data I saved.
How should I do to mount a volume that is independent of the node, and if possible independent from any container?
I wish I could remove and restart containers and still be able to retrieve my data
You can run use -v to specify the volume against which you want your docker deployment to save your data. Two things you should keep in mind is, one is that the docker that you are running should be exposing the -v and using this to store its data (mongodb does this, and so does most projects) and another the persistence volume path passed with -v should exist and the docker process should have write permissions on it.
Hope this helps !!
Please try as following
Step 1: Make the directory
$HOME/data/db/raw-data-db
Step 2: Then assign the volume
docker run --hostname raw-data-mongo --name=raw-data-mongo
--network=our-network -p 27017:27017 -v $HOME/data/db/raw-data-db:/data/db -d mongo
In this way, your data will be persisted for mongodb in the volume. For node.js use different volume.

File permissions for mounted volumes in image processing pipelines?

We're using docker to containerize some image processing pipelines to make sharing them with collaborators easier.
The current method we're using is to mount an "inputs" directory (which contains an image. i.e. a single jpg) and an "outputs" directory (which contains the processed data. i.e. maybe a segmentation of the input image). The problem we're having is we run docker with sudo, and after the processing is complete, the files in the outputs directory have root permissions.
Is there a standard or preferred way to set the files in mounted volumes to have the permissions of the calling user?
Perhaps you can use the --user flag in docker run
e.g.
docker run --user $UID [other flags...] image [cmd]
Alternatively the following might work (untested)
In Dockerfile
ENTRYPOINT "su $USERID -c"
Followed by: `
docker run -e USERID=$UID [other flags...] image [cmd]
Try setting the user in your Dockerfile so that when the container is started it uses 'tomcat' for example.
# example
USER tomcat

Stop VM with MongoDB docker image without losing data

I have installed the official MongoDB docker image in a VM on AWS EC2, and the database has already data on it. If I stop the VM (to save expenses overnight), will I lose all the data contained in the database? How can I make it persistent in those scenarios?
There are multiple options to achieve this but the 2 most common ways are:
Create a directory on your host to mount the data
Create a docker
volume to mount the data
1) Create a data directory on a suitable volume on your host system, e.g. /my/own/datadir. Start your mongo container like this:
$ docker run --name some-mongo -v /my/own/datadir:/data/db -d mongo:tag
The -v /my/own/datadir:/data/db part of the command mounts the /my/own/datadir directory from the underlying host system as /data/db inside the container, where MongoDB by default will write its data files.
Note that users on host systems with SELinux enabled may see issues with this. The current workaround is to assign the relevant SELinux policy type to the new data directory so that the container will be allowed to access it:
$ chcon -Rt svirt_sandbox_file_t /my/own/datadir
The source of this is the official documentation of the image.
2) Another possibility is to use a docker volume.
$ docker volume create my-volume
This will create a docker volume in the folder /var/lib/docker/volumes/my-volume. Now you can start your container with:
docker run --name some-mongo -v my-volume:/data/db -d mongo:tag
All the data will be stored in the my-volume so in the folder /var/lib/docker/my-volume. So even when you delete your container and create a new mongo container linked with this volume your data will be loaded into the new container.
You can also use the --restart=always option when you perform your initial docker run command. This mean that your container automatically will restart after a reboot of your VM. When you've persisted your data too there will be no difference between your DB before or after the reboot.

Resources