Run command on jenkins docker container to copy file to host - docker

Is there a way to copy file from docker container to the host by running the command on the container itself?
I know that I can use "volume" but it will not work - I want to copy files from container to arbitrary places on the host.
Only SCP file vis SSH?

From: Copying files from Docker container to host you can run:
docker cp <containerId>:/file/path/within/container /host/path/target

Related

How containerd copy a file from host to a running container?

I find that I can use ctr snapshot mount to copy a file from a container to a host.
But how can I copy a file from a host to the container using containerd?
I used golang to write some code to start a container, but I can't find any documentation about copying host files to a running container.
As of now there is no provision as such with either with ctr or crictl cli to copy a host file to a running container as we have with docker cli (eg: docker cp).
Though there is a project under containerd known as nerdctl which is trying to emulate the same.
nerdctl is a Docker-compatible CLI for containerd.
Link for reference: Nerctl cp command

How do access file located in host from Jenkins installed in a local docker container

How can I connect my Jenkins in docker to my hosts local filesystem. I need to build a code snippet in my local filesystem from the Jenkins shell
It's not quite clear what do you mean by 'connect' but you can copy your code from docker via:
docker cp "$(docker ps -aqf name=your_image):/opt/path/to/your/file" .
ReBuild your docker image with the following (mind meta there):
FROM jenkins_image
COPY /your_project_dir/ /your_build_dir_injenkins/

How to get an artifact generated from a container?

I have a Dockerfile dedicated to run my unit test, but i am not sure how i am supposed to get the coverage directory it generates (inside the container).
I would like to be able to get it as an artifact to be able to analyze it, but is it possible since it is generated from the container?
Use docker cp command
If you want to copy the /tmp/foo directory from a container to the existing /tmp directory on your host. If you run docker cp in your ~ (home) directory on the local host:
$ docker cp container_name:tmp/foo /tmp
Docker creates a /tmp/foo directory on your host.
If your container dies after executing you can map a volume from you host to the container, in this way you will have your data in your host after the container dies.
VOLUME ["/home/data"]
This will map /home/data in your machine with /home/data in your container, adjust at will.
More info
https://docs.docker.com/engine/reference/builder/#notes-about-specifying-volumes

Docker - access existing container files on host machine

I have a docker container which has some data in let's say /opt/files. File A and B. How can I start that container and access these files on my host machine?
I'm using Docker for Windows (Hyper-V). When i start the container with:
docker run -it -v C:/tmp:/opt/files myImage
I see an empty folder on my windows machine and inside of the container. Any new files I create there are of course reflected on both sides but how can I access files that are already in the container (e.g. because they're added in the Dockerfile)?
You can't share from inside container to host. There are two ways to do it
Copy the files from container
docker cp <containerid>:<file_path_inside_container> localpath
Share a folder other than the one where files will be generated
docker run -it -v C:/tmp:/opt/files_temp myImage
Then you get inside the container copy files from /opt/files to /opt/files_temp
Once your container is started, you can copy files inside it to your host.
Use docker cp for this (https://docs.docker.com/engine/reference/commandline/cp/).
Example : docker cp CONTAINER:SRC_PATH DEST_PATH|-

How could I get files from a docker container running the official etcd image if there is no shell?

I have a docker container that is running the etcd docker image by CoreOS which can be found here: https://quay.io/repository/coreos/etcd. What I want to do is copy all the files that are saved in etcd's data directory locally. I tried to connect to the container using docker exec -it etcd /bin/sh but it seems like there is no shell (/bin/bash, /bin/sh) on there or at least it can't be found on the $PATH variable. How can I either get onto the image or get all the data files inside of etcd copied locally?
You can export the contents of an image easily:
docker export <CONTAINER ID> > /some_file.tar
Ideally you should use volumes so that all your data is stored outside the container. Then you can access those files like any other file.
Docker has the cp command for copying files between container and host:
docker cp <id>:/container/source /host/destination
You specify the container ID or name in the source, and you can flip the command round to copy from your host into the container:
docker cp /host/source <id>:/container/destination

Resources