How to write files to a docker container's temporary directory? - docker

I am trying to write content(backup files) into a temporary directory inside a running Docker container.
Is there a way to create a temporary directory inside the container and write content to it?

You can bind this folder to the docker container directly using volumes.
As you do not want them changed I assume you could mount them as a read-only volume.
In that case you do not have to copy them around as you will have them accessible already in the container :)

Related

How do multiple containers access the directory in other container

How do multiple containers(create from same image) access the directory(packaged in the image) in other container? There are some ways not elegant:
Mount directory into a shared volume or host path. When a container changed files in the directory, other containers will perceive it. Is there any idea to make shared volume working like copy-on-write?
Create a volume for each container, and copy directory into the volume. In this way, the directory have to save multiple copies in disk.
I've browsed this problem [https://stackoverflow.com/questions/29550736/can-i-mount-docker-host-directory-as-copy-on-write-overlay], I don't want containers to run in privileged mode. Is there a better idea?
Per design one container cannot access the files inside another container.
You will need to store the data outside of a container, then mount it via volume mount or bind mount. Alternatively you could try to perform some NFS/SMB/... mount of shared storage inside the container.
This is completely independent from running a container in host mode.

Docker "write only" volume?

I would like to have a Docker container be able to copy files into a volume without being able to read its contents / know what files are inside of it.
The container should also be able to append contents to a file inside that volume.
Is there a way to achieve any of this by using Docker compose?
No, there is not. Docker volumes are mounted to the filesystem either read-write or read-only: https://docs.docker.com/storage/volumes/#use-a-read-only-volume.

How to keep file from mounted volume and create new container with that file?

I have a docker container image that requires me to mount a volume containing a specific configuration file, in order for that container to properly start (this image is not one that I have control over, and is vendor supplied). If that volume is not mounted, the container will exit because the file is not found. So I need to put a configuration file in /host/folder/, and then:
docker run --name my_app -v /host/folder:/container/folder image_id
The application will then look in /container/folder/ for the file it needs to start.
I want to create/commit a new image with that file inside /container/folder/, but when that folder is mounted as volume from the host, docker cp will not help me do this, as far as I have tried. I think, as far as docker is concerned, the file copied there is no different than the files in the mounted volume, and will disappear when the container is stopped.
Part of the reason I want to do this, is because the file will not be changed, and should be there by default. The other reason is that I want to run this container in Kubernetes, and avoid using persistent volumes there to mount these directories. I have looked into using configmaps, but I'm not seeing how I can use those for this purpose.
If you can store the file into the ConfigMap you can mount the file to volume and use it inside the Kubernetes.
I am not sure with the type of file you have to use.
ConfigMap will inject this file into the volume of a POD so the application could access and use it.
In this case there will be no PVC required.
You can also follow this nice example showing how to mount the file into a volume inside a pod.
OR
Also, I am not sure about the docker image but if you can use that docker image you can add the file into the path, something like:
FROM <docker image>
ADD file ./container/folder/
In this case, you might have to check you can use the vendor docker image as a base and add the file into it.

Give Docker access to host directory but discard changes later

I want to achieve the following with Docker: I want to give a container access to a host directory, such that the container can make changes, but the changes are discarded once the container is exiting/removed (pretty much like an overlayfs).
Simply mounting the directory as a volume for the docker container seems like the wrong way to me, since changes made to a volume persist and I don't want that.
How do I tackle this problem?
The only way for a container to modify the host is to mount a directory between the host and the container. But the changes made by host or container will persist.
You could try the other way: COPY the files you want from host to container using a Dockerfile. The files will be only on the container. When you remove and launch another one, the new container will start with the original files.

Dockerfile vs. docker-compose VOLUME

This experiment tries to build a container using this Docker file:
FROM lambdalinux/baseimage-amzn:2016.09-000
COPY ./bundle /opt/bundle/
VOLUME /bundle
Then inside the container, create a /opt/bundle/file.txt and put some text in it. But that file did not show up in the bundle directory on the host as I expected after reading Should I include my code with COPY/ADD or a volume last paragraph:
There may be cases where you’ll want to use both. You can have the image include the code using a COPY, and use a volume in your Compose file to include the code from the host during development. The volume overrides the directory contents of the image.
Doesn't Dockerfile VOLUME do the same as docker-compose.yml VOLUME? If so, how can this be done so that changes in the host directory is reflected inside the container directory in this case?
I also created a file on the host bundle/play.txt but that did not show up inside the container /opt/bundle/...
A VOLUME instruction in the dockerfile creates a mount point but initially only maps it to Docker's internal data directory.
In order to map the volume to the host filesystem, you need to specify which path on the host should be mapped to the volume. You can do this in the docker-compose file using the volumes parameter. (Note: you can create volumes using docker-compose without declaring them in the Dockerfile.)
Note that when mapping a directory from the host to a container, the directory contents on the host will replace the contents in the container, not vice versa.

Resources