How To Create And Manage Docker Volume
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed the docker network.
In this blog post, we will discuss docker volume. We will also create a volume explicitly and attach it to a docker container.
Prerequisite for this demo:
- One AWS EC2 instance with Linux 2 AMI and internet access.
What is docker volume?
By default, all files created inside a container are stored on a writable container layer. This is great if the file is not required to persist even after the container is stopped. But suppose you have created a database instance inside your container, in that case, you may need a persistent storage option so that you can retrieve the data event when the container is no longer running.
One of the solutions that you can implement for persistent storage is docker volume. By default, docker volumes are created and managed by docker. You can also create a volume explicitly using the docker volume create command, or Docker can create a volume during container or service creation. When you create a volume, it is stored within a directory on the Docker host. When you mount the volume into a container, this directory is what is mounted into the container. This is similar to the way that bind mounts work, except that volumes are managed by Docker and are isolated from the core functionality of the host machine. A given volume can be mounted into multiple containers simultaneously. When no running container is using a volume, the volume is still available to Docker and is not removed automatically.
When you mount a volume, it may be named or anonymous. Anonymous volumes are not given an explicit name when they are first mounted into a container, so Docker gives them a random name that is guaranteed to be unique within a given Docker host. Besides the name, named and anonymous volumes behave in the same ways. Volumes also support the use of volume drivers, which allow you to store your data on remote hosts or cloud providers, among other possibilities.
Next, we are going to create a docker volume and attach it to a container.
How To Create And Manage Docker Volume Demo:
## Docker: Volume Management
## docker volume [COMMAND]
## Create a volume
## docker volume create
docker volume create myVOLUME
## List all volumes
## docker volume ls
docker volume ls
## Display detailed information on one or more volumes
## docker volume inspect
docker volume inspect myVOLUME
## Create a container with volume
## docker container run --name
## remove the container if already exist, docker container rm -f myWEBSERVER
docker container run --name myWEBSERVER --publish 80:80 --volume myVOLUME --detach myhttpd
## Remove all unused local volumes
docker volume prune
## press N to abort
## Remove one or more volumes
## docker volume rm
docker volume rm myVOLUME
Hope you have enjoyed this article. In the next blog post, we will discuss Dockerfile.
To get more details on docker, please refer below docker documentation