Docker cp command does not delete unshared files when replacing directory in container

88 Views Asked by At

I am running an analysis in a Docker container that emulates the CERN remote server lxplus. After making changes within the container, I need to copy a specific file from the container to a local Git repository, then push it to a remote GitLab repository. I also need to be able to pull changes from the remote repository, copy them to the local repository, and then copy them to the Docker container, replacing the existing version of the directory and its contents. I have automated this process using separate bash scripts for push and pull.

The problem I am experiencing is that the docker cp command is not fully replacing the container directory. If there are files with shared names in both the repositories and the container, they get replaced as expected. However, if there are any extra unshared files, they are not deleted. I need any unshared files to be deleted as well. It is essential for my workflow that I copy only specific files instead of the entire Docker image to GitLab.

The bash script I have for the Git pull automation is:

# Add file to git repository
cd ~/Documents/Repos/validation-source-file
git pull $GITLAB_REPO

# Change back to home directory
cd ~

# Save file from container to local directory
docker cp $LOCAL_DIR $CONTAINER_NAME:$FILE_PATH 

I have defined the file paths and repository information earlier in the script, and I have tested the git pull command, which is working as intended. However, when I try to update the files in the Docker container with the docker cp command, the script fails to delete any files that have been removed from the remote repository.

For example, if I add a file called test.txt to the remote repository and pull it into the Docker container, the file is copied over to the correct location in the container. If I then remove test.txt from the remote repository and pull the changes into the local repository, the docker cp command in the script fails to remove test.txt from the Docker container directory.

I have tried changing the depth of the directory in the FILE_PATH and LOCAL_DIR variables, but the issue persists. How can I modify the script to ensure that files are deleted from the Docker container when they are removed from the remote repository?

0

There are 0 best solutions below