Docker-compose mount folder - docker

Hi I have multiple folders that I wish to mount. this is the scenario:
On my host:
path\to\mount\HostFolder\folder_*
On my container:
\path\to\mount\ContainerFolder\
* is a number
I want to mount all folder_* to ContainerFolder\
ContainerFolder already have some folders inside so I must not mount HostFolder to ContainerFolder to save them.
I wish to use regex but volume keyword doesn't seems to support it, any suggestions how to manage that?
My only idea right now is script to write for me the docker-compose.

My suggestion would be to seriously reconsider the assumption that you need the current directory structure. (Of course, this may be impossible, I know.)
If you can change the directory structure at all, you can try restructuring the folders like this:
ContainerFolder
folders
folder_1
folder_2
In this way, you can mount the HostFolder into a subdirectory of ContainerFolder without overwriting the contents of ContainerFolder itself.

Related

What is the exact role of tmp directory created by docker during its startup?

During initialization of the daemon, docker creates a tmp folder inside its root directory.
code reference
What is the exact purpose of this tmp directory, does that directory is only used to store temporary files related to docker, if yes does it only used in docker startup? Do containers also have access to that directory and use it to store their temporary files? Does it also cache some data for the next reboot of the host machine?
The prime objective to get this information is to estimate the size of the tmp directory and create a strategy for storing it.
I tried looking for the information in official docs, but I wasn't able to find any.
There is a question here regarding /tmp folder strategy, but this will only help me once I know what exactly tmp is used for.

Docker File Location for Asp.Net core

I am running Docker in Windows, using Linux containers.
I have an asp.net core hello-world app that writes a text file:
var path = Path.Combine(
Directory.GetCurrentDirectory(), "text.txt");
System.IO.File.WriteAllText(path, "text");
Directory.GetCurrentDirectory() comes back as "/app"
In my docker-compose I map /app to /usr/xxxxx
volumes:
- /app:/usr/xxxxx
My question is: Where on my Windows file system is the /usr/xxxxx ? I want to back it up so that it stays after containers are removed.
Volumes (actually bind-mounts) use the format host-path:container-path.
It looks like you put it in the wrong order in the docker-compose file.
Also, as there is no folder called /usr/xxx on windows, just use a path that exists instead.
For example, you can create a directory called "backup" inside the directory with the docker-compose file, and then modify the docker-compose file like so
volumes:
- "./backup:/app"
In the standard Dockerfile that Visual Studio generates, your application dll's are copied into /app. Therefore, it might be a bad choice to use /app. I'm actually not sure what happens if the bind-mount directory already exists with different data inside the container. But you could just write the file to another directory and use that instead.

what is the absolute path of a folder in docker?

I want to run an already existing app on docker. The app interacts with some files on a folder. So i am trying to hook up a volume.
Here is the docker-compose:
test:
container_name: test
volumes:
- C:\test\:\test\
build: .
When i hook into the docker image i can see that the folder is created on the root folder. Now i need to write the correct path to that folder into the application settings.
Before it was something like this:
"test": {
"Path": "C:\\test\\"
}
But i dont know how to get the absolute path of my folder from inside docker, so my app can understand where to search for it.
Thank you
EDIT: it looks like the problem was on my side: the way i defined the volumes created a folder with the name "\test\" ... doing C:\test:/test/ instead did the trick
If you’re building your own Docker image, you control the filesystem layout entirely. For Linux-based images it’s common to follow the FHS standard (I think the standard MySQL image stores its data in /var/lib/mysql) but it’s also common enough to just store data in subdirectories of the root directory (/data or /config or what not).
If you have a setup like this, your image should pick a path. If the only thing in the configuration is the location of that directory, it’s fine to hard-code it in the image. However you document your image (even if it’s just a standard docker-compose.yml file) mention that you have this fixed path; it doesn’t need to match the host path (if any) on any particular system.

Running container can't find the file that it has created to /home/user/ directory

I hope you are having a great day!
I'm new to docker. I think my problem is related to docker's directory tree
My app writes to a file to /home/user directory and then after some time reads that file again.
I got this error from my app.
[error] a.a.OneForOneStrategy - /home/user/bkjw_eqvfohygvkaxoxc-small.jpg
java.nio.file.NoSuchFileException: /home/user/bkjw_eqvfohygvkaxoxc-small.jpg
My dockerized app is unable to create the file and read. I'm thinking that the Docker considers the directory /home/user/ as a absolute directory of host.
I thought that the container would write to /home/user directory within the container's directory tree.
So the question is :
How can I specify the path to write the file inside the containers directory tree?
Your understanding about the directory tree is correct. Application running inside a docker container would write to /home/user/ in the container's directory tree.
Your issue seems to be with permissions, your java application probably doesn't have the rights to write to /home/user/ within the container. Either you should change the ownership/rights of the directory you're wanting to write in, or a simple solution I did in such case was to create the directory I wanted to write in, within the java code.
like:
// Create volume directories explicitly so that they are created with correct owner
Files.createDirectories(Paths.get(dirPath));
You can set dirPath String to something like /home/user/mydir IF your requirement is not to write in /home/user/ specifically.

How to map all containing folders using volume path docker

I'm new to Docker so let me know whether I should be doing something completely different, but here goes..
I'd like to create a volume within an nginx container which maps all sites-enabled configuration files to the location on the container. The catch is, these files are scattered across multiple folders which can be named at random. But wait! I can tell you for certain that the .conf file will always be in a specific folder only 1 level deep (because that's how I prefer the layout to be). Here's a basic layout explaining what I mean:
- images/
- nginx/
- docker-compose.yml
- sites/
- site_a/
- vhost/
site_a.conf
- site_b/
- vhost/
site_b.conf
I have tried to do this when declaring the volumes for nginx:
- ../sites/*/vhost/:/etc/nginx/sites-enabled
Yet I get the following error:
ERROR: for nginx Cannot start service nginx: mkdir /c/Users/Yates/Documents/docker/sites/*: protocol error
I kind of get the error, yet I can't find anything telling me how accomplish what I eventually want. Any help would be much appreciated.
Pretty sure you can't do this.
What you could do is symlink copy all the .conf files into a single directory on the host and use that as your volume mount point. Or just mount the sites/ root directory.
This is a conceptional question and your concept is very fragile and will lead to issues most probably.
What you are doing wrong is:
the configuration for the httpd server is kept inside the sites data/configuration structure or at least its nested inside
You are trying to configure a dynamic amount of 'services' sites using a static+nested folder. You are mixing up "configuration" and data. Try to put your configuration into something like consul/etcd/zookeeper and generate configuration usint tiller ( confd/consul-template)
If you to force it, you can:
Way a)
Mount the volume holding the sites folder in nginx, read only ( however this volume is called )
Then you runn an entrypoint script in the nginx container, some find expression to traverse your "sites" folder, find any site, use the vhost folder and create a symlink from there to /etc/nginx/sites-enabled
You need to also think about deletions, so pick a good namespaces for the symlinks, like mysite.automated.conf and before the entrypoint creates the symlinks, you run rm /etc/nginx/sites-enabled/*automated.conf to flush the potentially outdated/deleted ones. You can do it the update way also, but thats for more complicated..
way b)
If you can, change the structure of sites in general, extrac all "vhosts" into one folder, lets say you will have
sites/vhosts/*.sites.conf
sites/data/siteA/
sites/data/siteA/
and then mount the volume read-only on nginx and symlink sites/vhosts to /etc/nginx/site-enabled ( the folder itself )

Resources