Saddam Hussain I H
Saddam Hussain I H

Reputation: 200

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
WORKDIR /app
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 ?

Upvotes: 1

Views: 805

Answers (1)

atline
atline

Reputation: 31644

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.

Upvotes: 2

Related Questions