DanDan4561
DanDan4561

Reputation: 393

How to pass a filepath into a docker file

I'm trying to build a simple container that will allow the user to pass in a directory file path to an executable which should exist on the host machine.

I want to have this somewhat flexible whereby if the user doesn't specify a directory filepath it will use the default one. From what I have researched I need to use ARG and potentially the -e flag at run time. Lets go through an example with this simple Dockerfile.

FROM ubuntu:latest
#Assign the default value
ARG DIRHOME=/opt/docker/q
#if -e has not been set the use default DIRHOME
ADD $DIRHOME /q

The Dockerfile resides in a folder /opt/docker where I will run

docker build --build-arg DIRHOME=/q .

This all runs successfully but as you can see I am always using the default value. Is there a way for the user to pass their filepath to the q executable during run time to overwrite the DIRHOME variable? Something like below

docker run -e "QHOME=/user1/lib/q" <container id>

I think the problem with this is to do with build context, so if I run my Dockerfile inside /opt/docker then it treats that as its root and it can't find the path /user1/lib/q. Any potential workarounds?

Upvotes: 0

Views: 15270

Answers (2)

David Maze
David Maze

Reputation: 158908

In general the useful pattern here is to use a fixed path inside the Docker container, and let the administrator mount whatever needs to be mounted there using the docker run -v option. There is absolutely no requirement that the host and container paths match. The path name can be a fixed property in the Dockerfile, it is not something you especially want to be configurable via an ARG.

If this was some sort of security scanner, say, an administrator could run

sudo docker run \
  -v $GOPATH/bin:/scan \
  myscanner \
  myprogram

The scanner would look at /scan/myprogram; but on the host that would be $GOPATH/bin/myprogram.

If the primary goal of your program is working with files on the host, consider that Docker as a design goal makes working with host files difficult; in addition to this volume-mapping question there are recurring filesystem-permission problems and more issues that crop up if you want to run this job from within Docker. Consider using some simpler packaging mechanism (distributing a statically linked binary, a Python virtual environment, an RPM or dpkg package, ...). As a user I’d much rather run

myscanner $(which myprogram)

Upvotes: 1

matanper
matanper

Reputation: 931

When you run the docker build you copy the files from DIRHOME inside the docker image. so there is no real use for DIRHOME after the build. it would make more sense to set the path inside the container as environment varaible:

ENV qpath /q
ADD /opt/docker/q $qpath

And then after that when you try to run the image with docker run the standard way to pass data from the host machine to the container is by using volumes. then you can use the environment variable:

docker run -v /user1/lib/q:/q image

Upvotes: 1

Related Questions