Move jar files within one docker container - docker

I am using existing karaf image and unarchiving it while building it with other configurations.(dockerfile)
Now i realized that karaf123.jar file which is in lib folder, it should be in /lib/boot folder.
I tried using COPY but its copying from host, but in my scenario it should just move file from one folder to other within image.
I found following link but no option to copy from container1 to container1
https://medium.com/#gchudnov/copying-data-between-docker-containers-26890935da3f

Assuming you're using a Linux container just add this to your Dockerfile:
RUN mv /lib/karaf123.jar /lib/boot/karaf123.jar

Related

Copy folders from one directory to another within Docker container

Can anyone tell me how to move a folder from one Directory to another within a Docker container? I have a folder in the root directory of my Docker container:
root/folder/folder1
I've created a folder called Source at the same container level as root. In my Dockerfile I'm trying to copy folder1 into Source as below:
ADD root/folder/folder1/ /Source/
But I get an error saying that root/folder/folder1/ isn't a file or directory. I'm new to Docker, can anyone assist?
The ADD instruction copy from outside the image into the image. The source should be in your contexte directory or, for ADD, an URL and Docker will download it (and extract it if it's an archive).
It's usually a good practice to use COPY instead of ADD most of the time.
In your case, as you want to copy a directory inside your docker image, you should execute a shell command for that: RUN cp -r /root/folder/folder1 /Source (or maybe create a link if you don't need to duplicate the content).
For more information about ADD vs COPY, you can read the Dockerfile Best Practices from docker

How synchronize the container folder to local empty folder when container running

I would like run a container with links my container folder to my local folder. I can create a link the local folder to container folder, if the container folder isn't empty, he's overwritten.
But I would like a reverse link : if I run a image with docker run the folder in container writes in my local folder his data, and if I modify the files in local, the changments are writte in container.
I ask that because I download a framework online in my Dockerfile, and if I start a new project with this image, I would like the framework are directly download from container without having to download manually before.
It's possible ?
Thanks for your responses.
you can use volumes that will be mapped to your container:
docker run -it -v <my_local_folder>:<my_container_folder>

Docker, copy files in production and use volume in development

I'm new to using docker for development but wanted to try it in my latest project and have ran into a couple of questions.
I have a scenario where I want to link the current project directory as a volume to a running docker container in development mode, so that file changes can be done locally without restarting the container each time. To do this, I have the following comand:
docker run --name app_instance -p 3100:80 -v $(pwd):/app appimage
In contrast, in production I want to copy files from the current project directory.
E.G in the docker file have ADD . /app (With a .dockerignore file to ignore certain folders). Also, I would like to mount a volume for persistent storage. For this scenario, I have the following command :
docker run --name app_instance -p 80:80 -v ./filestore:/app/filestore appimage
My problem is that with only one dockerfile, for the development command a volume will be mounted at /app and also files copied with ADD . /app. I haven't tested what happens in this scenario, but I am assuming it is incorrect to have both for the same destination.
My question is, what is the best practice to handle such a situation?
Solutions I have thought of:
Mount project folder to different path than /app during development and ignore the /app directory created in the container by the dockerfile
Have two docker files, one that copies the current project and one that does not.
My problem is that with only one dockerfile, for the development command a volume will be mounted at /app and also files copied with ADD . /app. I haven't tested what happens in this scenario, but I am assuming it is incorrect to have both for the same destination.
For this scenario, it will do as follows:
a) Add your code in host server to app folder in container when docker build.
b) Mount your local app to the folder in the container when docker run, here will always your latest develop code.
But it will override the contents which you added in dockerfile, so this could meet your requirements. You should try it, no need for any complex solution.

Docker add files to VOLUME

I have a Dockerfile which copies some files into the container and after that creates a VOLUME.
...
ADD src/ /var/www/html/
VOLUME /var/www/html/files
...
In the src folder is an files folder and in this files folder are some files I need to have copied to the VOLUME the first time the container gets started.
I thought the first time the container gets created it uses the content of the original dir specified in the volume but this is not the case.
So how can I get the files into this folder?
Do I need to create an extra folder and copy it with a runscript (I hope not)?
Whatever you put in your Dockerfile is just evaluated at build time (and not when you are creating a new container).
If you want to make file from the host available in your container use a data volume:
docker run -v /host_dir:/container_dir ...
In case you just want to copy files from the host to a container as a one-off operation you can use:
docker cp /host_dir mycontainer:/container_dir
The issue is with your ADD statement. Also you might not understand how volumes are accessed. Compare your efforts with the demo below:
FROM alpine #, or your favorite tiny image
ADD src/files /var/www/html/files
VOLUME /var/www/html/files
Build an image called 'dataimg':
docker build -t dataimg .
Use the dataimg image to create a data container named 'datacon':
docker run --name datacon dataimg /bin/cat
Mount the volume from datacon in your nginx container:
docker run --volumes-from datacon nginx ls -la /var/www/html/files
And you'll see the listing of /var/www/html/files reflects the contents of src/files

How to run a simple main method and copy the file it generated using docker

I have a simple main method that outputs a folder with some files.
How do i copy the file to my host to view the output files. Tried using volume but it didnt work out. Here is the docker file
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD main.jar main.jar
ENTRYPOINT ["java","-jar","main.jar"]
//generated folder 'xxxx' with some files.
Thanks
The way to make files available outside the container is with a volume mapping. To make the files available in your current directory you would do docker run -v $PWD:/tmp .... yourimage

Resources