October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

Why Docker RUN Cannot See Build Files—and Why NAT Is Usually Outbound

Docker’s build context controls which files instructions can access; network settings control connections. Learn how to supply build files and configure inbound container access.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Docker build access and container network access are separate boundaries. A RUN command can read only files made available to the build, while a bridge-networked container can commonly make outbound connections through the host without automatically accepting unsolicited inbound connections. Fix a missing file by correcting the build context or mounting the file; fix reachability by configuring the relevant network or publishing a port.

Why a Docker build cannot see a project file

Docker Docs defines the build context as “the set of files that your build can access.” For a local build, the path supplied to docker build selects that set; the Dockerfile’s location does not grant access to every nearby host directory. Remote Git repositories, tarballs, and named contexts can also supply build inputs. See Docker’s build context documentation.

As an Amazon Associate I earn from qualifying purchases.

For example, in docker build -f docker/Dockerfile ., the final . is the context: the current directory. The Dockerfile may live under docker/, but that does not change the context. If instead you run docker build -f docker/Dockerfile docker, the context is docker/, so files elsewhere in the project are not available unless supplied separately.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why COPY ../something fails

COPY sources are resolved within the build context. Using .. cannot escape that boundary to read an arbitrary neighboring host file. This is intentional: the build receives only inputs made available through its declared context or other supported sources. Put the needed file inside the context, choose a suitable broader context, or add another source deliberately. Avoid expanding a context to include secrets or unrelated files. See the Dockerfile reference.

Check context and ignore rules first

  • Inspect the path at the end of the build command; that is the local context.
  • Check whether a .dockerignore file excludes the missing file.
  • If the file lives in another directory, provide that directory as a named context, for example with --build-context name=path, and refer to that named source deliberately in the build instructions.
  • If you use a text-only Dockerfile with docker build -, there is no local filesystem context from which to copy host files.

Docker documents local, remote, and named context behavior in its build context guide.

Make a context file available to a build instruction

Choose the method based on whether the file should remain in the resulting image or is merely an input to one build command.

Method When to use it What happens to the file
COPY (or ADD when its additional behavior is appropriate) The file belongs in the image or build stage. It is copied into the stage and persists in its image layer.
BuildKit RUN --mount=type=bind A command needs the file temporarily, such as a package installation using a requirements file. The bind-mounted file is available for that RUN and is not persisted in the final image.
Named context The input is in a separate directory or source that should be supplied explicitly. The build can refer to the declared source; whether files enter an image depends on how the Dockerfile uses them.

For a file included in the default context but needed only during one command, BuildKit supports a bind mount:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
# syntax=docker/dockerfile:1
RUN --mount=type=bind,source=requirements.txt,target=/tmp/requirements.txt 
    pip install --requirement /tmp/requirements.txt

The file is mounted for that instruction rather than copied into the resulting image. The source path must be available to the build, and Docker’s build best practices describe this pattern.

Does docker build --network=host make host files visible?

No. Network mode controls network access for build instructions; it does not enlarge the filesystem context. If a RUN command cannot read a file, inspect context selection, ignore rules, and mounts—not the network flag.

BuildKit documents three RUN --network modes:

  • default: the default network mode for the build instruction.
  • none: the instruction has no network access, although loopback remains available.
  • host: the instruction uses the host network environment.

BuildKit restricts host networking with the network.host entitlement: the builder and the build request must allow it. The Dockerfile reference explains the RUN network modes and entitlement; the CLI reference documents the build command’s network option. Exact support and configuration depend on the builder in use.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why a container can reach the internet but inbound traffic cannot reach it

At runtime, Docker bridge networking commonly uses masquerading for outbound container connections. That outbound path does not by itself publish a service running inside the container to incoming connections. Inbound access is configured separately: port publishing maps a host address and port to a container port, while routing may also be configured depending on the network setup.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For example, publishing a port with -p or --publish establishes an explicit host-to-container mapping. Which host address is bound and whether traffic can reach it depend on the publication and network configuration. Docker explains the distinction in its port publishing and mapping documentation. The phrase “NAT only goes one way” is a useful shorthand for the common bridge default, not a rule that inbound access can never be configured.

Best Value
Docker Container Linux Devops Programming Coding T-Shirt
  • Docker, Docker Swarm, Docker Compose, Programmer, Developer, Coding, Programming, Software Engineer, Code, DevOps, Deploy, Deployment, Kubernetes, Salt, Puppet, Chef, Terraform, Container, AWS, Azure, Cloud, Geek, Funny, Computer, Software, Tech, IT
  • Integration, Scrum, Compile, Compilation, Science, Bug, Debug, Python, Linux, Java, Javascript, Scala, Dotnet, Kotlin
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem

Diagnose the failure at the right boundary

Symptom Boundary to check First checks
COPY or RUN cannot find a project file Build filesystem inputs Build context path, .dockerignore, named contexts, and whether the file is copied or mounted.
A build command cannot download packages Build-time networking RUN --network mode, DNS, proxy settings, and the builder environment.
A running container can connect out, but an outside client cannot connect in Runtime networking Whether the service listens on the intended container port and whether the port is published or routing is configured.

Changing build networking cannot reveal an unprovided host file; adding a file mount cannot create network reachability. Treat the build context, build-step network, and runtime port configuration as distinct settings.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.