Skip to content

Instantly share code, notes, and snippets.

@vandycknick
Last active June 5, 2020 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vandycknick/5904de530348b0b389da42ca615c887d to your computer and use it in GitHub Desktop.
Save vandycknick/5904de530348b0b389da42ca615c887d to your computer and use it in GitHub Desktop.
Docker Volumes
FROM node:alpine
WORKDIR /app
RUN echo "hello" > hello-world.txt
# Build the image
docker build -t test-volume-shizzle .
# Creates a new container with an id that has a volume attached
docker create -v /app --name container-id-10 test-volume-shizzle /bin/true
# If you started with a clean docker env you should have 1 volume now
docker volume ls
# On mac you can also attach to the docker vm tty
screen ~/Library/Containers/com.docker.docker/Data/vms/0/tty
# And then, you should also see that volume here. It should show the same id!
ls -ltrh /var/lib/docker/volumes
# Start a new container with volumes from the container named earlier (container-id-10)
# you should be able to see the file with cat /app/hello-world.txt
docker run -it --volumes-from container-id-10 ubuntu bash
# Lets exit and clean up everything
docker rm $(docker ps -aq)
docker rmi $(docker images -aq)
# That volume should still be there
docker volume ls
# The file should be 6 bytes, and docker system df claims that the size used by volumes is 6B
docker system df
# Let's create a few more
docker create -v /app --name container-id-11 test-volume-shizzle /bin/true
docker create -v /app --name container-id-12 test-volume-shizzle /bin/true
docker create -v /app --name container-id-13 test-volume-shizzle /bin/true
docker create -v /app --name container-id-14 test-volume-shizzle /bin/true
docker create -v /app --name container-id-15 test-volume-shizzle /bin/true
# The amount of volumes taken up should now be 36B
docker system df
# Let's clean up again, docker will tell it cleaned up 36B of volumes
docker rm $(docker ps -aq)
docker rmi $(docker images -aq)
docker volume prune
# This shows that each volume created from a container contains
# all the files and folders behind that given folder. In this case /app.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment