Docker Containers
- The Docker image is collection of layers,
- This image is mounted as one file system when the container starts. For this docker has a filesystems such as overlay or union which understand image layers
- Storage Driver allows docker to understand and mount multiple layers image and one read write layer as a file system inside container
- All the changes made by the container i.e. w.r.t file creations, modifications and deletions are stored in R/W Layer specific to the container
- When the container is deleted the R/W Layer also gets deleted so the changes done by container are also lost.
- Docker volumes will solve the problem of preserving data which we will look into very soon.
- Docker has some storage drivers
- overlay2
- aufs
- devicemapper
- btrfs
- zfs
- Refer Here for the article around storage drivers
- using Docker container we would run our applications inside containers and there are two kinds of applications
- stateless applications
- These applications will not use local storage for storing any information, they would use remote systems such as databases, shared filesystems etc
- stateful applications:
- These applications will use local storage for storing some application state/data
- examples: databases, traditional applications
- To make stateful applications work with docker, we need a way to persist the data created in the R/W Layer which is important for your application.
- To do this we need to understand docker volumes Refer Here for the article about docker volumes
- Lets do a small experiment. Create 3 containers as shown below
Docker Volumes
- To persist the data from the Docker container we will be using Docker volumes.
- There are three types of docker volumes
- Volumes:
- Stored in the host system and manged by Docker.
- Generally these volumes are stored in /var/lib/docker/volumes on Linux
- Non-Docker processes should not modify this and this is docker specific location.
- Bind Mounts:
- Can store the data any where on the host system by creating a mapping as shown in the below image
- Non-Docker processes can easily modify the contents which have been created by your container
- Can store the data any where on the host system by creating a mapping as shown in the below image
- tmpfs mounts
-
Working with Docker Volumes
- Now inspect docker container
docker container inspect c1
- Now we can use this volume even after the container is deleted to mount to other container with same image
- Lets think of a database like mysql, I want to run mysql inside container, We need to preserve the data stored in the database. But where does mysql store the data
- Lets observe the Dockerfile of mysql Refer Here
- Now lets create a volume for mysql
- Now lets create a mysql container
docker container run --name mysql -v mysqldb:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=qwert456 -d mysql