Unable to get the local directory referenced to docker


I have a console application published in linux, the application reads the data from a particular directory in Linux. so if I want to run the console application, I would do the below

./myapp "/home/user1/mydata"

Files in mydata directory will be changing. It all works fine when I run the application directly in the Linux terminal.
But when I dockerize the application, I am unable to read the directory "/home/user1/mydata".

Below is Dockerfile contents

FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
COPY . .
ENTRYPOINT ["./myapp"]

my intention is when I run the docker image, I will also the include path of the directory

for example

docker run myimage:latest "/home/user1/mydata"

I understand that in order to read the directory, I need first mount the directory, so I created a volume

docker volume create myvolume

and then mounted my target directory

docker run -t -d -v my-vol:/home/user1/mydata --name mycontainer myimage:latest

even after mounting when I am running the docker as

docker run myimage:latest "/home/user1/mydata"

It is still unable to read the directory. Am I doing something wrong here ? After mounting the directory do I have to change the way I call my argument in this case /home/user1/mydata ?


docker volume create myvolume will create a folder in docker system location, while -v my-vol:/home/user1/mydata will pop the /home/user1/mydata in container to that docker host’s system location, typically /var/lib/docker/volumes.

So, for your case, you need to use bind mount, not volume, something like next:

docker run -t -d -v /home/user1/mydata:/home/user1/mydata --name mycontainer myimage:latest "/home/user1/mydata"

-v /home/user1/mydata:/home/user1/mydata will mount the folder /home/user1/mydata on docker host to container’s /home/user1/mydata, so this I guess could meet your requirement.

Answered By – atline

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published