Docker image layers and container
- Every container gets an virtual disk which is collection of
- readonly layers (reusable across containers using same image)
- writable layer (unique for every container)

- For this to workout docker relies on storage driver, as of now
overlay driver does this.
- Any changes from files in readonly layers will be copied into writable layer and then modified.
- When we delete the container the writable layer also gets deleted i.e. data loss happens.
Solution: Docker volumes
- figure out the folder where you are storing the data i.e. required (should not be lost)
- For convienience lets assume the folder is
/data
- We need to ensure this folder lifetime has no dependency on container lifetime.
- Docker volumes help in creating storages which can have lifetime independent of containers.
- docker volumes can be created in two ways
- Mount some folder on docker host into docker container
- Ask docker to manage this folder
Option 1: Mounting some folder on host into container
- Create a new folder
mkdir /root/data
- now lets create an alpine container
docker run -d --name alp1 -v /root/data:/data alpine sleep 1d
Option 2: Create a docker volume and then mount
docker volume create mydata
- now lets create an alpine container
docker run -d --name alp1 -v mydata:/data alpine sleep 1d
-
docker volumes with extensions installed can store volumes in aws, azure, or even external locations
-
Docker supports VOLUME instruction in Dockerfile. This instruction automatically creates an anonymous volume if the user doesn’t create explicitly.
Lets create a mysql container
# create a volume
docker volume create nopdb
# create a mysql contianer
docker run -d \
-p 33061:3306 \
--name nopdb \
-v nopdb:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=admin123 \
-e MYSQL_DATABASE=nop \
-e MYSQL_USER=nop \
-e MYSQL_PASSWORD=nop123 \
mysql:9
- Watch classroom recording for next steps
- Exercise: Repeat the same with postgres db and mongodb in a docker container.
Next Steps
- Networking
- optimizations
- Microservice
- docker compose
Like this:
Like Loading...