Skip to content

Instantly share code, notes, and snippets.

@aemloviji
Last active April 7, 2020 16:04
Show Gist options
  • Save aemloviji/229cf29af4b478ff48b3175aae28850a to your computer and use it in GitHub Desktop.
Save aemloviji/229cf29af4b478ff48b3175aae28850a to your computer and use it in GitHub Desktop.
Managing Docker Container Volumes

How to manage docker container volumes.

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. A volume allows data to persist, even when a container is deleted. Volumes can be used to share data between the host and the container(s).

By mounting a volume you can achive to

  • Pull data from a container.
  • Push data to a container.
  • Share data between containers.

Please note that, arguments prefixed with @(at) in below commands must be replaced with actual values.

Option 1: Docker volume by specifying a host directory

docker run -it --name [@container-name] -v [@directory-in-host-machine]:/[@directory-available-in-container] [@image-name] /bin/bash

Option 2: Use docker's volume create command

Step 1: Create a volume

docker volume create --name [@volume-name]

Step 2: Use volume when starting container

docker run -it --name [@container-name] -v [@volume-name]:/[@directory-available-in-container] [@image-name] /bin/bash

Option 3: Using Dockerfile

Step 1: Create a simple Dockerfile with and volume by Volume directive

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1

# create a volume
VOLUME /@directory-in-the-container

Step 2: Build and image

docker build -t [@image-name] .

Step 3: Use volume when starting container

docker run -it --name [@container-name] [@image-name] /bin/bash

Step 4: Use docker inspect command to see destination(directory in the container) and source(directory on the host machine) folders for created volume

docker inspect [@container-name]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment