Skip to content

Instantly share code, notes, and snippets.

@subfuzion
Last active February 5, 2023 03:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save subfuzion/b9e9a2fda9f0ee660f2652cccc0fc614 to your computer and use it in GitHub Desktop.
Save subfuzion/b9e9a2fda9f0ee660f2652cccc0fc614 to your computer and use it in GitHub Desktop.
How to use Docker bind mount instead of copying files for a local dev container.

Example of a Docker bind mount.

Create an example directory and copy the following script to it. Make it executable and then run it.

$ chmod +x demo.sh
$ ./demo.sh
.
.
.
hello world

The last line of the script demonstrates using a bind mount. The source directory ("$PWD"/src) is mounted so that its contents are accessible to a running container under the target directory (/src).

This is useful for dev containers where you want to build things without copying the entire local file system for the Docker build context.

#!/bin/sh
mkdir -p src
cat << EOF > src/file.txt
hello world
EOF
cat << EOF > Dockerfile
FROM alpine
CMD ["cat", "src/file.txt"]
EOF
docker build -t mount-ex .
docker run -it --rm --mount type=bind,source="$PWD"/src,target=/src mount-ex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment