Learn how to mount a host directory into a Docker container with docker run --mount source=/my/path,destination=/tmp nginx

Discover how to mount a host directory into a Docker container using docker run --mount source=/my/path,destination=/tmp nginx. This modern technique gives precise control over paths and mount types, making it easier to share code, configs, and logs between host and container, while keeping setups readable.

Outline:

  • Opening hook: the practical vibe of sharing files between host and container
  • What mounting really is in Docker (simple intuition)

  • The correct command and why it’s the right approach

  • Breaking down the syntax: --mount vs other methods

  • A concrete example with nginx to make it tangible

  • A quick note on bind mounts vs volumes

  • Practical tips and common gotchas

  • How this fits into broader Docker knowledge (storage, security, lifecycle)

  • Warm wrap-up

Mounting a host directory into a container: a quick, friendly guide

If you’ve ever run an app in Docker and wished your local files could be visible inside the container, you’re not alone. It’s a tiny trick, but it unlocks a lot of practical workflows—things like tweaking configuration, serving static files, or debugging with real data. In Docker terms, that trick is called mounting a directory from the host into the container. And yes, there’s a specific, sensible way to do it.

What mounting really means, in plain language

Think of a container as a self-contained apartment. You bring a few things in, but you also want access to a few things from the building’s hall closet (your host). Mounting is the door between those two spaces. You point Docker to a folder on your machine (the source) and say where inside the container you want to see it (the destination). The app inside the container can read and, depending on how you set it up, write to those files.

The correct command that makes it happen

Here’s the simple, direct approach many developers rely on when they need to share a host directory with a container:

docker run --mount source=/my/path,destination=/tmp nginx

That’s the one the Docker folks often recommend in practice, because it clearly states what’s coming from the host and where it lands inside the container. The key bits:

  • --mount is the flag that configures the mount.

  • source=/my/path is the directory on your host.

  • destination=/tmp is where inside the container you want to see it.

  • nginx is the image you’re running (you can substitute any image as needed).

If you’re curious about the more explicit flavor, you’ll sometimes see it written with the type parameter:

docker run --mount type=bind,source=/my/path,destination=/tmp nginx

Both achieve the same practical result here—the host directory becomes available inside the container. The second version makes the kind of mount you’re using explicit (bind mount, in this case). It’s a reminder that Docker supports different mounting modes, each with its own nuances.

Why this is the right approach—and how it compares to other options

Several choices exist for mounting files, and not all of them behave the same way. The example above uses the approach that’s clean, structured, and flexible for real-world work.

  • docker mount: Not a standalone Docker CLI command you’d use in most cases. It’s more about internal tooling or documentation rather than a direct run command.

  • docker run -m … (or similar flags): This isn’t the right syntax for mounting a host directory. The -m flag is for memory limits, not mounting filesystems.

  • docker attach: This tool connects you to a running container’s stdin/stdout, but it doesn’t mount host directories. It’s for live interaction, not filesystem sharing.

  • The right method (the --mount approach): It explicitly defines what you’re bringing in, from where, and where it goes—plus it scales well as you add more mount points or switch between bind mounts and volumes.

A concrete, everyday example you can visualize

Let’s pretend you’re working on a small Nginx setup and you want the container to serve files from a folder on your laptop. You might have a directory at /Users/alice/www that holds your site assets.

  • Using the straightforward form:

docker run --mount source=/Users/alice/www,destination=/usr/share/nginx/html nginx

  • Or, more explicit about the type:

docker run --mount type=bind,source=/Users/alice/www,destination=/usr/share/nginx/html nginx

In either case, what happens is this: the files live on your host, but the container can read them as if they were part of its own filesystem. If you’re serving static content, you’ll see updates inside the container instantly as you modify the files on your machine—super handy for quick iterations.

Bind mounts vs. volumes: a quick reality check

Two big ideas sit behind the mounting concept: bind mounts and Docker-managed volumes. They aren’t just technical terms; they reflect different workflows.

  • Bind mounts: You point Docker to a folder on your host. It’s transparent, flexible, and great for local development, config sharing, and debugging. The content on the host directly controls what the container sees.

  • Volumes: These are managed by Docker, stored in a special area on the host, and typically used for data you want Docker to own and manage across container lifecycles. They’re a solid choice when you’re moving things toward production or when you want portability and cleaner cleanups.

In many DCA-relevant scenarios, bind mounts do the job just fine. When you need durability and portability, volumes come into play. The choice isn’t about one being “better”—it’s about what fits your task, your team’s norms, and how you want to manage data.

Practical tips and common gotchas

  • Absolute paths matter: Always use an absolute path on the host (like /home/you/project). Relative paths can lead to surprises depending on where the command runs.

  • Check permissions: If the container can’t read or write, it’s often a permissions issue on the host folder. You may need to adjust file permissions or run the container with a user that matches your host’s permissions.

  • Don’t mount sensitive areas: It’s easy to slip in /etc or /root by accident. Be deliberate about what you mount.

  • Consider read-only mounts when appropriate: If your container only needs to read files, you can set the mount as read-only to reduce risk.

  • Verify the mount: If you’re unsure it worked, consult ls inside the container or run a quick test that writes to a file in the mounted directory. If it shows up on the host, the mount is active.

  • Windows and macOS quirks: Path syntax differs a bit on non-Linux hosts, and file permissions can be trickier. When in doubt, test with a tiny, simple directory first.

Connecting this to broader Docker knowledge

This mounting topic sits at the intersection of several core Docker concepts that show up in practical work:

  • Container lifecycle and isolation: Mounts illustrate how containers stay isolated yet can access specific host resources in controlled ways.

  • Storage strategies: Understanding when to use binds versus volumes helps with data management, backups, and portability.

  • Networking and orchestration basics: While not directly about networking, thinking about mounted directories often pairs with config files for services, which in turn ties into how you orchestrate multi-container apps.

  • Security implications: Mounts are powerful, so you want to keep an eye on what’s exposed and how permissions are set. Small choices here can prevent big headaches later.

A few human touches—because real work isn’t all code

Let me explain this with a tiny analogy you’ve probably used in daily life: giving a friend a folder in your house so they can drop in a map or notes whenever they need to. It’s convenient, but you’d like to keep some rooms private and set rules about who can mess with what. Mounting in Docker works the same way. You decide exactly what gets shared, where inside the container it lands, and whether it’s read-only or read-write. It’s sensible to keep the door modest, the path clear, and the access limited to what’s necessary.

If you’re exploring Docker for real-world projects, this concept crops up again and again—config files, assets, logs, you name it. The clarity of the --mount syntax makes it easier to reason about these connections. And that clarity pays off when you’re debugging or revisiting a project after a break.

Wrapping up with a clear takeaway

In short: when you want a host directory visible inside a container, the clean, predictable path is to use docker run with the --mount flag, explicitly naming source and destination. The exact form may vary a bit (source/destination vs type=bind and so on), but the principle remains the same: your host directory enters the container in a controlled, transparent way.

So next time you’re wiring up a container to use local configs, templates, or site assets, remember the door you’re choosing to open. It’s not just a line of code; it’s a practical bridge between your development environment and the containerized world. And in the grand scheme of Docker, this is one of those small moves that makes everything smoother—less friction, more speed, and a clearer sense of what each container really owns.

If you want to explore further, you’ll find the same ideas echoed across Docker docs and real-world projects—how people structure their mounts, how they secure them, and how they keep things tidy as teams grow. The more you experiment, the more this will feel like second nature—a natural rhythm you carry from local development into more ambitious setups.

And that’s really what this whole space is about: turning simple, precise commands into reliable, scalable workflows. Mounts are a perfect example—small, powerful tools that quietly shape how you work with containers every day.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy