How to mount a container's directory in a docker run command - docker

I am trying to run a "build container" which simply builds an output file from source code and then exits. So I have to bind mount the source code directory when running this build container so that it can build the output file.
The complicated part is that the source code directory is inside a separate container.
One solution is to manually copy the source code directory from the separate container to the docker host. Then I can give the path to the source code directory on the docker host, like docker run --mount type=bind,src=/path/to/sourcecode,dst=/local build_container.
The separate container has access to the docker socket so I can issue docker commands directly from it. I would like to be able to run the "build container" from this separate container so that the docker daemon magically knows that the source directory is on this separate container.
Is there a way to do that without having to manually copy the source code directory back to the docker host?

Related

Is it possible to "open" vscode to see the contents of a docker container?

I have a docker image, and I am running it now (finishing with bash)
When I do, I have a file structure inside the container.
However, this is not some file structure mapped (with -v) from outside the container. These files and folders exist only inside the container.
My question is, since it is bothersome to be opening each file with vi and navigating from the terminal, is there a way that I can open vscode on these files?
Be aware that these files do not exist outside the container
I found how to do it from this link
However I used the "attach to running container" command
I rarely do that but when I have to I usually mount an empty volume to the container, then exec into the container copy the folder which I need into that empty volume, which is then replicated on my host machine. From my host machine I then open it in vscode.
However please be careful if you have sensitive information in that container, not to expose something by accident.
So the steps are:
Create empty volume ( docker-compose example )
Note do not overwrite the folder/file which you want to extract. containerpath is path which does not exist in the container prior to creating it.
volume:
- ./hostpath:/containerpath
Find docker id so that you can use it to exec into it:
docker ps
Exec into the container:
docker exec -it <container_id> /bin/sh
Copy the file/folder to that empty volume:
cp -r folder containerpath
Exit the container and look at your files in ./hostpath folder.

-v deleted all the data from the docker container

I made a docker image called myImage, there is a folder: /data I want to let the user edit it by themselves. I read that -v flag can mount the volume, so I used it like following:
I run the container with this command:
docker run -v /my_local_path:/data -it myImage /bin/bash
But surprisingly, docker cleared all the files in /data in the container. But this is not I want... I want actually the host can get all the files from /data... :(
How can I do that?
When you share a volume like this, the volume on the host overwrites the volume in the container, so the files in the container's folder will be removed.
What you need to do is put the files in the container in folder A (a folder in the container). Mount folder B (another folder in the container). Then AFTER the volume is mounted, move the files from folder A to folder B. Then these files will be both available to the host and inside the container.
You can achieve this 'move files' operation using a RUN or an ENTRYPOINT script in your Dockerfile.
See Run a script in Dockerfile
Sorry, I forget if you need RUN or ENTRYPOINT (or if either will work) but one of these will definitely do it.
I think you want ENTRYPOINT because an ENTRYPOINT script runs AFTER the container is created. Thus it will run after the volume is mounted.

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

Sharing files between container and host

I'm running a docker container with a volume /var/my_folder. The data there is persistent: When I close the container it is still there.
But also want to have the data available on my host, because I want to work on code with an IDE, which is not installed in my container.
So how can I have a folder /var/my_folder on my host machine which is also available in my container?
I'm working on Linux Mint.
I appreciate your help.
Thanks. :)
Link : Manage data in containers
The basic run command you want is ...
docker run -dt --name containerName -v /path/on/host:/path/in/container
The problem is that mounting the volume will, (for your purposes), overwrite the volume in the container
the best way to overcome this is to create the files (inside the container) that you want to share AFTER mounting.
The ENTRYPOINT command is executed on docker run. Therefore, if your files are generated as part of your entrypoint script AND not as part of your build THEN they will be available from the host machine once mounted.
The solution is therefore, to run the commands that creates the files in the ENTRYPOINT script.
Failing this, during the build copy the files to another directory and then COPY them back in your ENTRYPOINT script.

Run a script into a container then copy files to host from the container

I want to run a script against a container and copy the output files back to the host. I have few questions:
Does the script need to be inside the container in order to run OR I can have the script in the host and still can run it against the container?
Copying files is available through cp command which only available in docker. Now in the container 'docker cp' is not available. So if the script is running inside the container how it can copy files to the host?
What I am trying to do is the following (my running container has mongodb):
Export certain collections to json files
Copy the resulted files to the host
As you can see some commands are available in the container such as 'mongoexport' and some are available in the host only like 'docker cp'.
Simply use a Docker volume—this is the best way to share data between containers and their host.

Resources