Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically removes files/folder resides in docker container

Tags:

docker

I am currently exploring on how we can remove the file/folder resides inside docker container programmatically . I know we can copy files from container to host using docker cp. However, I am looking for something like docker mv or docker rm which allows me to move or remove files/folders inside docker.

The scenario is, We are writing the automated test cases in Java and for one case we need to copy the log file from the server's log folder to assert the log written by the test case. We are using the docker cp to copy the log files. However it contains the log of old test cases as well. So I was thinking if I can remove the log files before executing my test case. It make sure the log I have copied is written by my test case only. Is there any other way around?

like image 659
Jimit Joshi Avatar asked Jul 26 '16 09:07

Jimit Joshi


People also ask

How do I remove a directory in docker?

If permission is denied while trying to delete directory try to delete it with root user. Container must be started in order to run this command. "-u root" was exactly what I needed.

Where are files in docker container stored?

The docker images, they are stored inside the docker directory: /var/lib/docker/ images are stored there.


2 Answers

You can use below command to remove the files from program running on host

docker exec <container> rm -rf <YourFile> 

However if old files exist because the container were never removed, then general practice is to remove the container once the all test suite execution is complete,

New job should create the new container.

like image 130
sudhir sharma Avatar answered Sep 22 '22 19:09

sudhir sharma


In the docker file definition you can use

RUN rm [folder-path]

However anything added using ADD or COPY will still increase the size of your image.

EDIT: For accessing a running container from an external program running on your host try this.

  1. Mount a host folder as a volume when starting the instance.
  2. Run a script or program on the host to delete desired folders on the host which will affect the container file system as well.
like image 30
Santanu Dey Avatar answered Sep 18 '22 19:09

Santanu Dey