PUBLISHED: October 26, 2020
Deprecation Notice: This article was written more than a year ago which means that its information might no longer be up-to-date. We cannot therefore guarantee the accuracy of it's contents.

Table of Contents


How to mount external volumes in Docker

Docker is an OS-level virtualization tool that allows us to run non-native software packages via containers. By default, external volumes are not mounted in Docker. We will in this guide show how they can be mounted on computers running macOS.


Getting started

Before getting started we will download a sample image in order to be able to follow along this tutorial. We will use Alpine Linux because it weighs only 5 MBs.

Let us download the image by running the following command in a terminal:

docker image pull alpine

Docker Mount External Volumes Image 1

Alpine Linux should be downloaded within seconds.


Mounting via the command line

We are now ready to run the Alpine Linux container and mount our external volume. For this example, let us assume that we just want to access some files on an external USB drive. To achieve this, we will need to supply the docker command with a -v /source:/destination flag.

Here is the complete command:

docker container run -it -v /Volumes/usb:/mnt/usb alpine /bin/sh

Docker Mount External Volumes Image 2

This will run Alpine Linux in an interactive terminal and mount the USB drive in /Volumes/usb/ onto the destination /mnt/usb. If we browse to the mounted directory and list its contents we can see that the files exist.


Mounting via the Docker UI

If you prefer not to use the command line it is also possible to mount volumes directly from within the Docker Desktop UI. Here is how to do it:

Open Docker Desktop, select Images in the left hand column, locate the container image and press the Run button.

Docker Mount External Volumes Image 3

Press the down arrow to expand the Optional Settings tab. Fill in the path to the external volume under Host Path and type in the destination path under Container Path. Press Run to launch the container.

Docker Mount External Volumes Image 4

Once the container is up and running we can open its Command Line Interface (CLI) by pressing the round button highlighted in red.

Docker Mount External Volumes Image 5

This will open a new terminal inside the Alpine Linux container. If we now browse to the mounted directory and list its contents we can see the files that exist on our USB drive.

Docker Mount External Volumes Image 6


Summary

This guide has shown how external volumes can be mounted inside Docker, via the command line, or the user interface.


Further information

To learn more about Docker, visit:

You can also type docker in a terminal to get a list of available command line arguments.

$ docker

Usage:	docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default
                           "/Users/hackintosh/.docker")
  -c, --context string     Name of the context to use to connect to the
                           daemon (overrides DOCKER_HOST env var and
                           default context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level
                           ("debug"|"info"|"warn"|"error"|"fatal")
                           (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default
                           "/Users/geekbitzone/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default
                           "/Users/geekbitzone/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default
                           "/Users/geekbitzone/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  deploy      Deploy a new stack or update an existing stack
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.

See Also

Which components can be upgraded on my Mac?
Which components can be upgraded on my Mac?

How to hide external disk drives from the desktop in macOS
How to hide external disk drives from the desktop in macOS

How to upgrade rsync on macOS Catalina
How to upgrade rsync on macOS Catalina

How to change the startup programs in macOS
How to change the startup programs in macOS

How to install Nextcloud on Mac with Docker
How to install Nextcloud on Mac with Docker

How to extend the lifespan of memory cards on the Raspberry Pi with Log2Ram
How to extend the lifespan of memory cards on the Raspberry Pi with Log2Ram

How to measure the network speed between two computers with iPerf
How to measure the network speed between two computers with iPerf

How to turn your Raspberry Pi into a Jukebox
How to turn your Raspberry Pi into a Jukebox

How to connect to GitHub with SSH
How to connect to GitHub with SSH

How to compile OpenEmu for macOS
How to compile OpenEmu for macOS



comments powered by Disqus

See also