Tuesday, September 18, 2018

Container Lifetime and Persistent data


Container Lifetime and Persistent data:

·         Containers are usually immutable and ephemeral
·         "immutable infrastructure": only re-deploy containers, never change
·         This is the ideal scenario, but what about databases, or unique data?
·         Docker gives us features to ensure these "separation of containers"
·         This is known as "persistent data"
·         2 ways:Volumes and Bind Mounts
·         Volumes: make special location outside of container UFS
·         Bind Mounts: link container path to host path

Persistent Data: Volumes
-VOLUME command in docker file.

Run below commands
docker pull mysql
docker image inspect mysql
docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True mysql
docker container ls
docker container inspect mysql

In the container inspection, you can see the volumes section and also the Mounts section separately, which means a space on host machine(or on the Linux VM if it is docker for windows) is provided to that container uniquely and the destination details.

docker volume ls   --> will show the volumes creates
and if we pick one id from it.
docker volume inspect <idpicked>



if we delete the container:
docker container stop mysql
docker container rm mysql mysql2  --> 2 separate containers.

it won't delete the volumes.
docker volume ls

in the docker container run command, -v referes to creation of a new volume(with name we want) or attach to the existing volume.
-v /var/lib/mysql
-v mysql-db:/var/lib/mysql ---> Named volume

docker volume ls
docker volume inspect mysql-db

if I use the run command again for a new container , as the volume exists it uses the existing one.

docker container run -d --name mysql3 -e MYSQL_ALLOW_EMPTY_PASSWORD=True -v mysql-db:/var/lib/mysql mysql

to create a volume..
docker volume create [OPTIONS] [VOLUME]


Persistent Data: Bind Mounting:
·         Maps a host file/directory to a container file/directory
·         Basically just 2 locations pointing to the same file(s).
·         Again , skips UFS, and host files overwrite any in container.
·         Can't use in Dockerfile, must be at (container run)

... run -v /Users/uday/stuff:/path/container (mac/linux)
... run -v //c/Users/uday/stuff:/path/container (windows)

Where this really comes to shine is with development and running services inside your container that are accessing files you're using on your host are changing.

Let's see for nginx.

docker container run -d --name nginx -p 80:80 -v $(pwd):/usr/share/nginx/html nginx

the default ngninx is with below.
docker container run -d --name nginx2 -p 8080:80 nginx

So if you see, the html pages are different.
As we mapped to a different file in the first container.

let's open a command line with the container on a separate shell window.

docker container exec -it nginx bash
cd /usr/share/nginx/html

this is the folder we mapped and we can see the files from the host machine.


-----------------------


Case studies:
---------------

database upgrades with named volumes:
----------------------------------------

docker container run -d --name psql -v psql:/var/lib/postgresql/data postgres:9.6.1
--create container for a specific image version 9.6.1
--as it runs in background,see the logs
docker container logs -f psql

docker container stop

docker container stop <psql_container_id>

now create a new container with psql version 9.6.2 and attach the same named volume.

Using Bind Mounts for editing host files on a container.:
----------------------------------------
docker run -p 80:4000 -v $(pwd):/site uday/folder1

Any changes we do on host machine, it will show on the logs of container also.

-----------------------------------
jekyll-serve  --- is a 3rd party tool to convert plain text to websites



No comments:

Post a Comment