Copy files from host to Docker container without using Docker command - java

Is there any way to copy files from the host to Docker container without using Docker cp command?
I am developing a web project using Java. In that I need to download a file from the web and copy that file to my Docker container.
How can I do it without using Docker cp command?

Take a look at this link. Let me know if it works for you http://docs.docker.oeynet.com/machine/reference/scp/#example

Related

How to load custom xml files during docker run?

I have a two custom xml property files that are environment specific used in my spring boot project. Is it possible to use mount or volume tag to get the files from a location specified during docker run? The xml files are required to successfully connect to a db server.
Also if I specify an env-file command in the docker run can i put my sh files in any location on the docker server and specify the path there in the run command?
Yes, you can do that by mounting a volume. It will swap your in-container location with chosen server location. Inside container there will be no difference between this shared location and any other. Use flag -v "SERVER_LOCATION:CONTAINER_LOCATION":
docker run -v /etc/xmlsFolder:/etc/appConfig/destinationFolder your_image
Yes, you can specify run command in script anywhere on server.

how to parse a file from a java docker container application

I have a java application which needs to parse a data set file. I have containerized the app using docker but the container does not see my file in my host.
So, I need to somehow include it inside the docker container or make it available through a docker volume which is mapped into my container.
inside the java app I have
String dataSet = "/usr/DataSet/TempData.txt"; // inside the container
File textfile = new File(dataSet);
Scanner sc = new Scanner(textfile);
and my file is stored in a directory (/home/myusername/Desktop/DataSet) in my host.
I compile and build my docker image using spotify plugin in maven.
After I build the image, I run a container and map a volume that contains my TempData.txt:
sudo docker run -it -v /home/myUserName/Desktop/DataSet:/usr/DataSet --name myapp myImageName bash
But I get java.io.FileNotFoundException error because the file is not available in my container ..
what is the best way to make a file available to my java dockerized app to be able to read and parse it ?
Thanks for your inputs!
The problem was solved when I put my file in my home directory. and then mapped it into my container ...

Read local file system with Java from Docker container

I have a java app running on a docker container on mac osx. I want to access a file on a certain directory within the local file system using Paths.get('/Users/username/folder')
I am getting errors because docker is reading from its vm directory. How will I within a java app access the local file system while running the app in docker?
You can share the folder in the host machine with the container:
docker run -v your/host/folder:/your/container/folder ....
And then you can use Paths.get('/your/container/folder')
If you map the host folder in the same folder inside the container then you don't have to take care about it in the java code docker run -v your/folder:/your/folder ..... You also have to keep in mind issues with permissions...
Official documentation

How to run Docker container from Java project?

How to run a docker container from java code? I'm trying to make a SaaS using docker, once the user logs in, I should start a memcached container from java code, this solution doesn't work:
Process p = Runtime.getRuntime().exec("docker images");
Docker cmds run usually on git bash, not on cmd.
PS: I'm using docker on windows.
You can do it using https://github.com/docker-java/docker-java . It allows you to build a custom image and run it from java
I assume you are using Docker Toolbox for Windows.
The docker command does not take a capital D. Maybe try with
Process p = Runtime.getRuntime().exec("docker images");
but as you are probably running this code on Windows, that might work anyway.
Another thing to consider is the value of the DOCKER_HOST environment variable which must be set accordingly to instruct the docker client how to communicate with the docker engine.
In your case the docker client is run on Windows while the docker engine runs inside a virtual machine living in VirtualBox.
The shell provided by Runtime.getRuntime().exec() won't have the DOCKER_HOST environment variable set.
Another way is to use the --host or -H docker client option to specify how to connect to your docker engine:
Process p = Runtime.getRuntime().exec("docker --host=tcp://<some IP>:2376 images");
I'm sorry guys, when I open a new shell (client), I have to configure it in order to know how to connect to the docker daemon that is running in the virtualbox. I had to run cmds that set the shell environment, because the quickstart terminal does it automatically. So I had to run the following and then paste the output back into my cmd shell:
docker-machine env --shell cmd default
Now it works perfectly.
Update (thanks to #thaJeztah) : It's better to use Java libraries to connect directly to the docker daemon.
Link to API https://docs.docker.com/engine/reference/api/remote_api_client_libraries/

Spring Boot External Configuration when using Docker

I'm developing a Spring Boot application which I'd like to deploy with Docker.
The trouble I'm having is we need to store the properties file on the server, similar to how Tomcat allows you to put the properties file in /lib.
How would I go about getting Spring Boot to use this file when running inside Docker?
Docker provides a way to do this using Volumes:
In addition to creating a volume using the -v flag you can also mount a directory from your own host into a container.
$ sudo docker run -d -P --name yourapp -v /lib:/lib yourcontainer/name
So in your containerized app, you would just look in /lib (or wherever you find convenient to mount it), and when you book the container, you specify the host directory you want mounted.
Another option I've used is to create a container with just the configuration (use busybox or something small) and then export a directory from within that as a volume that you share in other containers. This does set up a dependency between containers that you have to manage, but it gives you the benefit of being able to version your configuration and not have to have it just sitting on the host file system.
Both of these strategies are discussed at the link above.
You can also override application.properties file directly:
docker run -v custom.properties:/data/application.properties spring-boot-app

Categories

Resources