Docker mounted folder does not contain any files - docker

I am creating a docker container. In the docker file I use the following lines:
RUN mkdir -p /data
VOLUME ["/data"]
This should create an empty folder called "data" and expose it as a volume.
Then I run the container like this:
docker container run mycontainer -v $(pwd)/data:/data
On my host system I have a folder called "data" in the current directory, which contains several files.
The problem now is, that the container does not see the files. It contains an empty folder called "data", but there are no files in it. Are other steps required to get the host system files to actually show up in the container?

Related

Docker - mounted directory is empty inside container

I am attempting to mount a non-empty directory into a container, however, the directory inside the container is empty. The directory does not already exist in the container. My command is:
docker run --volume /tmp:/test busybox:latest ls -l /test
I have also tried this using the --mount flag instead but no success. Why is the /test directory inside my container empty? It should contain the contents of /tmp from the host.
Finally figured things out, in part provoked by #BMitch's comment. I was running Docker in a multi-node environment so Docker was creating the new container on a separate node. I solved the problem by coping the file into the new container via docker cp

Why docker run can't find file which was copied during build

Dockerfile
FROM centos
RUN mkdir /test
#its ensured that sample.sh exists where the dockerFile exists & being run
COPY ./sample.sh /test
CMD ["sh", "/test/sample.sh"]
Docker run cmd:
docker run -d -p 8081:8080 --name Test -v /home/Docker/Container_File_System:/test test:v1
Log output :
sh: /test/sample.sh: No such file or directory
There are 2 problems here.
The output says sh: /test/sample.sh: No such file or directory
as I have mapped a host folder to container folder, I was expecting the test folder & the sample.sh to be available at /home/Docker/Container_File_System post run, which did not happen
Any help is appreciated.
When you map a folder from the host to the container, the host files become available in the container. This means that if your host has file a.txt and the container has b.txt, when you run the container the file a.txt becomes available in the container and the file b.txt is no longer visible or accessible.
Additionally file b.txt is not available in the host at anytime.
In your case, since your host does not have sample.sh, the moment you mount the directory, sample.sh is no longer available in the container (which causes the error).
What you want to do is copy the sample.sh file to the correct directory in the host and then start the container.
The problem is in volume mapping. If I create a volume and map it subsequently it works fine, but directly mapping host folder to container folder does not work.
Below worked fine
docker volume create my-vol
docker run -d -p 8081:8080 --name Test -v my-vol:/test test:v1

WORKDIR as VOLUME

In my dockerfile, I have my WORKDIR and I want to have it as a VOLUME, so that on the host I have a directory in /var/lib/docker/volumes/ where is the same content as in the WORKDIR.
How do I use the VOLUME Dockerfile command for this?
While you can mount a volume over the WORKDIR that you were using when building your image, the volume isn't available at build time. Volumes are only available for a container, not while building an image.
You can COPY files into the image to represent the content that will exist in the volume once a container is running, and use those temporary files to complete the building of the image. However, those exact files would be inaccessible once a volume is mounted in that location.
To have a directory from the host machine mounted inside a container, you would pass a -v parameter (you can do multiple -v params for different directories or for individual files) to the docker run command that starts the container:
docker run -v /var/lib/docker/volumes:/full/path/inside/container your_image_name

docker run -v works even without VOLUME or mkdir

What is the use of "VOLUME" or "RUN mkdir /m"?
Even if I do not specify any of these instructions in the Dockerfile, then also "docker run -v ${PWD}/m:/m" works.
Inside a Dockerfile, VOLUME marks a directory as a mount point for an external volume. Even if the docker run command doesn't mount an existing folder into that mount point, docker will create a named volume to hold the data.
RUN mkdir /m does what mkdir does on any Unix system. It makes a directory named m at the root of the filesystem.
docker run -v ... binds a host directory to a volume inside a container. It will work whether or not the mount point was declared as a volume in a Dockerfile, and it will also create the directory if it doesn't exist. So neither VOLUME or RUN mkdir are specifically necessary before using that command, though they may be helpful to communicate the intent to the user.

How to merge host folder with container folder in Docker?

I have Wikimedia running on Docker. Wikimedia's extensions reside in extensions/ folder which initially contain built-in extensions (one extensions = one subfolder)
Now I wish to add new extensions. However I don't prefer the option of modifying the Dockerfile or creating new commit on the existing container.
Is it possible to create a folder in the host (e.g. /home/admin/wikimedia/extensions/) which is to be merged (not to overwrite) with the extension folder in the container? So whenever I want to install new extension, I just copy the extension folder to the host /home/admin/wikimedia/extensions/
You can mount a volume from your host to a separate location than the extension folder, then in your startup script, you can copy the contents to the container's directory. You will need to rebuild your host once.
For example:
Dockerfile:
RUN cp startup-script /usr/local/bin/startup-script
CMD /usr/local/bin/startup-script
startup-script:
#!/bin/bash
cp /mnt/extensions /path/to/wikipedia/extensions
/path/to/old-startup-script $#
docker run -d -v /home/admin/wikimedia/extensions:/mnt/extensions wikipedia
That is one way to get around this problem, the other way would be to maintain a separate data container for extensions, then you will mount this and maintain it outside of the wikipedia container. It would have to have all the extensions in it.
You can start one like so:
docker run -d -v /home/admin/wikimedia/extensions:/path/to/wikipedia/extensions --name extensions busybox tail -f /dev/null
docker run -d --volumes-from extensions wikipedia

Resources