How to reference embedded Docker resource files using file path URL - docker

I have created a Docker image and embedded some static resource files within it, using the following command in the Dockerfile:
COPY resources /resources
I have a java web application running within the Docker container which requires access to these static files. File paths must be provided using a URL, E.g.:
file://c:/resources/myresourcefile.css
I am able to construct the URL programmatically but am unsure if embedded files can be referenced this way. Any guidance would be appreciated!
Note: I am specifically using the pdfreactor web service, and my Dockerfile is thus:
FROM realobjects/pdfreactor:9.1
EXPOSE 9423
COPY resources /resources
I am trying to set the "BaseURL" of the PDFreactor wrapper client to the root resource folder.

If it’s a Linux container, and the requestor is specifically your Java process running inside the container, then file:///resources will point at the directory you added (a subdirectory of the image root directory). If the URL is being served to something outside the container (like an HTML link or image reference) then a file: URL won’t be able to access files inside the container; you’d have to come up with something else to serve up the files and provide an HTTP URL to them.

As per the official doc on https://www.pdfreactor.com/product/doc_html/index.html#resourceLoading
It is also possible to specify file URLs:
Java: config.setBaseURL("file:///directory/")
PHP: $config["baseURL"] = "file:///directory/";
.NET: config.BaseURL = "file:///directory/";
CLI: --baseURL "file:///directory/"

Related

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 make dockerfile and .conf file are not accessible direct through url

How to make dockerfile and .conf file are not accessible direct
through url
Look this url
http://localhost/dockerfile
http://localhost/nginx.conf
If I Open this url this files are downloadable which should be not. How can i prevent this?
You can use a .dockerignore file to avoid sending files to your container. See docs here for things like your Dockerfile.
To exclude config files and others that you need in your container from being served, you would do this via your web server config. E.g., for nginx:
location ~ (\.conf$|Dockerfile) {
return 403;
}
As a best practice, you might want to structure your project so the Dockerfile root isn't the same as your web root, similar as to how would (should) structure a non-Dockerized app.

ADD command with network path in Windows Containers Dockerfiles

I'm creating some Windows Container images that I need but the source file I want to ADD are in a network share \\myserver\myshare\here.
I've tried in any possible way but I always get the message error The system cannot find the path specified.
Is it because I have not yet found the right way to set it or is it that it is just not possible?
From the Docker site:
Multiple resource may be specified but if they are files or directories then they must be relative to the source directory that is being built (the context of the build).
Is that why I can't accomplish what I need?
Full error message: GetFileAttributesEx \\myserver\myshare\here\: The system cannot find the path specified.
Whatever you ADD or COPY must be in the docker build context.
When you do this:
docker build .
That directory param (the . in the example) is the context that is copied and sent to the Docker daemon. Then the docker daemon use those files to COPY or ADD. It won't use any file that is not in that context.
That is the issue that you are experiencing. I'm not sure how you can solve it anything than copying the files from \\myserver to your build directory.
ADD is capable of download files by providing an URL (should investigate if it supports Windows' shares)

Resources