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

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.

Related

Docker bind mount is empty inside a containerized TeamCity build agent with DOCKER_IN_DOCKER enabled

I'm running containerized build agents using the linux-sudo image tag and, using a Dockerfile (here) I have successfully customized it to suit our needs. This is running very well and successfully running the builds I need it to.
I am running it in a Swarm cluster with a docker-compose file (here), and I have recently enabled the DOCKER_IN_DOCKER variable. I can successfully run docker run hello-world in this container and I'm happy with the results. However I am having an issue running a small utility container inside the agent with a bind mount volume.
I want to use this Dockerfile inside the build agent to run npm CLI commands against the files in a mounted directory. I'm using the following command to run the container with a custom command and a volume as a bind mount.
docker run -it -v $(pwd):/app {IMAGE_TAG} install
So in theory, running npm install against the local directory that is mounted in the container (npm is the command in the ENTRYPOINT so I can just pass install to the container for simplicity). I can run this on other environments (Ubuntu and WSL) and it works very well. However when I run it in the linux-sudo build agent image it doesn't seem to mount the directory properly. If I inspect the directory in the running utility container (the npm one), the /app folder is empty. Shouldn't I expect to see the contents of the bind mount here as I do in other environments?
I have inspected the container, and it confirms there is a volume created of type bind and I can also see it when I list out the docker volumes.
Is there something fundamental I am doing wrong here? I would really appreciate some help if possible please?

how to configure Cassandra.yaml which is inside docker image of cassandra at /etc/cassandra/cassandra.yaml

I am trying to edit cassandra.yaml which is inside docker container at /etc/cassandra/cassandra.yaml, I can edit it from logging inside the container, but how can i do it from host?
Multiple ways to achieve this from host to container. You can simple use COPY or RUN in Dockerfile or with basic linux commands as sed, cat, etc. to place your configuration into the container. Another way you can pass environment variables while running your cassandra image which will pass those environment variables to the spawning container. Also, can use the docker volume mount it from host to container and you can map the configuration you want into the cassandra.yaml as shown below,
$ docker container run -v ~/home/MyWorkspace/cassandra.yaml:/etc/cassandra/cassandra.yaml your_cassandra_image_name
If you are using Docker Swarm then you can use Docker configs to externally store the configuration files(Even other external services can be used as etcd or consul). Hope this helps.
To edit cassandra.yaml :
1) Copy your file from your Docker container to your system
From command line :
docker ps
(To get your container id)
Then :
docker cp your_container_id:\etc\cassandra\cassandra.yaml C:\Users\your_destination
Once the file copied you should be able to see it in your_destination folder
2) Open it and make the changes you want
3) Copy your file back into your Docker container
docker cp C:\Users\your_destination\cassandra.yaml your_container_id:\etc\cassandra
4) Restart your container for the changes to be effective

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

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?

Export environment variables from docker to host

I am running a docker container on Jenkins. I can't install anything on jenkins, so I did some processing on docker and want to get the results out to the host. If I set an environment variable in the docker container, how do I extract it to my jenkins host?
I can see that I can write the env variable to a file and copy it to the host, but is there another way ?
When running the container, you can mount a file or a folder into the container. Inside your container, you can write to this file and have the changed reflected on the host file on the machine.
To do that create a file result.txt on the host machine and when running the container, specify the -v option to mount the file.
docker run -v ./result.txt:/result.txt ...
And let the jenkins job write the results into this file.

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.

Resources