Volumes:
By default docker doesnot persist the data. For example,
You run the mysql server in a docker and DB gets stored in a file. As long as Docker runs you will not have any problem. Due to some unexpected things the docker instance got removed . Also the DB is gone.
To avoid such stuff, Persistence Data needs to be outside of the docker container. So when the docker instance removed, you can launch a another docker container with this persistence data, so that it will retain the state.
Major places where the persistence data is required in,
- DBs (SQL, NoSQL or any sort of DB apps such as MySQL, Mongo, Cassandra etc)
- WebServers (apache2, ngnix,etc)
- Application needs to Log the Files which needs to be persisted.
To Persist the data in docker:
- Create a docker volume
- Attach the docker volume to the container at the time of container creation.
Volume is just a directory located in the host and will be mapped to the docker instance.
Docker Volume Creation :
docker volume create <volume name>
The volume will be created in /var/lib/docker/volumes folder.
To List :
docker volume list
To see the details :
docker inspect <volume name>
$ sudo docker volume create myvol1
myvol1
$ sudo docker volume list
DRIVER VOLUME NAME
local myvol
local myvol1
$ sudo docker inspect myvol1
[
{
"CreatedAt": "2017-10-23T05:52:38Z",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/myvol1/_data",
"Name": "myvol1",
"Options": {},
"Scope": "local"
}
]
Create a docker container with volume:
The mount option in the container creation can be used for volume.
--mount source=<volume name>,target=<target directory in the container>
<volume name> is the name of the volume
<target directory> - In the container, the volume will be mounted in the specified directory.
Example:
docker run -dit --name my-apache-app -p 8080:80 --mount source=myvol,target=/usr/local/apache2/htdocs/ httpd:2.4
Now you can query on port 8080 from the docker machine