title: Mounting a directory within a docker build
body: <p>I'm wondering how to use a simple script with a docker container.</p>
  <p>The script is:</p>
  <h1>example python script</h1>
  <pre class="lang-py prettyprint-override"><code># Example python script
  import argparse
  import pathlib
  
  
  def run(
      *,
      input: pathlib.Path | str,
      output: pathlib.Path | str,
  ) -&gt; None:
      pathlib.Path(output).write_text(pathlib.Path(input).read_text().upper())
  
  
  def main() -&gt; int:
      desc = &quot;example script&quot;
      parser = argparse.ArgumentParser(
          description=desc,
          formatter_class=argparse.RawDescriptionHelpFormatter,
      )
  
      parser.add_argument(
          &quot;-i&quot;,
          &quot;--input&quot;,
          help=(&quot;input file&quot;),
          required=True,
      )
  
      parser.add_argument(
          &quot;-o&quot;,
          &quot;--output&quot;,
          help=(&quot;output file&quot;),
      )
  
      parser.add_argument(
          &quot;-x&quot;,
          &quot;--overwrite&quot;,
          help=(&quot;Whether to overwrite previously created file.&quot;),
          action=&quot;store_true&quot;,
      )
  
      args = parser.parse_args()
  
      if not pathlib.Path(args.input).exists():
          raise FileNotFoundError(f&quot;input file {args.input} not found&quot;)
  
      if not args.output:
          raise argparse.ArgumentError(f&quot;output not given&quot;)
  
      if pathlib.Path(args.output).exists() and not args.overwrite:
          raise FileExistsError(f&quot;{args.output} already exists. &quot;)
  
      run(input=args.input, output=args.output)
  
  
  if __name__ == &quot;__main__&quot;:
      raise SystemExit(main())
  
  </code></pre>
  <p>The script works fine on my system (without docker).</p>
  <h1>example docker file</h1>
  <p>The Dockerfile is:</p>
  <pre><code>FROM python:3.10.6-bullseye
  COPY . .
  ENTRYPOINT [&quot;python&quot;, &quot;example.py&quot;]
  </code></pre>
  <p>This works (ish) after the following:</p>
  <pre><code># build
  docker build -t demo .
  
  # run
  docker run demo --help
  </code></pre>
  <p>Which outputs:</p>
  <pre><code>usage: example.py [-h] -i INPUT [-o OUTPUT] [-x]
  
      example.
  
  
  options:
    -h, --help            show this help message and exit
    -i INPUT, --input INPUT
                          input file
    -o OUTPUT, --output OUTPUT
                          output file
    -x, --overwrite       Whether to overwrite previously created file.
  </code></pre>
  <p>But I'm not sure how to use it with the <code>-i</code> and <code>-o</code> arguments.</p>
  <h1>what I'd like to do</h1>
  <p>I'd like to be able to do the following:</p>
  <pre><code>echo &quot;text&quot; &gt; input.txt
  
  # Create output from input
  docker run demo -i input.txt -o output.txt
  
  # Create output from input and say it's ok to overwrite
  docker run demo -i input.txt -o output.txt -x 
  </code></pre>
  <p>And after this there by a output.txt file created which has TEXT in it.</p>
  <h1>Error</h1>
  <p>I've tried to do this with the above command, and it doesn't work.</p>
  <p>Eg:</p>
  <pre><code>echo &quot;this&quot; &gt; input.txt
  
  docker run demo -i input.txt -o output.txt -x
  </code></pre>
  <p>After this there is no <code>output.txt</code> file created which has <code>THIS</code> in it.</p>
  <h1>Attempted solution (<code>--mount</code> within the shell command)</h1>
  <p>Using the following seems to work - but it feels as though It's a lot in a shell command :</p>
  <pre><code>docker run  \
      --mount type=bind,source=&quot;$(pwd)&quot;,target=/check \
      --workdir=/check demo:latest \
      -i input.txt -o output.txt -x
  </code></pre>
  <p>Is there a way to do the <code>--mount</code> within the dockerfile itself?</p>